new UWA.Class.Events()
Add methods to register and dispatch events.
Index
Methods
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
truename of the event as the first argument).Parameters
Name Type Argument Description nameString Event name
argsArray <optional>
The arguments that will be passed to the listeners
contextObject <optional>
Change callback binding
Returns
- Type
- this
-
dispatchAsEventListener(name, args, context) → {Function}
-
Prepare an event to be triggered with
window.eventhas first argument.Parameters
Name Type Argument Description nameString Event name
argsArray <optional>
The arguments that will pass to the listeners
contextObject <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, oronChange: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 nameString The event name (e.g. "onUpdateTitle")
listenerFunction Listener function that will be executed when event is triggered.
contextObject <optional>
Context on which listener will be executed (object that should represent the
thisvariable 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.priorityNumber <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 nameString The event name (e.g. "onUpdateTitle")
listenerFunction Listener function that will be executed when event is triggered
contextObject Context on which listener will be executed (object that should represent the
thisvariable 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.priorityNumber <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 eventsObject The listeners methods indexed by name (e.g. {"onUpdateTitle", function() {}}). These listeners will be called with context passed as second argument.
contextObject Context on which listener will be executed (object that should represent the
thisvariable 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.priorityNumber <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 nameString <optional>
Event name
listenerFunction <optional>
Function to remove
contextVoid <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 nameString <optional>
The event name.
skipMethodBoolean <optional>
false Do not check if the associated method is defined.
Returns
trueif there is at least one event listener attached to this event.- Type
- Boolean