Created
September 5, 2013 16:48
-
-
Save majimenezp/6452860 to your computer and use it in GitHub Desktop.
Como handlebars en el each no soporta el tipo Backbone.Collection, con este helper uno puede recorrer la coleccion pasándola a un objeto json para que handlebars la pueda renderear.
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.registerHelper('eachBackbone', function (context, options) { | |
var context1 = context.toJSON(); | |
var fn = options.fn, inverse = options.inverse; | |
var i = 0, ret = "", data; | |
var current; | |
if (options.data) { | |
data = Handlebars.createFrame(options.data); | |
} | |
if (context1 && typeof context1 === 'object') { | |
if (context1 instanceof Array || context1 instanceof Backbone.Collection) { | |
for (var j = context1.length; i < j; i++) { | |
if (data) { data.index = i; } | |
current = context1.at ? context1.at(i) : context1[i]; | |
ret = ret + fn(current, { data: data }); | |
} | |
} else { | |
for (var key in context1) { | |
if (context1.hasOwnProperty(key)) { | |
if (data) { data.key = key; } | |
ret = ret + fn(context1[key], { data: data }); | |
i++; | |
} | |
} | |
} | |
} | |
if (i === 0) { | |
ret = inverse(this); | |
} | |
return ret; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment