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


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...???.


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.


Very Thanks, :smile: