Skip to content

Instantly share code, notes, and snippets.

@jrf0110
Last active August 29, 2015 14:16
Show Gist options
  • Save jrf0110/a22084c45c22face52e1 to your computer and use it in GitHub Desktop.
Save jrf0110/a22084c45c22face52e1 to your computer and use it in GitHub Desktop.
When handlebars calls a function via a template, the context of the function call is always set to the data passed into the template. This botches functions that rely `this`.
var handlebars = require('handlebars');
var tmpl = handlebars.compile('hello, {{person.getName}}');
var bob = Object.create({
name: 'Bob'
, getName: function(){
console.log( 'this', this );
return this.name;
}
});
// this { name: 'Bob' }
// 'Bob'
bob.getName()
// this { person: { name: 'Bob' } }
// 'hello, '
tmpl({ person: a })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment