eventDispatcher
JavaScript events for custom objects
Install
npm
$ npm install gr-event-dispatcher
API
- eventDispatcher
- ~addEventListener(type, listener) ⇒
Object
- ~hasEventListener(type, listener) ⇒
Boolean
- ~removeEventListener(type, listener) ⇒
Object
- ~dispatchEvent(event) ⇒
Object
- ~apply(object) ⇒
Object
- ~addEventListener(type, listener) ⇒
Object
eventDispatcher~addEventListener(type, listener) ⇒ Registers the specified listener
on the event target it's called on.
Kind: inner method of eventDispatcher
Returns: Object
- The eventDispatcher
object.
Throws:
TypeError
If the object thateventDispatcher
is applied to is not extensible.
Param | Type | Description |
---|---|---|
type | String |
A string representing the event type to listen for. |
listener | function |
A function to be executed when an event of the specified type occurs. |
Boolean
eventDispatcher~hasEventListener(type, listener) ⇒ Checks if the target object has a listener
registered on for specific event type
.
Kind: inner method of eventDispatcher
Returns: Boolean
- True if target object has listener
registered for specific event type
; otherwise false.
Param | Type | Description |
---|---|---|
type | String |
A string representing the event type. |
listener | function |
The event listener to check if registered for the specified event type . |
Object
eventDispatcher~removeEventListener(type, listener) ⇒ Removes the previously registered event listener
from the event target.
Kind: inner method of eventDispatcher
Returns: Object
- The eventDispatcher
object.
Param | Type | Description |
---|---|---|
type | String |
A string representing the event type to remove. |
listener | function |
The event listener function to remove from the event target. |
Object
eventDispatcher~dispatchEvent(event) ⇒ Dispatches an event at the specified event target.
Kind: inner method of eventDispatcher
Returns: Object
- The eventDispatcher
object.
Param | Type | Description |
---|---|---|
event | Object |
The event object to be dispatched. |
Object
eventDispatcher~apply(object) ⇒ Applies the eventDispatcher
prototype methods to the event target.
Kind: inner method of eventDispatcher
Returns: Object
- The eventDispatcher
object.
Throws:
TypeError
If the object thateventDispatcher
is applied to is not extensible.
Param | Type | Description |
---|---|---|
object | Object |
The event target object. |
Example
var car = { // Dispatch `start` event. this; afterStartCallback && ; } { // Dispatch `stop` event. this; }; { console;} { console;} // Apply `eventDispatcher` methods to the prototype of the event target.eventDispatcher; // Register event listeners to event target.car ; // `start` event fires first, then `afterStart` and finally// after two seconds `stop` events fires. carstart { // Dispatch `afterStart` event. self;}; console; // -> trueconsole; // -> trueconsole; // -> falseconsole; // -> true ;