Skip to content

Instantly share code, notes, and snippets.

@yesasha
Last active August 29, 2015 14:17
Show Gist options
  • Save yesasha/5117222b0ce128e130a9 to your computer and use it in GitHub Desktop.
Save yesasha/5117222b0ce128e130a9 to your computer and use it in GitHub Desktop.
Attach and then remove event listeners
// Helps to attach and then remove event listeners
// without having to worry about saving the callback and other arguments,
// like the original javascript functions require.
// Also allows passing "this" and any amount of arguments
// to the callback function
//
// Copyright (c) 2015 Aleksandr Efremov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Usage:
// var myListener = new EventListener();// Creates an instance
// myListener.listen(element /*Event Target*/, type, listener, useCapture, thisArg /*, argumentToPass1, argumentToPass2, etc. */);// Starts listener
// myListener.remove();// Removes previously set listener
//
// I'm using underscores before property names, to make GCC to compress them
(function () {
// Using this as a namespace proxy
this['EventListener'] = EventListener;
function EventListener () {}
EventListener.prototype['listen'] = function (element, type, listener, useCapture, thisArg /*, argumentToPass1, argumentToPass2, etc. */) {
// Removing the old listener if exists before setting new parameters
this['remove']();
// If not provided, then set to window.
thisArg = thisArg || window;
var args = Array.prototype.slice.call(arguments, 4);// (!)
// Actual listener
function newListener (event) {
args[0] = event;// event instead of thisArg
listener.apply(thisArg, args);
}
// Store the new parameters, to be able to remove the listener later
this._element = element;
this._type = type;
this._listener = newListener;
this._useCapture = useCapture;
// Setting the listener
element.addEventListener(type, newListener, useCapture);
};
EventListener.prototype['remove'] = function () {
// Remove the listener if it was set(assuming, that it was set if the _element property exists)
if (this._element) {
this._element.removeEventListener(this._type, this._listener, this._useCapture || false);// Let useCapture be false, if undefined, for broader compatibility
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment