Created
February 5, 2013 00:06
-
-
Save amatiasq/4710958 to your computer and use it in GitHub Desktop.
This allow Handlebars to look up Backbone Model's attributes: {{ user.address.street }} If user is a Backbone Model this will become user.get("address").street And if user.get("adress") is also a Backbone Model this will be produced: user.get("address").get("street")
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
Handlebars.JavaScriptCompiler.prototype.nameLookup = function(parent, name, type) { | |
var result = '(' + parent + ' instanceof Backbone.Model ? ' + parent + '.get("' + name + '") : ' + parent; | |
if (/^[0-9]+$/.test(name)) { | |
return result + "[" + name + "])"; | |
} else if (Handlebars.JavaScriptCompiler.isValidJavaScriptVariableName(name)) { | |
return result + "." + name + ')'; | |
} else { | |
return result + "['" + name + "'])"; | |
} | |
}; |
WARNING: Don't use it if Backbone.Model isn't global, it will broke all the templates.
Handlebars exposes this function in order to allow us to overwrite it.
I love this. Thank you!
Great ! Thank you !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested with Handlebars 1.0.rc.1