productListView
画像のリストを含むページがありますproductView
。モデルを含むproductListView
コレクションにバインドされます。画像がクリックされると、写真がクリックされた製品に関する詳細を含むモーダルが表示されます。productList
product
ModalView
問題:ユーザーに転送されるデータを最小限に抑えるために、ページとロードfetch
時に製品のいくつかの属性のみが編集されました。内の写真がクリックされたときに、モデルをより多くの属性(非常に長い説明など)でproductListView
更新するにはどうすればよいですか?product
productListView
モデル
Product = Backbone.Model.extend({
url: '/api/product' // Gets FULL details of product
});
コレクション
ProductCollection = Backbone.Collection.extend({
url: '/api/products' // Gets MINIMAL details of product
})
意見
ProductListView = Backbone.View.extend({
el: '#photo_list',
initialize: function() {
this.collection.bind('reset', this.render, this);
},
render: function() {
this.collection.each(function(photo, index) {
$(this.el).append(new ProductView({ model: photo }).render().el);
}
return this;
},
});
ProductView = Backbone.View.extend({
tagNAme: 'div',
template: _.template( $('#tpl_ProductView').html() ),
events: {
'click .photo': 'showModal',
},
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
return this;
},
// Creates the Modal View with FULL details of the product
showModal: function() {
modalView = new ModalView({ model: this.model });
}
});
モーダルビュー
ModalView = Backbone.View.extend({
el: $('#modal'),
template: _.template( $('#tpl_modal').html() ),
initialize: function() {
this.render();
},
render: function() {
$(this.el).show().append( this.template( this.model.toJSON( this.model ) ) );
},
});
アップデート
エラーが発生しますUncaught TypeError: Object [object Window] has no method 'render'
。_.bindAll
バインドに使用しているのに、なぜこれがそうなのrender
ですか?私var self=this
はうまくいくことを知っていますが、なぜ_.bindAll
ですか?
initialize: function() {
_.bindAll(this, 'render');
var self = this;
// Update Model with Full details
this.model.fetch({
data: {post_id: this.model.get('id')},
processData: true,
success: function() {
// The usual renders
this.render();
}
});