Class: UWA.Class.Events

UWA/Class/Events. UWA.Class.Events

new UWA.Class.Events()

Add methods to register and dispatch events.

Index

Methods

dispatchEvent(name, args, context) → {this}

Trigger an event, executing all bound listeners. Listeners callbacks are passed the same arguments as <dispatchEvents> is, apart from the event name (unless you're listening on 'onAnyEvent', which will cause your callback to receive the true name of the event as the first argument).

Parameters
Name Type Argument Description
name String

Event name

args Array <optional>

The arguments that will be passed to the listeners

context Object <optional>

Change callback binding

Returns
Type
this

dispatchAsEventListener(name, args, context) → {Function}

Prepare an event to be triggered with window.event has first argument.

Parameters
Name Type Argument Description
name String

Event name

args Array <optional>

The arguments that will pass to the listeners

context Object <optional>

Change callback binding

Returns

bound to dispatch the event passed as first argument before the rest of the arguments.

Type
Function

addEvent(name, listener, context, priority) → {this}

Add a listener associated with the given event name. If you have a large number of different events, you can namespace them, using colons for example: onPoll:start, or onChange:selection.

Listeners of the special "onAnyEvent" event will be executed when any event occurs, and are passed the name of the event as the first argument. See example below.

Example
// Example to proxy all events from one object to another:
proxy.addEvent("onAnyEvent", function (eventName) {
   object.dispatchEvent(eventName);
});
Parameters
Name Type Argument Default Description
name String

The event name (e.g. "onUpdateTitle")

listener Function

Listener function that will be executed when event is triggered.

context Object <optional>

Context on which listener will be executed (object that should represent the this variable inside listener function). If left undefined, the context on which listener will be executed will be the one passed to <dispatchEvent>, which defaults to this instance of Events class.

priority Number <optional>
0

The priority level of the listener. Listeners with higher priority will be executed before listeners with lower priority Listeners with same priority level will be executed at the same order as they were added.

Returns
Type
this

addEventOnce(name, listener, context, priority) → {this}

Add a listener associated with the given event name. Once the event is fired, the listener will be removed. This is handy for saying "the next time that X happens, do this".

Parameters
Name Type Argument Default Description
name String

The event name (e.g. "onUpdateTitle")

listener Function

Listener function that will be executed when event is triggered

context Object

Context on which listener will be executed (object that should represent the this variable inside listener function). If left undefined, the context on which listener will be executed will be the one passed to <dispatchEvent>, which defaults to this instance of Events class.

priority Number <optional>
0

The priority level of the listener. Listeners with higher priority will be executed before listeners with lower priority Listeners with same priority level will be executed at the same order as they were added.

Returns
Type
this

addEvents(events, context, priority) → {this}

Add listeners methods associated with the given event name.

Parameters
Name Type Argument Default Description
events Object

The listeners methods indexed by name (e.g. {"onUpdateTitle", function() {}}). These listeners will be called with context passed as second argument.

context Object

Context on which listener will be executed (object that should represent the this variable inside listener function). If left undefined, the context on which listener will be executed will be the one passed to <dispatchEvent>, which defaults to this instance of Events class.

priority Number <optional>
0

The priority level of the listener. Listeners with higher priority will be executed before listeners with lower priority Listeners with same priority level will be executed at the same order as they were added.

Returns
Type
this

removeEvent(name, listener, context) → {this}

Remove some listeners. Calling it without any argument will remove all listeners. Specifying a name will remove all listeners bound to this event. Specifying a listener function will remove this listener only. Specifying a context will remove only the listeners bound with this context

Parameters
Name Type Argument Description
name String <optional>

Event name

listener Function <optional>

Function to remove

context Void <optional>

Object that should match binding associated to listener context if provided.

Returns
Type
this

removeEvents() → {this}

Remove all listeners.

Returns
Type
this

hasEvent(name, skipMethod) → {Boolean}

Check if this object has an event listener attached to a specified event. If no event name is specified, check if there is at least one event listener attached to an event emitted by this object (in this case, true is returned) or if there is no listener at all attached to any event emitted by this object (in this case, false is returned)

Parameters
Name Type Argument Default Description
name String <optional>

The event name.

skipMethod Boolean <optional>
false

Do not check if the associated method is defined.

Returns

true if there is at least one event listener attached to this event.

Type
Boolean