Skip to content

Instantly share code, notes, and snippets.

@literalsands
Last active August 29, 2015 13:56
Show Gist options
  • Save literalsands/9295397 to your computer and use it in GitHub Desktop.
Save literalsands/9295397 to your computer and use it in GitHub Desktop.
Meteor Template Syntax
// A bit hacky...
// I'm using Underscore
// Make Template a function. Gets and extends template instances.
Template = _.extend(function(instance_name, instance_functions) {
var instance = Template[instance_name];
if (instance && _.isObject(instance_functions))
_.extend(instance, instance_functions);
return instance;
}, Template);
var do_chain = function(obj, func_name) {
var func = obj[func_name]; // Don't be recursive;
return function() {
func.apply(obj, arguments);
return obj;
};
};
// Quite hacky...
// Make template instance helpers and events function return the template instance.
_.chain(Template).keys().each(function(instance_name) {
if (instance_name[0] !== '_') { // Don't touch __define__, etc.
var instance = Template[instance_name];
instance['helpers'] = do_chain(instance, 'helpers');
instance['events'] = do_chain(instance, 'events');
}
});
// Oooh, chains and stuff.
Template('template-instance', {
created: function() {},
rendered: function() {},
preserve: function() {},
destroyed: function() {},
someHelper: function() {}
})
.helpers({
})
.events({
});
// Don't think anything is broken...
Template['template-instance'].someHelper();
Template['template-instance']();
// Can still overwrite elsewhere.
Template['template-instance'].rendered = function(){this.breakEverything = "Nah."};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment