我有一系列按钮,单击这些按钮会在按钮正下方显示一个弹出菜单。我想将按钮的位置传递给 View 。我该怎么做?
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?
}
});
最佳答案
构造MenuView时只需要传递额外的参数即可。无需添加initialize
函数。
new MenuView({
collection: itemColl,
position: this.getPosition()
})
然后,在 MenuView
中,您可以使用 this.options.position
。
更新:作为@mu is too short states ,从 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.
因此在您的initialize
方法中,您可以将传递的options
保存为this.options
:
initialize: function(options) {
this.options = options;
_.bindAll(this, 'render');
},
或者使用一些更好的方式作为 described by @Brave Dave .
关于javascript - 如何将参数传递给 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7803138/