如果我在对象上有一个事件聚合器,
eventAggregator: _.extend({}, Backbone.Events),对于模式视图,我基本上让模式视图的演示者监听模式视图的事件。
this.eventAggregator.on('modal:close', function () {
console.log('do some clean up');
});当模式视图消失时,我调用
this.unbind(); 这会删除所有的事件吗?或者我需要做像这样的事情
this.eventAggregator.off('modal:close');提前感谢!
发布于 2013-05-01 11:49:59
o.unbind()对o中的对象一无所知,它只知道使用o.on (又称o.bind)绑定到o的内容。所以不,this.unbind()不会对绑定到this.eventAggregator的东西做任何事情,你必须:
this.eventAggregator.unbind();
this.unbind();来清空这两个名单。
发布于 2013-05-01 15:53:46
在最近的主干中有listenTo (http://backbonejs.org/#Events-listenTo),因此您可以通过以下方式订阅事件:
this.listenTo(this.eventAggregator, 'modal:close', function () {
console.log('do some clean up');
})
this.listenTo(this.model, 'change', this.doSomeStuff);然后,如果您需要取消订阅,只需调用以下命令:
this.stopListening();或者用view.remove (http://backbonejs.org/#View-remove)移除你的视图,它会在幕后调用view.stopListening。
发布于 2013-05-03 18:32:59
如果你想使用on/off来绑定/解绑事件,你可能可以这样做。
//declare some global object to get the scope.
window.eventAggregator = _.extend({}, Backbone.Events);现在在backbone视图中:
var SomeView = Backbone.View.extend({
el : 'body',
initialize : function() {
//pass the context as this object
eventAggregator.on('modal:close', function(data) {
console.log('Clean up');
eventAggregator.off('modal:close', null, this);
}, this);
}
});eventAggregator.off('modal:close', null, this);它将帮助你关闭绑定到'this‘当前视图对象的事件。
https://stackoverflow.com/questions/16312303
复制相似问题