Simple events for simple objects. Three methods to rule'em all: .on
, .off
, .trigger
. Lots of fun.
npm install --save got-events
var gotEvents = require('gotEvents');
var needEvents = {};
// The magic trick
gotEvents.extend(needEvents);
/* Simple events */
needEvents.on('hello', function() {
return console.log('world!');
});
needEvents.trigger('hello');
// Console says 'world!'
needEvents.off('hello');
needEvents.trigger('hello');
// Nothing happens
/* More events */
needEvents.on('hello', 'world', function() {
return console.log('world!');
});
needEvents.on('hello', 'planet', function() {
return console.log('planet!');
});
needEvents.trigger('hello');
// Console says 'world!' and then 'planet!'
needEvents.off('hello', 'world');
needEvents.trigger('hello');
// Console says 'planet!'
Three options:
- Function:
var evHandler = require('got-events')();
- Extend object:
require('got-events').extend(simpleObject);
Optionally preserving old attributes:
require('got-events').extend(simpleObject, true);
- Singleton:
var evHandler = require('got-events').singleton;
-
.on(eventName, callback)
Adds a callback to an event.
eventName
: String or Array of strings.callback
: Function.
-
.on(eventName, callbackId, callback)
Adds a callback to an event, identified.
eventName
: String or Array of strings.callbackId
: String.callback
: Function.
-
.off(eventName)
Clears the event's callback list.
eventName
: String or Array of strings.
-
.off(eventName, callback)
Removes a callback from an event.
eventName
: String or Array of strings.callback
: Function.
-
.off(eventName, callbackId)
Removes an identified callback from an event.
eventName
: String or Array of strings.callbackId
: String or Array of strings.
-
.trigger(eventName, data)
Triggers an event with
err = null
.eventName
: String or Array of strings.data
: Anything.
-
.trigger(eventName, err, data...)
Triggers an event.
eventName
: String or Array of strings.err
: Error or Boolean.data...
: Anything. Every argument from here will be passed to callback.
- Wildcard event
'*'
fired on every trigger.