Meteor - Quick Tips #3

Template Helpers and Subscriptions

Templates helpers format data onto your templates, but one must ensure that subscription data is fully loaded before the template helpers have a chance to render.

With the help of Iron Router, use the ‘this.ready()’ check inside the data option.

Router.js
1
2
3
4
5
6
7
8
9
10
11
12
Router.route('/post/:_id', {
waitOn: function() {
return Meteor.subscribe('post', this.params._id)
},
data: function () {
if (this.ready()) {
return {
post: Posts.findOne({_id: this.params._id});
}
}
}
});

Once the above is in place, a template helper can now safely rendered the subscription data.

Template.js
1
2
3
4
5
Template.post.helpers({
post: function() {
return Template.currentData().post;
}
});

Remove Event Listeners From Blaze Templates

As of Meteor 1.0.4.2, this can not be done. The Blaze to HTML looked promising, but this only creates static html element from a template, it does not provide the ability to render an active Blaze template inert.

For example the following template has a click event listener attached, but there is not way to undo or remove the click event listener.

Click Event Listener
1
2
3
4
5
Template.my-template.events({
"click #container a": function(evt) {
//do some stuff here
}
});