Event helpers toolkit.
It uses target events, if possible, and then falls back to own safe implementation.
It handles multiple events on(el, 'click touchstart', cb)
and namespaces on(el, 'click.x', cb); off(el, 'click.x');
.
It may bind events to anything: plain objects, elements, jQuery objects, Backbone models, color, webWorkers etc.
It is a useful tiny replacement for jquery events.
var on = require('emmy/on');
var once = require('emmy/once');
var off = require('emmy/off');
var emit = require('emmy/emit');
on(target, 'evt', function(){});
emit(target, 'evt', data1, data2);
off(target, 'evt');
//typical use-case
once(webWorker, 'message', function(){...});
You might need to polyfill Element.contains
, Element.closest
and Array.some
for old browsers:
https://cdn.polyfill.io/v1/polyfill.js?features=default,Node.prototype.contains,Element.prototype.closest,Array.prototype.some,
Bind an event handler to a target. event
may contain a class suffix: click.my-element
.
Bind all events defined in object.
Bind single-shot event handler to a target.
Unbind event handler from a target. If calback isn’t passed - unbind all callbacks for an event. If no event passed - unbind all known callbacks for any events.
Also you can pass only class suffix to unbind all events for a class: off(target, '.my-element')
.
Emit an event on a target, passing dataN
. If target is an element then data1
is e.details
, data2
is bubbles
. So to fire bubbling event, call emit(element, 'click', null, true)
.
Bind an event handler which triggers a delay
later than actual event occures.
Bind an event handler which won’t be called more often than an interval
.
Bind an event handler catching bubbling events from target’s descendants. selector
can be a string, an element or a list of elements/selectors.
Bind an event handler catching events from target’s descendants ignoring ones that match selector.
Bind an event handler which triggers only if e.which
or e.keyCode
is one from the defined keylist
. Any keyname can be declared instead of a code.
Get list of listeners registered for an event.
- enot (event notation system) — an easy wrapper for emmy with humanized event notation.
- emmitt — universal event wrapper.
- event — unified DOM event binder.