ここにフィドルがあります
モデルを含む
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;
}
});