Currently reading about FlowRouter. Faced with example with autorun function and can’t find good explanation. Also want to understand what object ‘this’ will be. Please help with useful links.
Template.blogPost.onCreated(function() { var self = this; self.autorun(function() { var postId = FlowRouter.getParam('postId'); self.subscribe('singlePost', postId); }); });
Tracker.autorun(…) allows converting a piece of code into reactive code when the source data it relays on changes.
According to the Meteor documentation, .onCreated is a Template method, so “this” is bound to a template instance object that is unique to this inclusion of the template and remains across re-renderings… not sure how in the code you pasted can do .autorun(…). I’d like to know too.
In this case, Tracker.autorun() is making the console to automatically display the “There are X posts” message whenever the posts collection changes, where X is the number of posts in the collection. This is pretty cool since it saves you lots of lines of code.
this.autorun -> this refers to the template instance
Then the ES2015 arrow function notation binds the parent scope, so you don’t have to do the var self = this dance.