如何将参数传递给视图
我有一系列按钮,单击这些按钮时会显示位于按钮正下方的弹出菜单。我想将按钮的位置传递给视图。我怎样才能做到这一点?
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?
}
});
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?
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只需在构造 MenuView 时传递额外的参数即可。无需添加
initialize
函数。然后,在
MenuView
中,您可以使用this.options.position
。更新:如@mu状态太短,自 1.1.0 起,主干视图不再自动附加作为 this.options 传递给构造函数的选项,但如果您愿意,您可以自己执行此操作。
因此,在您的
initialize
方法中,您可以可以将传递的options
保存为this.options
:或者使用一些更好的方法,如 描述的作者:@Brave Dave。
You just need to pass the extra parameter when you construct the MenuView. No need to add the
initialize
function.And then, in
MenuView
, you can usethis.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 theoptions
passed asthis.options
:or use some finer ways as described by @Brave Dave.
将选项参数添加到
initialize
:然后在创建视图时传入一些选项:
有关详细信息:http ://backbonejs.org/#View-constructor
Add an options argument to
initialize
:And then pass in some options when you create your view:
For more information: http://backbonejs.org/#View-constructor
从主干 1.1.0 开始,
options
参数不再自动附加到视图(请参阅问题 2458 供讨论)。您现在需要手动附加每个视图的选项:或者您可以使用这个迷你插件来自动附加白名单选项,像这样:
As of backbone 1.1.0, the
options
argument is no longer attached automatically to the view (see issue 2458 for discussion). You now need to attach the options of each view manually:Alternatively you can use this mini plugin to auto-attach white-listed options, like so:
从其他位置传递
添加一个选项参数来初始化,以查看您正在获取传递的变量,
以获取值
使用 -
pass from other location
Add an options argument to initialize in view you are getting that passed variable,
to get the value
use -
使用 this.options 在视图中检索参数
工作示例:http://jsfiddle.net /Cpn3g/1771/
Use this.options to retrieve argumentr in view
Working Example: http://jsfiddle.net/Cpn3g/1771/