引导插件中的removeEventListener在禁用插件时不起作用 [英] removeEventListener in bootstrapped addon not working when addon disabled
本文介绍了引导插件中的removeEventListener在禁用插件时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我注意到在禁用自引导的附加组件之后, removeEventListener
似乎没有移除监听器,我找不出原因。
let contextMenu = window.document.getElementById('contentAreaContextMenu');
if(contextMenu){
contextMenu.addEventListener('popupshowing',
this.contextPopupShowing.bind(this),false);
//添加这个以确保它们引用相同的函数
console.log(this.contextPopupShowing.toString());
}
然后禁用插件
console.log(this.contextPopupShowing.toString()); //同上
this.contextMenu.removeEventListener('popupshowing',
this.contextPopupShowing.bind(this),false);
//只显示'this'正在工作
this.contextMenu.removeChild(this.menuitem);
最后...
contextPopupShowing:function(){
//甚至在removeEventListener之后记录
console.log('contextPopupShowing called');
// more code
},
解决方案
因为它是 bind
'ed。
添加时:
let contextMenu = window.document.getElementById('contentAreaContextMenu');
if(contextMenu){
this.contextPopupShowingBound = this.contextPopupShowing.bind(this);
contextMenu.addEventListener('popupshowing',
this.contextPopupShowingBinded,false);
//添加这个以确保它们引用相同的函数
console.log(this.contextPopupShowing.toString());
}
然后禁用插件
console.log(this.contextPopupShowing.toString()); //同上
this.contextMenu.removeEventListener('popupshowing',
this.contextPopupShowingBound,false);
//只显示'this'正在工作
this.contextMenu.removeChild(this.menuitem);
最后...
contextPopupShowing:function(){
//甚至在removeEventListener之后记录
console.log('contextPopupShowing called');
//更多代码
},
您不能使用 bind
在 removeEventListener
中,我确定。
关于这个问题的精彩话题:删除已添加绑定的事件监听器
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
},
解决方案
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
这篇关于引导插件中的removeEventListener在禁用插件时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文