Javascript 我是否需要将两个模型传递到此视图中?
我有一个页面,其中显示了许多照片,每个照片都是由视图Javascript 我是否需要将两个模型传递到此视图中?,javascript,jquery,backbone.js,underscore.js,Javascript,Jquery,Backbone.js,Underscore.js,我有一个页面,其中显示了许多照片,每个照片都是由视图PhotoListItemView呈现的。单击照片时,会出现一个模式视图ModalAddToSetView,其中包含一个集合列表SetListView。单击其中一个SetSetView时,我需要将photo\u id和Set\u id发送到后端 问题:在集合的点击处理程序中,我可以通过使用this.model.get('id')轻松获取点击集合的Set\u id。获取照片id的传统方法是什么 照片视图 photo\u id位于传递到此视图的模型
PhotoListItemView
呈现的。单击照片时,会出现一个模式视图ModalAddToSetView
,其中包含一个集合列表SetListView
。单击其中一个SetSetView
时,我需要将photo\u id
和Set\u id
发送到后端
问题:在集合的点击处理程序中,我可以通过使用this.model.get('id')
轻松获取点击集合的Set\u id
。获取照片id的传统方法是什么
照片视图
photo\u id
位于传递到此视图的模型中
模态视图
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')
})
}
});
我认为将
photo_id
参数传递给SetListView
构造函数并再次传递给SetView
构造函数没有任何问题:
// code simplified and no tested
SetListView = Backbone.View.extend({
initialize: function( opts ) {
this.photo_id = opts.photo_id;
this.collection.on('reset', this.render, this);
},
render: function() {
this.collection.each(function(set, index) {
$(this.el).append( new SetView({ model: set, photo_id: this.photo_id }).render().el );
}, this);
}
});
SetView = Backbone.View.extend({
initialize: function( opts ) {
this.photo_id = opts.photo_id;
},
addToSet: function() {
$.post('api/add_to_set', {
photo_id: this.photo_id,
set_id: this.model.get('id')
})
}
});
请注意如下全局变量的实例化
modalAddToSetView=new ModalAdd…
。。。请记住使用var
,否则您会发现奇怪的行为。谢谢,我将为所有行为添加一个var!我想知道是否有任何地方我可以发布我的代码,并让人们审查它的古怪你的意思是这样的谢谢!不知道这是可能的。从opts
param任何主干元素构造函数,您都可以访问传递给构造函数调用的哈希。有一些神奇的参数,如collection
、el
、model
等,它们会自动执行,但添加自己的参数没有任何问题。实际上,我看到新的主干将所有构造函数参数附加到this.options
以供将来参考。
// code simplified and no tested
SetListView = Backbone.View.extend({
initialize: function( opts ) {
this.photo_id = opts.photo_id;
this.collection.on('reset', this.render, this);
},
render: function() {
this.collection.each(function(set, index) {
$(this.el).append( new SetView({ model: set, photo_id: this.photo_id }).render().el );
}, this);
}
});
SetView = Backbone.View.extend({
initialize: function( opts ) {
this.photo_id = opts.photo_id;
},
addToSet: function() {
$.post('api/add_to_set', {
photo_id: this.photo_id,
set_id: this.model.get('id')
})
}
});