walmartlabs/thorax — pull request #85 by eastridge (2012-12-22 01:17:12+00:00)
Title: Add local view helpers

Handlebars 1.0.0-rc1 removed a feature (or bug) that allowed methods that were passed to the template context to be called with arguments.

var view = new Thorax.View({
exclaim: '!',
myMethod: function(arg) {
return arg + this.exclaim;
},
template: '{{myMethod "hello"}}'
});
view.render();
view.html() === 'hello!'

Methods without arguments may still be called in this manner but is unclear wether that is an omission on Handlebars part. The documented way to call a method is via a helper, this pull request introduces a `helpers` hash to accomplish what was possible in Handlebars 1.0.0-b6:

var view = new Thorax.View({
exclaim: '!',
helpers: {
myMethod: function(arg) {
return arg + this.exclaim;
}
},
template: '{{myMethod "hello"}}'
});
view.render();
view.html() === 'hello!'

Since the helper method is always called with a context of the declaring view the `options` object gains a `context` property containing the current handlebars context:

var view = new Thorax.View({
helpers: {
myBlockMethod: function(options) {
return options.fn(options.context);
}
},
key: 'value',
template: '{{#myBlockMethod}}{{key}}{{/myBlockMethod}}'
});
view.render();
view.html() === 'value'