I know this is correct as I followed the examples in the guide, but when else should I use autorun? For instance I have to check the FlowRouter route name sometimes and I put that in an autorun in onCreated. Is that correct? Should I use a separate autorun function for each task or use them same one for all tasks?
An autorun establishes a reactive context which is re-executed whenever any reactive data it depends on changes. In addition, an autorun always runs once (the first time it is executed).
In your example, the first execution of the autorun subscribes to two publications. However, neither publication (nor anything else in that autorun) is a reactive dependency, so the autorun is never re-executed (there is no need). That means your example could be rewritten as:
If you have a conventional mapping of route name to template, then you should not need to check the FlowRouter route name. In any event, if you do need to check the name, then it probably should not be in a template autorun. The example in the docs uses a Tracker.autorun, which may be defined elsewhere (including globally).
Therefore each autorun should be limited to a particular task. For example, perhaps you have an autorun that transforms some data when a checkbox is toggled, and another that retrieves a new page of data when the URL query parameters are updated (eg FlowRouter.getQueryParam). Since these are unrelated tasks, they need to be two separate autoruns; otherwise both tasks will run every time either scenario is triggered.
Without the “this.autorun()”, everybody would make a subscription for nothing …
But when Meteor.user() will change, subscription will automatically be done