Skip to content

Instantly share code, notes, and snippets.

@m-coding
Forked from amatiasq/Handlebars- Backbone.js
Created March 29, 2016 06:59
Show Gist options
  • Save m-coding/d486f2dfda8ab821fe5a to your computer and use it in GitHub Desktop.
Save m-coding/d486f2dfda8ab821fe5a 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")
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 + "'])";
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment