View によってそれぞれレンダリングされた多数の写真を表示するページがありますPhotoListItemView
。ModalAddToSetView
写真をクリックすると、セットのリストを含むモーダル ビューが表示されますSetListView
。Set の 1 つSetView
がクリックされたら、photo_id
andset_id
をバックエンドに送信する必要があります。
問題:set_id
Set のクリック ハンドラーで、を使用してクリックされたセットの を簡単に取得できますthis.model.get('id')
。を取得する従来の方法は何photo_id
ですか?
写真の見方
photo_id
このビューに渡されたモデルにある
PhotoListItemView = Backbone.View.extend({
events: {
'click #add.photo_btn' : 'add'
},
add: function(event) {
event.stopImmediatePropagation();
// Show modal
$('#modal_addit').modal();
modalAddToSetView = new ModalAddToSetView({ model: this.model });
}
});
モーダルの見方
ModalAddToSetView = Backbone.View.extend({
initialize: function() {
this.render();
this.renderSets();
},
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
return this;
},
renderSets: function() {
this.setList = new SetCollection();
this.setListView = new SetListView({ collection: this.setList });
this.setList.fetch({
data: {user_id: $('#user_id').val()},
processData: true
});
}
});
セットの見方
SetListView = Backbone.View.extend({
initialize: function() {
this.collection.on('reset', this.render, this);
},
render: function() {
this.collection.each(function(set, index) {
$(this.el).append( new SetView({ model: set }).render().el );
}, this);
}
});
SetView = Backbone.View.extend({
template: _.template( $('#tpl_modal_addit_set').html() ),
events: {
'click': 'addToSet'
}
render: function() {
$(this.el).html( this.template( this.model.toJSON() ) );
return this;
},
addToSet: function() {
$.post('api/add_to_set', {
photo_id: , // HOW DO I PASS THE PHOTO_ID?
set_id: this.model.get('id')
})
}
});