Meteor - Quick Tips #2

02/14/2015, Sat
Categories: #JavaScript
Tags: #Meteorjs

Blaze Template Removal

Instead of using jQuery to perform DOM removal operations, Meteor's Blaze remove can do the same thing with the following

// Remove Element with Blaze

Blaze.remove(Template.instance().view);

The above can be run in the Template's render function or in a Template event handler. Most likely, the removal will take place in an event handler.

Attach Template's Local Variables to Template Instance

Since all Templates files variables are encapsulated within a closure, there might be possibilities of memory leaks. To ensure that local variables are properly cleaned up, attached a data store object to the Template instance. When the Template instance is destroy, so will all the references to its variables.

// DataStore Object on Template Instance

var ds = {
  thing1: 1,
  thing2: 2,
};

Template.myTemplate.created = function () {
  this.ds = ds;
};