removeEventListener in bootstrapped addon not working when addon disabled

狂风中的少年 提交于 2019-12-12 01:58:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!