Event Target

Definition of a custom event type. This is used as a utility throughout the framework whenever custom events are used. It is intended to be inherited from, either through the prototype or via mixin.

Message names should be all lower-case with no dashes or underscores, however, it is not enforced.


on

Description

Adds a new event handler for a particular type of event.

Usage

Parameter Type Description
type string The name of the event to listen for.
handler Function The function to call when the event occurs.

Example

function customEventHandler(event) {
    console.log('test');
});

EventTarget.on('custom-event', customEventHandler);

EventTarget.fire('custom-event'); // Triggers an output of "test"

off

Description

Removes an event handler from a given event.

Usage

Parameter Type Description
type string The name of the event to remove from.
handler Function The function to remove as a handler.

Example

function customEventHandler(event) {
    console.log('test');
});

EventTarget.on('custom-event', customEventHandler);
EventTarget.off('custom-event', customEventHandler);

EventTarget.fire('custom-event'); // Triggers nothing

fire

Description

Fires an event with the given name and data.

Usage

Parameter Type Description
type string The type of event to fire.
data Function An object with properties that should end up on the event object for the given event.

Example

function searchCompleteHandler(event, data) {
    console.log('Found ' + data.numResults + ' results.');
});

EventTarget.on('searchcomplete', searchCompleteHandler);

// Triggers an output of "Found 100 results."
EventTarget.fire('searchcomplete', {
    numResults: 100
});