Last active
August 29, 2015 13:56
-
-
Save literalsands/9295397 to your computer and use it in GitHub Desktop.
Meteor Template Syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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