Documentation

Events

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

Basic example

//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:
Name Type Description
name String

The name of event to emit

args String[]

0 or more additional data arguments to deliver with the event

off(name, listener)

Unsubscribes a callback to an event name.

Parameters:
Name Type Description
name String

The event name to unsubscribe the listener from

listener function

The listener callback to unsubscribe from the event name

offAll(name)

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

Parameters:
Name Type Argument Description
name String <optional>

The event name to unsubscribe all listeners from

offAny(listener)

Unsubscribes a callback from the set of 'any' event listeners.

Parameters:
Name Type Description
listener function

A listener callback to unsubscribe from any event name

on(name, listener)

Subscribes a callback to an event name.

Parameters:
Name Type Description
name String

The event name to subscribe the listener to

listener function

A listener callback to subscribe to the event name

onAny(listener)

Subscribes a callback to all events, regardless of name.

Parameters:
Name Type Description
listener function

A listener callback to subscribe for any event name

once(name, listener)

Subscribes a callback to an event name, removing it once fired.

Parameters:
Name Type Description
name String

The event name to subscribe the listener to

listener function

A listener callback to subscribe to the event name