hear v1.1.1
hear
Listen to any event emitter with a single API.
.on, .addEventListener, .subscribe, ... Why are there so many method names ?
hear is an "universal binder" that allows you to use one method name with the different event emitters, whether it is a DOM node, Node EventEmitter, mediator...
You can also pass a context and hear will handle this for you (no bind leak).
Supported emitter types
- DOM nodes
- jQuery events
- Angular & Vue internal emitters
- Google Maps events
Basically all objects with
on/offmethods. See events.js for API support list.
Installation & usage
npm i --save hearjs
var hear = require('hearjs');
var emitter = new EventEmitter();
var mediator = new Mediator();
function MyType() {
hear($node, 'click', onEvent, this); // document.querySelector('.node');
hear(emitter, 'onClick', onEvent, this); // EventEmitter
hear(mediator, 'onClick', onEvent, this); // Mediator
}
MyType.prototype.onEvent = function() {
// ...
};API
hear.on(emitter, eventName, fn, context)listeneventNameon theemitter.hear.once(emitter, eventName, fn, context)like.onbut is unbound after first call.hear.off(emitter, eventName, fn, context)Unbind an event listener. If supported by the passedemitter:- if no
fnis passed, all theeventNamelisteners will be unbound - if no
eventNameis passed, theemitterwill be totally unbound
- if no
Contributing
Checkout from dev, merge back against dev.
Add relevant test cases.
4 spaces, semicolon.
Todo
- support
offmethod without event/fn argument - add test with gmaps, angular/vue, jquery