Lithium

API Docs for: 0.8.0
Show:

Li.Publisher Class

Module: pubsub

Base class for Publishers.
This class helps you to achieve the Observer (also known as publisher-subscriber) design pattern.

Your class prototype may have a property named 'eventTypes' which is a list (i.e array) of eventTypes that the class as a publisher would/can trigger.

Methods

relayEvents

(
  • publisher
  • eventTypes
)

This function listens to a given publisher on the given event types, and refires the events from itself (scope of the event fired would be this object). This useful for chaining events.

Parameters:

  • publisher Li.Publisher

    A publisher instance.

  • eventTypes Array | Null

    Event types to listen on. If eventType is null, it listens to all events of the publisher.

subscribe

(
  • object
  • handler
  • scope
)

Adds a listener. If no parameters are specified, then this would re-enable events that were switched off by publisher.unsubscribe();

Parameters:

  • object String | Object

    The event type that you want to listen to as string. Or an object with event types and handlers as key-value pairs (with event type as the keys). You can also subscribe for an event that has not yet been registered as an event. Hence the order of registeration is not a concern.

  • handler Function

    Function that gets notfied when a event of 'eventType' gets fired. This param is used only when eventType is a string.

  • scope Object

    The context in which the function should be called.

Returns:

A UUID which can be used to remove the event when required.

trigger

(
  • eventType
  • ...
)

Call all events listeners for the given event name.

Parameters:

  • eventType String
  • ... Any

    n number of arguments. These shall be directly passed onto the event listeners.

unsubscribe

(
  • uuidORfunc
)
Boolean

Remove an event listener. If no parameters are specified, then all events are switched off till you call publisher.subscribe().

Parameters:

  • uuidORfunc String | Function

    Can be the event listener as a Function object, OR the UUID returned by 'subscribe' function can also be used.

Returns:

Boolean: Returns true if listener was successfully removed.