Why does a Meteor.subscribe reloads my client's page ?

108 views
Skip to first unread message

Styve Simonneau

unread,
Dec 16, 2014, 4:40:36 AM12/16/14
to meteo...@googlegroups.com

I'm calling Meteor.subscribe on a click, and unfortunately, if I comment the line, it does not refresh the page anymore. Why does it refresh the page and how can I dodge this ?

client side :
'click #add_addr_save': function (e) {
    e
.preventDefault();
   
var $form = $('#add_address');

   
var addr = {};
    addr
.departement = $form.find('#add_departement').val();
    addr
.city = toTitleCase($form.find('#add_city').val());
    addr
.addr = $form.find('#add_addr').val();
    addr
.addr_comp = $form.find('#add_addr_comp').val();
    addr
.contact = $form.find('#add_addr_contact').val();
    addr
.note = $form.find('#add_addr_note').val();

   
Meteor.call('checkAddr', addr, function (err) {
       
if (!err) {
           
Meteor.subscribe('addresses', Meteor.userId());
       
} else
        console
.log(err);
   
});
});

server side, the publish :

Meteor.publish('addresses', function(opt) {
    var fields = {};
    if (opt != this.userId) {
        fields = {fields: {city: true, departement: true} };
    }
    var user = Meteor.users.findOne({$or: [{_id: opt}, {username: opt}]});
    if (user) {
        var addresses = user.addresses;
        return Address.find({_id: {$in : addresses}}, fields);
    }
    return Address.find({_id: -1});
});

And the html part, in case it changes everything..

<p class="text-center">
   
<button class="btn btn-info" id="add_addr_save">Add</button>
</p>




steph643

unread,
Dec 16, 2014, 8:16:44 AM12/16/14
to meteo...@googlegroups.com
Can you please explain the logic of what you are trying to do? Why do you need to call subscribe when saving a new address? (subscribe is to load data, right?)
Having some elements of a page refreshed when calling subscribe is very common: if a template HTML uses a helper that returns a cursor, then any change in the subscribe that changes the cursor will trigger a refresh.

Kelly Copley

unread,
Dec 16, 2014, 8:27:50 AM12/16/14
to meteo...@googlegroups.com
I can't say that this is true in your case but I had an issue with the new iron:router where I was using a waitOn hook and changes to the subscription would cause the page to seem to refresh.. Turned out that it was due to the new loading plugin which automatically renders a loading template for routes which have a waitOn.. Changing waitOn to subscriptions fixed it in my case. 

If this turns out not to be your issue then, If you have a repository I can look at to see the code you are using, I'm more than happy to.

On Tue, Dec 16, 2014 at 8:16 AM, steph643 <sylvain...@gmail.com> wrote:
Can you please explain the logic of what you are trying to do? Why do you need to call subscribe when saving a new address? (subscribe is to load data, right?)
Having some elements of a page refreshed when calling subscribe is very common: if a template HTML uses a helper that returns a cursor, then any change in the subscribe that changes the cursor will trigger a refresh.

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/53540c87-3ff5-448b-b342-9dc450d0efa4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Dornhardt - Daniel Dornhardt Development

unread,
Dec 16, 2014, 9:28:06 AM12/16/14
to meteo...@googlegroups.com
Another hint:

You're supplying the userid to the publication as a parameter...

Inside of the publish function on the server, you can and probably should use this.userId (i think, check the doc for details). The advantage is that this makes the publication reactive, getting re-run every time the user document changes on the server!

This means, if you update a user somehow, somewhere, from the client / the server, the publication gets rerun, queries get re-run and the results get re-published.

But only if you user this.userId, not if it's coming as a param from the client!

Not sure if that is important for your current problem, I just thought that it's a good thing to know (and can save a lot of work).

Also, I found out, that calling the same publication multiple times with the same parameters won't actually re-run the publication if the params didn't change!

Best wishes

Daniel

Daniel Dornhardt, Daniel Dornhardt Development
+49 152 - 56 17 22 61

Styve Simonneau

unread,
Dec 16, 2014, 12:22:06 PM12/16/14
to meteo...@googlegroups.com


Here is my template Adresses, which contains the template addAddress. The button contained in addAdress does the subscribe on click.

<template name="addresses">
    <h3>Registered addresses</h3>

    <div class="panel-body">
        <div class="col-sm-12">
            {{#each getAddr}}
                <div class="col-sm-6">
                    <input type="hidden" class="addressIndex" value="{{_id}}"/>
                    <address>
                        <p><strong>{{getCity city}}</strong>
                            <small>&mdash; {{departement}}</small>
                        </p>
                        <p>{{addr}}{{#if addr_comp}} &mdash; {{addr_comp}}{{/if}}</p>
                        {{#if contact}}<p><abbr tile="Contact">Tel</abbr>: {{contact}}</p>{{/if}}
                        {{#if note}}
                            <p>
                                <small>{{note}}</small>
                            </p>{{/if}}
                    </address>
                </div>
            {{/each}}
        </div>
        <div class="col-sm-12">
            <legend>Add new</legend>
            {{> addAddress}}
        </div>
    </div>
</template>

and the getAddr function, in case...

Template.addresses.helpers({
    getAddr: function () {
        var user = Meteor.user();
        var addr = [];
        if (user) {
            addr = user.addresses;
            ret = [];
            addr.forEach(function (elem, index, arr) {
                var address = Address.findOne(elem);
                if (address) {
                    Meteor.subscribe('ville', address.ville);
                    ret.push(address);
                }
            });
            return ret;
        }
    }
});

PS : this works. it displays my addresses, but when I subscribe it refreshes the page. And if I don't, then nothing happens, no address is added visually, but if I reload the page, then I can see the new address.

Styve Simonneau

unread,
Dec 16, 2014, 12:24:23 PM12/16/14
to meteo...@googlegroups.com
Thank you for your answer,
my data is already subscribed in the waitOn.

I have a private repository that you cannot access, maybe if I don't find the problem, i'll zip you that so you can look further..

Styve Simonneau

unread,
Dec 16, 2014, 12:25:04 PM12/16/14
to meteo...@googlegroups.com, dan...@dornhardt.com
Hi and thank you for the advice, i'll change that. :)

Styve Simonneau

unread,
Dec 17, 2014, 3:56:11 AM12/17/14
to meteo...@googlegroups.com
I have an update for you, so you can understand better.
Here is a video (1 min at max) of my problem. Check this out please. http://youtu.be/ANSAODuVywY
Reply all
Reply to author
Forward
0 new messages