Skip to content

Instantly share code, notes, and snippets.

@scottcorgan
Last active August 29, 2015 13:55
Show Gist options
  • Save scottcorgan/8713870 to your computer and use it in GitHub Desktop.
Save scottcorgan/8713870 to your computer and use it in GitHub Desktop.
var bind = function (selector, event) {
return binder(selector, event);
};
var binder = function (selector, event) {
var listeners = [];
document.querySelector(selector)
.addEventListener(event, function (e) {
listeners.forEach(function (listener) {
listener.call(this, e)
};
});
function addListener () {
listeners.push(listener);
};
addListener.selector = selector;
addListener.event = event;
return addListener;
};
// This is basically the same idea behind
// using promises for events, except
// the binding is partially applied so that
// we can send any amount of callbacks to
// the event listener.
var clicked = bind('.element', 'click');
clicked(function (e) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment