Javascript 删除主干事件的侦听器
我想知道如何将侦听器删除到Javascript 删除主干事件的侦听器,javascript,backbone.js,backbone-events,Javascript,Backbone.js,Backbone Events,我想知道如何将侦听器删除到主干.history.on().off()对我不起作用 Backbone.history.on('all', function() { doStuff(); }); off正常工作,这里有一个路由器证明了这一点: var MyRouter = Backbone.Router.extend({ routes: { 'off': 'offAll', '*actions': 'index', }, initia
主干.history.on()
.off()
对我不起作用
Backbone.history.on('all', function() {
doStuff();
});
off
正常工作,这里有一个路由器
证明了这一点:
var MyRouter = Backbone.Router.extend({
routes: {
'off': 'offAll',
'*actions': 'index',
},
initialize: function(opt) {
Backbone.history.on('all', this.all);
},
index: function() {
console.log('route');
},
offAll: function() {
console.log('offAll');
// remove only this one listener
Backbone.history.off('all', this.all);
},
all: function(){
console.log('all test');
}
});
导航到除#/off
以外的任何位置时,将显示:
route
all test
然后,导航到#/off
将显示:
offAll
然后,all test
永远不会出现
主干的事件功能
如何确保只删除一个匿名函数?@Jon我编辑了我的答案,以包含所需的行为。
// Removes just the `onChange` callback.
object.off("change", onChange);
// Removes all "change" callbacks.
object.off("change");
// Removes the `onChange` callback for all events.
object.off(null, onChange);
// Removes all callbacks for `context` for all events.
object.off(null, null, context);
// Removes all callbacks on `object`.
object.off();