API Docs for: 1.0.1

Grape.EventEmitter Class

An object which cna emit events, others can subscribe to it, and we can use the event keyword to make easier the subscription when extending this class.

Item Index

Methods

Events

Methods

decompose

(
  • method
  • target
  • name
)
static

A helper function for decomposing nested event handlers. When the method is a function, it is just added to the target. If the method is an object, all of it's element are added with name.key.

Parameters:

  • method Function | Object

    The method or methods

  • target Object

    The target the methods are added to.

  • name String

    method name or nested method prefix

Example:

 var target = {};
 Grape.EventEmitter.decompose(function(){},target,'name1');
 Grape.EventEmitter.decompose({
     x:function(){}
 },target,'name2');

 //target will be {'name1':function(){},'name2.x':function(){}}

emit

(
  • event
  • payload
)

Emits an event to the instance: calls all event listeners subscribed to this event, or the 'any' event.

Parameters:

  • event String

    Event

  • payload

    An object passed as parameter to all event listeners.

off

(
  • event
  • listener
)

Unsubscribes from an event.

Parameters:

  • event String

    Event

  • listener Function

    Event listener

on

(
  • event
  • listener
)

Subscribes to an event. The event handler will be called with this instance as context.

Parameters:

  • event String

    The event to subscribe

  • listener Function

    Event listener

Events

any

Emitted when any event is emitted. The parameters are the event and the payload.