Tutorial: Events

Events

Several objects fire events that your code can receive by registering callback functions for them. Objects that support this have documented that they mix in EventEmitter.

One example is to be notified when a new session instance is created.

var onSessionCreated = function (session) {
  window.alert('New session was created: ' + session.getName());
};

var sessionManager = zfe.getSessionManager();
sessionManager.addListener(sessionManager.Event.SESSION_CREATED, onSessionCreated, this);

Note: the third parameter (this) is optional and provides the context for execution of the callback function. Your application code may or may not need it, but it's unlikely to hurt anything. The function execution context can also be bound with onSessionCreated.bind(this), though passing the context as a separate parameter has an added advantage of making removing of listeners easier.

Removing event listeners

To stop your event listeners from receiving event callbacks they've registered for they can be removed.

sessionManager.removeListener(sessionManager.Event.SESSION_CREATED, onSessionCreated, this);