Events

The Events module provides a mechanism for emitting and receiving events.

Basic example

1
2
//The following will create an alert message every time the event `customEvent` is triggered.
AP.require('events', function(events){
  events.on('customEvent', function(){
      alert('event fired');
  });
  events.emit('customEvent');
});

Methods

emit (name, args)

Emits an event on this bus, firing listeners by name as well as all any listeners. Arguments following the name parameter are captured and passed to listeners.

Parameters

NameTypeDescription
nameStringThe name of event to emit
argsArray.<String>0 or more additional data arguments to deliver with the event

off (name, listener)

Removes a particular listener for an event.

Parameters

NameTypeDescription
nameStringThe event name to unsubscribe the listener from
listenerfunctionThe listener callback to unsubscribe from the event name

offAll name

Removes all listeners from an event name, or unsubscribes all event-name-specific listeners if no name if given.

Parameters

NameTypeArgumentDescription
nameString<optional>The event name to unsubscribe all listeners from

offAny (listener)

Removes an any event listener.

Parameters

NameTypeDescription
listenerfunctionA listener callback to unsubscribe from any event name

on (name, listener)

Adds a listener for all occurrences of an event of a particular name. Listener arguments include any arguments passed to events.emit, followed by an object describing the complete event information.

Parameters

NameTypeDescription
nameString

The event name to subscribe the listener to

listenerfunctionA listener callback to subscribe to the event name

onAny (listener)

Adds a listener for all occurrences of any event, regardless of name. Listener arguments begin with the event name, followed by any arguments passed to events.emit, followed by an object describing the complete event information.

Parameters

NameTypeDescription
listenerfunctionA listener callback to subscribe for any event name

once (name, listener)

Adds a listener for one occurrence of an event of a particular name. Listener arguments include any argument passed to events.emit, followed by an object describing the complete event information.

Parameters

NameTypeDescription
nameStringThe event name to subscribe the listener to
listenerfunctionA listener callback to subscribe to the event name

Rate this page: