Last active
August 29, 2015 14:16
-
-
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`.
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
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