我正在尝试在使用 mustache.js 构建的模板上渲染 Backbone 集合。问题是我无法在模板中获取模型的 cid。我的代码是
<div class="phone span4">
<h5> Phone Appointments</h5>
{{ _.each(slots, function(slot) { }}
{{ if(slot.aptType == "P"){ }}
<h6 cid="{{=slot.cid}}" aptId="{{=slot.aptId}}"> {{=slot.beginTime}} - {{=slot.endTime}} </h6>
{{ } }}
{{ }); }}
</div>
从上面的代码中,我可以得到 aptId、beginTime 和 end Time,但不能得到 Cid。如何在模板上渲染时从集合中获取模型的 Cid?
我在 View 中的渲染方法看起来像这样
render:function(){
var template = _.template($("#slot-display-template").html());
compiledTmp = template({slots: this.collection.toJSON()})
this.$el.append(compiledTmp);
}
另外,使用 cid 作为模型的唯一标识符有什么缺点吗?
提前致谢!!!
最佳答案
默认情况下,cid
不包含在 toJSON
输出中。您需要在模型定义中覆盖 toJSON
并包含 cid
。
toJSON: function() {
var json = Backbone.Model.prototype.toJSON.apply(this, arguments);
json.cid = this.cid;
return json;
}
关于javascript - 在模板上渲染主干集合时无法获取模型的 cid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15298449/