包含一个模型
Item = Backbone.Model.extend({....})
Apvdtl = Backbone.Model.extend()
和一个集合
Apvdtls = Backbone.Collection.extend({
model: Apvdtl
})
并使用 Apvdtl 模型填充集合,例如
apvdtls.add([{ itemid: 'c4676cef679a11e28b7a3085a942bd8e', qty: 10 },
{ itemid: '1da17339690f11e285d63085a942bd8e', qty: 5 }])
并生成了这个视图
但我想做的是做出这样的看法
通过在此JSON 文件上获取带有 ID 的项目
ApvdtlView = Backbone.View.extend({
tagName: 'tr'
initialize: function(){
this.model.on('change', this.render, this);
this.template = _.template('<td><%=itemid%></td><td><%=qty%></td>');
},
render: function(){
item.set({'id': this.model.get('itemid')});
// item = {id: 'c4676cef679a11e28b7a3085a942bd8e'}
item.fetch(); // but this doesnt get the data
// this should contain this data after i fetch
// item = {"id": "c4676cef679a11e28b7a3085a942bd8e",
// "code": "prp125", "descriptor": "Paper", "price": "7.00"}
// build new data to render
var data = {
"itemid": item.get('descriptor'),
"qty": this.model.get('qty')
}
this.$el.html(this.template(data));
//this.$el.html(this.template(this.model.toJSON()));
return this;
}
});