问题
I have noticed that after disabling a bootstrapped add-on, the removeEventListener
does not seem to remove the listener and I can't work out the reason.
let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
contextMenu.addEventListener('popupshowing',
this.contextPopupShowing.bind(this), false);
// added this to make sure they refer to the same function
console.log(this.contextPopupShowing.toString());
}
And then on disabling the addon
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing',
this.contextPopupShowing.bind(this), false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem);
Finally ...
contextPopupShowing: function() {
// logs even after removeEventListener
console.log('contextPopupShowing called');
// more code
},
回答1:
Because it's bind
'ed. What you gotta do is this:
When adding:
let contextMenu = window.document.getElementById('contentAreaContextMenu');
if (contextMenu) {
this.contextPopupShowingBound = this.contextPopupShowing.bind(this);
contextMenu.addEventListener('popupshowing',
this.contextPopupShowingBinded, false);
// added this to make sure they refer to the same function
console.log(this.contextPopupShowing.toString());
}
And then on disabling the addon
console.log(this.contextPopupShowing.toString()); // same as above
this.contextMenu.removeEventListener('popupshowing',
this.contextPopupShowingBound, false);
// just showing that 'this' is working
this.contextMenu.removeChild(this.menuitem);
Finally ...
contextPopupShowing: function() {
// logs even after removeEventListener
console.log('contextPopupShowing called');
// more code
},
You cannot use bind
in the removeEventListener
, I'm pretty sure.
See this excellent topic on the subject: Removing event listener which was added with bind
来源:https://stackoverflow.com/questions/24470716/removeeventlistener-in-bootstrapped-addon-not-working-when-addon-disabled