Meteor - Quick Tips #3

03/25/2015, Wed
Categories: #JavaScript
Tags: #Meteorjs

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

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 render the subscription data.

// Template.js

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 no way to undo or remove the click event listener.

// Click Event Listener

Template.my.template.events({
  "click #container a": function (evt) {
    //do some stuff here
  },
});