Reason of subscribe in autorun?

Hello all,

I tot subs is reactive by default. In the following example, what is the reason to have subs inside autorun? Please advice, thank you.

Template.todoList.onCreated(function () {
  var self = this;
  // When this todo list template is used, get
  // the tasks we need.
  self.autorun(function () {
    self.subscribe(“tasksInList”, Template.currentData()._id);
  });
});

The autorun block will re-run whenever the Template.currentData() changes, so when you switch from one list to another, the subscription will rerun.

onCreated is only called the first time a template is used, or the next time it is used after it is destroyed. So if you change the context of the template, without destroying the template, you need to use an autorun block to detect that change

2 Likes

Ok it’s because onCreated run only once if it doesn’t get destroy and use to show again. Thanks for clearing this up. Appreciate much.