【发布时间】:2013-09-27 10:47:37
【问题描述】:
我创建了一个继承自 Backbone.View 的类,它定义了一些 DOM 事件:
var MyView = Backbone.View.extend({
el: '#myview',
events: {
'click .somebutton': 'somefunction',
'click .otherbutton': 'otherfunction'
},
somefunction: function(){ console.log('somefunction!'); },
otherfunction: function(){ console.log('otherfunction!'); }
});
当实例化这个视图 (new MyView();) 时,一切似乎都井然有序,只要点击元素就会触发我的回调。
但是,如果我像这样实例化我的视图:
new MyView({
events: {
'click .thirdbutton': function(){
console.log('thirdfunction');
}
}
});
我所有现有的班级事件都被这个单一的事件所覆盖。将我的仅实例事件与现有类事件合并的正确方法是什么?在我的示例中,我希望所有 3 个事件在我的实例中都处于活动状态。
【问题讨论】:
标签: javascript events backbone.js prototypal-inheritance