StructureJS

0.15.2

A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.

EventBroker Class

Module: event
Parent Module: StructureJS

EventBroker is a simple publish and subscribe static class that you can use to fire and receive notifications. Loosely coupled event handling, the subscriber does not know the publisher. Both of them only need to know the event type.

Index

Show:

Properties

Properties

_eventDispatcher EventDispatcher private static

A reference to the EventDispatcher object.

_waitForList Array, callback:Function, callbackScope:any, events:Array, once:boolean> private static

A list of wait for objects.

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

_dispatchWaitFor () private static

Helper method to dispatch events on the waitForObject objects.

addEventListener
(
  • type
  • callback
  • scope
  • [priority=0]
)
public static

Registers an event listener object with an EventBroker object so that the listener receives notification of an event.

Parameters:

  • type String

    The type of event.

  • callback Function

    The listener function that processes the event. The callback function will receive a BaseEvent object or custom event that extends the BaseEvent class.

  • scope Any

    The scope of the callback function.

  • [priority=0] Int optional

    Influences the order in which the listeners are called. Listeners with lower priorities are called after ones with higher priorities.

Example:

EventBroker.addEventListener('change', this._handlerMethod, this);
// Example of using a constant event type.
EventBroker.addEventListener(BaseEvent.CHANGE, this._handlerMethod, this);

// The event passed to the method will always be a BaseEvent object.
_handlerMethod(event) {
     console.log(event.data);
}

addEventListenerOnce
(
  • type
  • callback
  • scope
  • [priority=0]
)
public static

Registers an event listener object once with an EventDispatcher object so the listener will receive the notification of an event.

Parameters:

  • type String

    The type of event.

  • callback Function

    The listener function that processes the event. The callback function will receive a BaseEvent object or custom event that extends the BaseEvent class.

  • scope Any

    The scope of the callback function.

  • [priority=0] Int optional

    Influences the order in which the listeners are called. Listeners with lower priorities are called after ones with higher priorities.

Example:

EventBroker.addEventListenerOnce('change', this._handlerMethod, this);
// Example of using a constant event type.
EventBroker.addEventListenerOnce(BaseEvent.CHANGE, this._handlerMethod, this);

// The event passed to the method will always be a BaseEvent object.
_handlerMethod(event) {
     console.log(event.data);
}

dispatchEvent
(
  • event
  • [data=null]
  • [scope=null]
)
public static

Dispatches an event within the EventBroker object.

Parameters:

  • event String | BaseEvent

    The Event object or event type string you want to dispatch.

  • [data=null] Any optional

    The optional data you want to send with the event. Do not use this parameter if you are passing in a BaseEvent.

  • [scope=null] Any optional

    You can optionally pass in the target of the object that dispatched the global event. Since EventBroker

Example:

 EventBroker.dispatchEvent('change');

 // Example: Sending data with the event.
 EventBroker.dispatchEvent('change', {some: 'data'});

 // Example: Sending a BaseEvent or custom event object.
 let event = new BaseEvent(BaseEvent.CHANGE);
 event.data = {some: 'data'};
 EventBroker.dispatchEvent(event);

getEventListeners () Array public static

Returns and array of all current event types and there current listeners.

Returns:

Array:

Example:

 const listenerList = this.getEventListeners();

hasEventListener
(
  • type
  • callback
  • scope
)
Boolean public static

Check if EventBroker has a specific event listener already added.

Parameters:

  • type String

    The type of event.

  • callback Function

    The listener method to call.

  • scope Any

    The scope of the listener object.

Returns:

Example:

 EventBroker.hasEventListener(BaseEvent.CHANGE, this._handlerMethod, this);

print () String public static

Prints out each event listener in the console.log

Returns:

Example:

 this.printEventListeners();

 // [ClassName] is listening for the 'BaseEvent.change' event.
 // [AnotherClassName] is listening for the 'BaseEvent.refresh' event.

removeEventListener
(
  • type
  • callback
  • scope
)
public static

Removes a specified listener from the EventBroker object.

Parameters:

  • type String

    The type of event.

  • callback Function

    The callback function to be removed.

  • scope Any

    The scope of the callback function to be removed.

Example:

EventBroker.removeEventListener('change', this._handlerMethod, this);

EventBroker.removeEventListener(BaseEvent.CHANGE, this._handlerMethod, this);

removeWaitFor
(
  • eventTypes
  • callback
  • scope
)
public static

A way to listen for multiple events. Once all events all are triggered it will no longer

Parameters:

  • eventTypes Array

    A list of event types you are waiting for.

  • callback Function

    The callback function that will be triggered when all event types are

  • scope Any

    The scope of the callback function.

Example:

EventBroker.removeWaitFor(['someEvent', 'anotherEvent', CustomEvent.CHANGE], this._handlerMethod, this);

waitFor
(
  • eventTypes
  • callback
  • scope
)
public static

A way to listen for multiple events.

If only listening for one event use addEventListener.

Parameters:

  • eventTypes Array

    A list of event types you are waiting for.

  • callback Function

    The callback function that will be triggered when all event types are

  • scope Any

    The scope of the callback function.

Example:

EventBroker.waitFor(['someEvent', 'anotherEvent', CustomEvent.CHANGE], this._handlerMethod, this);

_handlerMethod(events) {
     // An array of the event objects you waited for.
}

waitForOnce
(
  • eventTypes
  • callback
  • scope
)
public static

A way to listen for multiple events. Once all events all are triggered this listener will be removed.

If only listening for one event use addEventListenerOnce.

Parameters:

  • eventTypes Array

    A list of event types you are waiting for.

  • callback Function

    The callback function that will be triggered when all event types are

  • scope Any

    The scope of the callback function.

Example:

EventBroker.waitForOnce(['someEvent', 'anotherEvent', CustomEvent.CHANGE], this._handlerMethod, this);

_handlerMethod(events) {
     // An array of the event objects you waited for.
}

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index