0.2.0 • Published 12 years ago
listenify v0.2.0
listenify
Partially applied DOM event listeners. Makes awesome use of partial application.
Install
NPM
npm install listenify --saveBower
bower install listenify --saveUsage
Browserify
var listen = require('listenify');
var clicked = listen('.selector', 'click');
function handler1 (e) {
// Do stuff
}
function handler2 (e) {
// Do stuff
}
clicked(handler1);
clicked(handler2);
clicked.off(); // No more listenersStandalone
<script src="/bower_components/listenify/dist/listenify.js"></script>var listen = window.listenify;
var clicked = window.listen('.selector', 'click');
function handler1 (e) {
// Do stuff
}
function handler2 (e) {
// Do stuff
}
clicked(handler1);
clicked(handler2);
clicked.off(handler1); // Removed this listnerlistenify(selector, event)
selector- a valid css selector or DOM node objectevent- a valid DOM event (click, submit, etc.)
var listen = require('listenify');
var clicked = listen('.selector', 'click');Returns a partially applied function, which also has a number of properties and methods on it:
off([listener])- Removes event listeners. If no arguments are passed, it removes all listeners and event listeners. If you pass sthe listener function as an argument, that listener will be removed, but the rest will remain.element- the DOM node associated with this listenertype- the type of event passed in (i.e, click, submit, etc);listeners- an array of all the listeners on this bound element.
var listen = require('listenify');
var clicked = listen('.selector', 'click');
console.log(clicked.element === document.querySelector('.selector')); // true
console.log(clicked.type === 'click'); // true
clicked.listeners.forEach(function (listener) {
// Do Stuff
});
clicked.off(); // Removed all listenersRun Tests
Requires Phantomjs is installed
npm install
npm testCredits
Huge thanks to the following for fixing my code!
- Matthias Le Brun - Helped me through fixing my borked promises implementation. Ultimately leading to using this with partial application.
- Ben Lower - Helped me avoid a wrong promises definition.
0.2.0
12 years ago
