HexagonJS
Edit Page
Event Emitter
An interface for emitting and subscribing to events.
Examples
var eventEmitter = new hx.EventEmitter;

eventEmitter.on('my-event', function(d){ console.log(d) });

// this will result in the object {x: 0, y: 123} being logged to the console
eventEmitter.emit('my-event', {
  x: 0,
  y: 123
});
Api
Prototypes
hx.EventEmitter
A class that can be extended or used standalone to provide a way of emitting events to named registered callbacks. This is generally used internally to give objects the .on method for listening for events, but can be used as a standalone object if desired.
Constructors
hx.EventEmitter
Creates a new EventEmitter instance.
Methods
Emits an event to all listeners registered under the name supplied.
Arguments
nameString
The event type to emit.
data
Any data that should be sent along with the event
Returns
This event emitter
Checks if there are any callbacks registered for an event type.
Arguments
nameString
The event type to check.
Returns
True if the event type has some callbacks registered.
offnameStringnamespaceStringcallbackFunctionEventEmitter
De-register a callback that was registered under the given name. If called with no arguments, all handlers will be removed from the event emitter for all names and namespaces.
Arguments
nameString
The event type this callback was registered under.
namespaceString
The namespace to remove handlers from. If not specified handlers will be removed from all namespaces. To remove handlers from the default handler supply "default" for the namespace name.
callback
The callback function to remove.
Returns
This event emitter
onnameStringnamespaceStringcallbackFunctionEventEmitter
Register a callback that will be called whenever an event is emitted under the given name
Arguments
nameString
The event type to listen out for
namespaceString
The namespace to register the handler under. Namespaces isolate groups of handlers. Handlers can be removed by namespace without affecting handlers in other namespaces. The value "default" is reserved and may not be used as a namespace name.
callbackdataEventData
The function to call when the event occurs
Arguments
dataEventData
The data sent along with the event
Returns
This event emitter
pipeeventEmitterEventEmitterprefixStringfilterArray[String]EventEmitter
Pipes events from this event emitter into another. This makes it possible to chain multiple event emitters together if wanted.
Arguments
eventEmitterEventEmitter
The event emitter to send events to.
prefixString
A string to append to the names of events that get piped through
var ee1 = new hx.EventEmitter;
var ee2 = new hx.EventEmitter;

// pipe events from ee1 into ee2 with prefix `my-prefix`
ee1.pipe(ee2, 'my-prefix');

// listen for events from ee1 being piped through
ee2.on('my-prefix.click', function(){
  console.log('got here');
});

// will cause 'got here' to be logged
ee1.emit('click');
filterArray[String]
An array of event names to pipe through
var ee1 = new hx.EventEmitter;
var ee2 = new hx.EventEmitter;

// only pipe the 'click' and 'move' events from ee1 into ee2. ignore the rest
ee1.pipe(ee2, undefined, ['click', 'move']);
Returns
This event emitter
suppressednameStringsuppressedBooleanEventEmitter
Sets whether events of a particular name should be suppressed (not emitted).
Arguments
nameString
The event name to change the suppression state of.
suppressedBoolean
True to prevent any events of the given name from being emitted, false to re-enable emission (which is the default behaviour).
Returns
This EventEmitter
suppressednameStringBoolean
Gets whether events of a particular name are being suppressed.
Arguments
nameString
The event name to check the suppression state of.
Returns
True if the events of the given name are being suppressed, false if not.