Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
Created December 15, 2013 07:25
Show Gist options
  • Save craigmaslowski/7969987 to your computer and use it in GitHub Desktop.
Save craigmaslowski/7969987 to your computer and use it in GitHub Desktop.
Function for defining a Meteor template.
// pass n template name parameters
// the last parameter contains the object that defines the template.
// pass either an object literal or a function that returns an object for the last parameter
Templates.define('postSubmit', 'postEdit', {
// assign either an object literal or return an object from a function
helpers: {
post: function (id) {
Posts.findOne(id);
}
},
// assign either an object literal or return an object from a function
events: function () {
if (Session.get('someSetting')) {
return {
'submit form': function (e) {
e.preventDefault();
// do stuff
}
};
}
return {
'submit form': function (e) {
e.preventDefault();
// do other stuff
}
}
};
},
// assign either an object literal or return an object from a function
preserve: {
'input[id]': function (node) {
return node.id;
}
},
// assign a function
created: function () {
// do stuff
},
// assign a function
rendered: function () {
// do stuff
},
// assign a function
destroyed: function () {
// do stuff
}
});
Templates.define = function () {
var args = Array.prototype.slice.call(arguments, 0);
var tmpl = _.result(args.pop());
_.each(args, function (name) {
Template[name].helpers(_.result(tmpl, 'helpers'));
Template[name].events(_.result(tmpl, 'events'));
Template[name].preserve(_.result(tmpl, 'preserve'));
Template[name].rendered = tmpl.rendered;
Template[name].created = tmpl.created;
Template[name].destroyed = tmpl.destroyed;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment