Backbone.js的:如何将参数传递到一个视图 [英] backbone.js: how to pass parameters to a view
问题描述
我有一系列的点击显示正好位于按钮下方的弹出菜单时按钮。我想按钮的位置传递给视图。我该怎么办呢?
= ItemView控件Backbone.View.extend({
标签名:礼,
事件:{
'点击':'showMenu
},
初始化:功能(){
_.bindAll(这一点,'渲染');
},
渲染:功能(){
返回$(this.el)的.html(this.model.get('名'));
},
showMenu:功能(){
VAR itemColl =新ItemColl();
新MenuView({集合:itemColl}); //如何通过这里菜单的位置?
}
});
您只需要传递,当你构建MenuView额外的参数。无需添加初始化
功能。
新MenuView({
收藏:itemColl,
位置:this.getPosition()
})
,然后在 MenuView
,您可以使用 this.options.position
。
更新:为<一个href=\"http://stackoverflow.com/questions/7803138/backbone-js-how-to-pass-parameters-to-a-view#comment28706113_7807449\">@mu太短状态的,因为1.1.0,骨干查看不再自动连接传递给构造为this.options的选择,但你可以自己做,如果你preFER。
因此,在你的初始化
方法,你可以保存选项
通过为这一点。选项
:
初始化:功能(选件){
this.options =选择;
_.bindAll(这一点,'渲染');
},
或者使用一些更精细的方式由@Brave戴夫描述。
I have a series of buttons which when clicked display a popup menu positioned just below the button. I want to pass the position of button to the view. how can I do that?
ItemView = Backbone.View.extend({
tagName: 'li',
events: {
'click': 'showMenu'
},
initialize: function() {
_.bindAll(this, 'render');
},
render: function() {
return $(this.el).html(this.model.get('name'));
},
showMenu: function() {
var itemColl = new ItemColl();
new MenuView({collection: itemColl}); // how to pass the position of menu here?
}
});
You just need to pass the extra parameter when you construct the MenuView. No need to add the initialize
function.
new MenuView({
collection: itemColl,
position: this.getPosition()
})
And then, in MenuView
, you can use this.options.position
.
UPDATE: As @mu is too short states, since 1.1.0, Backbone Views no longer automatically attach options passed to the constructor as this.options, but you can do it yourself if you prefer.
So in your initialize
method, you can save the options
passed as this.options
:
initialize: function(options) {
this.options = options;
_.bindAll(this, 'render');
},
or use some finer ways as described by @Brave Dave.
这篇关于Backbone.js的:如何将参数传递到一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!