Created
December 15, 2013 07:25
-
-
Save craigmaslowski/7969987 to your computer and use it in GitHub Desktop.
Function for defining a Meteor template.
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
// 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 | |
} | |
}); |
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
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