name
- a string value representing the name of eventfn
- action which will be called when event will be triggeredctx
- object will be context of triggered action
Example:
instance.on('foo', function (payload) {
console.log(payload, this);
});
instance.on('foo', function (payload) {
console.log(payload, this);
}, instance);
Special behavior:
- when
name
=all
(or*
) register handler will be fired on any event
instance.on('all', function (name, payload) {
// will be fired, when emit any of event type
// - name - event name, in this example will be equal: "something"
// - payload - data which are sended, in this example will be equal: { foo: 1 }
});
instance.emit('something', { foo: 1 });
The same as on
but, after triggered event, destroy all listeners
Example:
instance.once('foo', function (payload) {
console.log(payload, this);
});
instance.once('foo', function (payload) {
console.log(payload, this);
}, instance);
name
- a string value representing the name of eventfn
- action which will be removed from listeners
Example:
instance.off();
instance.off('foo');
instance.off('foo', fooHandler);
name
- a string value representing the name of eventpayload
- will be passed as first argument in called actions
Example:
instance.emit('foo');
instance.emit('foo', { name: 'bar' });
For compatibility with any other APIs I was added some aliases:
on
=>addEventListener
,addListener
,bind
off
=>removeEventListener
,removeListener
,unbind
emit
=>dispatchEventListener
,dispatchListener
,trigger