我需要将2个模型传递到此视图中吗
Do I need to pass 2 Models into this View?
我有一个页面,其中显示了View PhotoListItemView
渲染的多张照片。单击照片时,会出现模式视图ModalAddToSetView
,其中包含集合SetListView
的列表。当单击其中一个Set SetView
时,我需要将photo_id
和set_id
发送到后端。
问题:在Set的点击处理程序中,我可以使用this.model.get('id')
轻松获得点击的集合的set_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')
})
}
});
我认为将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')
})
}
});
相关文章:
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- 在离子态上加载多个视图
- Backbone.js视图中的多个模型
- 如何加载嵌套3个状态的UI路由器UI视图模板
- 如何应用带过滤器的ng if来过滤记录,并在同一页面中显示两个不同的视图
- 使用应用程序状态js和视图在angular js url上传递两个参数
- 在视图中渲染多个按钮以编程方式进行本机反应
- 如何在Famo.us中的两个视图(不同的js文件)之间进行通信
- Dojo:在单页应用程序中交换两个不同的视图
- rails 4.1.6 form_for coffeescript在多个视图上不起作用
- d3日历视图:如何将所有内容放在一个svg中,而不是放在几个svg中
- 谷歌地图没有显示在带有angularjs的第二个部分视图上
- 将多个事件动态添加到多个 Web 视图
- 具有多个控件的数据视图
- Angular中的同一视图多个控制器
- 主干组由两个视图组成
- jquery-mobile的列表视图中的两个弹出窗口
- 对于多个视图,当URL更改时,我的子模板不会在Angular UI中加载
- 嵌套状态的多个视图
- Ember.js - 一个视图多个布局(已登录/未登录)