admin管理员组文章数量:1201982
I am trying to render a backbone collection on a template that is built with mustache.js . The problem is I couldn't get the cid of the model in the template. My code is
<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>
from the above code, I can get the aptId, beginTime and end Time, but not the Cid. How to get the Cid of the model from a collection while rendering it on a template?
and my render method from the view looks like this
render:function(){
var template = _.template($("#slot-display-template").html());
compiledTmp = template({slots: this.collection.toJSON()})
this.$el.append(compiledTmp);
}
Also is there any disadvantage of using cid as the unique identifier of a model ?
Thanks in advance !!!
I am trying to render a backbone collection on a template that is built with mustache.js . The problem is I couldn't get the cid of the model in the template. My code is
<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>
from the above code, I can get the aptId, beginTime and end Time, but not the Cid. How to get the Cid of the model from a collection while rendering it on a template?
and my render method from the view looks like this
render:function(){
var template = _.template($("#slot-display-template").html());
compiledTmp = template({slots: this.collection.toJSON()})
this.$el.append(compiledTmp);
}
Also is there any disadvantage of using cid as the unique identifier of a model ?
Thanks in advance !!!
Share Improve this question edited Mar 8, 2013 at 16:38 Ben 10.1k5 gold badges42 silver badges42 bronze badges asked Mar 8, 2013 at 16:16 digToGetWaterdigToGetWater 1712 silver badges9 bronze badges 1 |3 Answers
Reset to default 23The cid
is not included by default in the toJSON
output. You will need to override toJSON
in your model definition and include cid
.
toJSON: function() {
var json = Backbone.Model.prototype.toJSON.apply(this, arguments);
json.cid = this.cid;
return json;
}
If you need an ad hock solution, This would work also:
var params = _.extend({}, this.model.toJSON(), {cid: this.model.cid})
By the way if you don't need to extend behavior of all models you can just add cid
to your model using parse
method. For example you have collection 'Collection'. You can specify model for this collection and override parse
method to attach model's cid
to the response.
var Collection = Backbone.Collection.extend({
model: Model
});
var Model = Backbone.Model.extend({
parse: function(response) {
response.cid = this.cid;
return response;
}
});
So you'll be able to get cid
from model's attributes.
本文标签:
版权声明:本文标题:javascript - cannot get the cid of the model while rendering a backbone collection over a template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738623462a2103327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
collection.get(modelId)
. – Paul Hoenecke Commented Mar 8, 2013 at 16:32