Skip to content

Instantly share code, notes, and snippets.

@gabskoro
Forked from stringparser/eventListenerList.js
Created May 29, 2016 13:48
Show Gist options
  • Save gabskoro/6861d5ee1e581837ae729e000a51cbc8 to your computer and use it in GitHub Desktop.
Save gabskoro/6861d5ee1e581837ae729e000a51cbc8 to your computer and use it in GitHub Desktop.
`eventListenerList`
;[Element].forEach(function(self){
self.prototype.eventListenerList = {};
self.prototype._addEventListener = self.prototype.addEventListener;
self.prototype.addEventListener = function(type, handle, useCapture) {
useCapture = useCapture === void 0 ? false : useCapture;
var node = this;
node._addEventListener(type, handle, useCapture);
if(!node.eventListenerList[type]){
node.eventListenerList[type] = [];
}
node.eventListenerList[type].push({
type : type,
handle : handle,
useCapture : useCapture,
remove : function(){
node.removeEventListener(
this.type, this.handle, this.useCapture
);
return node.eventListenerList[type];
}
});
};
self.prototype._removeEventListener = self.prototype.removeEventListener;
self.prototype.removeEventListener = function(type, handle, useCapture){
var node = this;
if(!node.eventListenerList[type])
return ;
node._removeEventListener(type, handle, useCapture);
node.eventListenerList[type] = node.eventListenerList[type].filter(
function(listener){
return listener.handle.toString() !== handle.toString();
}
)
if(node.eventListenerList[type].length === 0){
delete node.eventListenerList[type];
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment