prominent
v0.2.0
Published
Partially applied DOM event listeners
Downloads
24
Readme
prominent
Partially applied DOM event listeners. Makes awesome use of partial application.
Install
NPM
npm install prominent --save
Bower
bower install prominent --save
Usage
Browserify
var listen = require('prominent');
var clicked = listen('.selector', 'click');
function handler1 (e) {
// Do stuff
}
function handler2 (e) {
// Do stuff
}
clicked(handler1);
clicked(handler2);
clicked.off(); // No more listeners
Standalone
<script src="/bower_components/prominent/dist/prominent.js"></script>
var listen = window.prominent;
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 listner
prominent(selector, event)
selector
- a valid css selector or DOM node objectevent
- a valid DOM event (click, submit, etc.)
var listen = require('prominent');
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('prominent');
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 listeners
Run Tests
Requires Phantomjs is installed
npm install
npm test
Credits
Huge thanks to the following for fix 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 implementation.