backbone.js - Binding a model to a backbone client template -
i have following backbone.js client side template:
<script id="calleetemplate" type="text/x-jquery-tmpl"> <tr style="background-color: ${statuscolour}"> <td class="responder">${contactfullname}</td> <td class="status" style="width:200px">${status}</td> <td class="replied">${replied}</td> <td class="wauto">${response}</td> </tr> </script>
in order able bind these properties, have following render method of view:
app.views.callees = backbone.view.extend({ initialize: function () { }, el: $('#calleesstatuses'), render: function () { var col = _.map(this.collection.models, function (item) { return item.attributes; }); $('#calleesstatuses').html(''); $('#calleetemplate').tmpl(col).appendto(this.el); } });
i have extract attributes using underscore _.map
function model. think reason because backbone uses .get("property")
extract property value.
this not seem right me, doing wrong?
you're right, have transform data in order able use tmpl.
however, it's better practice use tojson
rather accessing attributes directly. it's best avoid calling .models
directly well.
you don't need anyway, backbone collections have full suite of underscore.js enumerators. can cut transformation down 1 line:
var col = this.collection.invoke('tojson')
Comments
Post a Comment