Where does Tracker.autorun go


#1

I was reviewing @arunoda repo on FlowRouter and it mentions once you registered your subscription you can reactively check on it using this

Tracker.autorun(function() {
    console.log("Is myPost ready?:", FlowRouter.subsReady("myPost"));
    console.log("Does all subscriptions ready?:", FlowRouter.subsReady());
});

What I don’t understand is where I put this piece of code should it be in the ‘subscription’ inside the route? or should I just include this as a helper or outside of a helper but in that same file. Not sure where to put this.


#2

You can put it anywhere you like, it doesn’t need to be in the same file as long as it runs on the client.


#3

I haven’t looked at @arunoda 's code but I’m guessing this goes into the Templates created method. For example

Template.templateName.created = function () {
  var template = this;
  template.autorun(function() {
    // Make user reactive so that it reloads after the userProfile
    // subscription is ready.
    var user = Meteor.user();
    if(user) {
      template.subscribe('something_about_the_user');
    }
  }
}

#4

@robsw has a good point – if you use this.autorun inside a template’s created hook, the function is automatically stopped when the instance gets removed again.

(Note, in regard to user data, that all publications are automatically rerun whenever login status changes.)


#5

Excellent! Thank you.