0

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
},
erosman
  • 7,094
  • 7
  • 27
  • 46

1 Answers1

3

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

Community
  • 1
  • 1
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • Beat me to it. :p But I guess it's *bound* – nmaier Jun 28 '14 at 20:37
  • Sucker!!!! Finally!! I woke up from my sleep this morning when and by chance logged in via mobile and saw the "how to intercept download", it was just posted 14min ago, so I even called the topic by making a comment that Im going to answer in a sec. I went to my desktop started it up, and by then you frikin posted! :'( And it was a MDN copy! :( – Noitidart Jun 28 '14 at 20:39
  • "But I guess it's bound" give me a break you're just pissed i beat you for once :nahnah: I used bind'ed so noobs more noob than me understand :P – Noitidart Jun 28 '14 at 20:41
  • I'm referring to your variable name and just tried to help :p – nmaier Jun 28 '14 at 20:41
  • Aw god ok yah you got me. i made up up that excuse on the "binded" easy for noobs to understand thing. :P edited as you got rid of fat arrows! :P – Noitidart Jun 28 '14 at 20:44
  • Thank you Noitidart & @nmaier I spent 2 hours wondering what was wrong. I didnt know that `bind` creates a new function. I have used it with a few eventlisteners. I need to rethink my approach. :) – erosman Jun 29 '14 at 03:56