Use Track.autorun() in "onCreate of Template"?


#1

Many blog show “how to use pub/sub”,

Template.blogPost.onCreated(function() {
  var self = this;
  self.autorun(function() {
    var postId = FlowRouter.getParam('postId');
    self.subscribe('singlePost', postId);  
  });
});

But I don’t understand Track.autorun() use for...???.


#2

The subscription is for a single post (hence the postId in the subscribe call). Tracker.autorun ensures that the subscription is re-run when the postId changes (because the viewer went to another post) to receive the new post data.


#3

Very Thanks, :smile: