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.
Adds a new event handler for a particular type of event.
Parameter | Type | Description |
---|---|---|
type | string | The name of the event to listen for. |
handler | Function | The function to call when the event occurs. |
function customEventHandler(event) {
console.log('test');
});
EventTarget.on('custom-event', customEventHandler);
EventTarget.fire('custom-event'); // Triggers an output of "test"
Removes an event handler from a given event.
Parameter | Type | Description |
---|---|---|
type | string | The name of the event to remove from. |
handler | Function | The function to remove as a handler. |
function customEventHandler(event) {
console.log('test');
});
EventTarget.on('custom-event', customEventHandler);
EventTarget.off('custom-event', customEventHandler);
EventTarget.fire('custom-event'); // Triggers nothing
Fires an event with the given name and data.
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. |
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
});