I am wondering how would I remove a listener to Backbone.history.on()
? .off()
did not work for me.
Backbone.history.on('all', function() {
doStuff();
});
off
works as it should be, and here's a Router
which proves it:
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');
}
});
Navigating to anything other than #/off
will display :
route
all test
Then, navigating to #/off
will display:
offAll
Then, all test
never shows up.
.off
function// 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();