backbone.js - Iterate through arbitrary json in backbone/underscore template -
in normal case value via key, value of name <%= name %>, have know key "name". question if not know key of json field in first place, how can iterate through them, example, dump key-value pairs in table?
you transform object array of objects keys , values attributes. example,
var obj = {one: 1, two: 2, three: 3}; var fields = _.map( _.pairs(obj), function(pair) { return { key: pair[0], value: pair[1] }; } ); would yield [{key: "one", value: 1}, {key: "two", value: 2}, {key: "three", value: 3}]
you can feed array template,
var template = _.template( '<% _(list).each(function(field) { %>'+ '<%= field.key %> : <%= field.value %><br>'+ '<% }); %>' ); $('body').append(template({ list: fields })); and fiddle play http://jsfiddle.net/nikoshr/kvxun/
Comments
Post a Comment