Events

The flex framework implements an observer design pattern for the dispatch and handling of events within the app. There are a variety of baseline events that get triggered within the framework on a regular basis that you can easily add your own handlers to. And creating your own events that other components can listen for and respond to is just as easy.

Events allow you to write code that stays synchronized with the application as the user interacts with it. By writing event driven code, you can be sure that your features will work with all of the other moving parts in the application.

Handle Events: App.on('event-name', Handler)

Using the App.on() method, you can write your own handler code that responds to any event which is triggered by the application. Depending on the event triggered, any number of parameters may be passed to your handler. Refer to the documentation on the specific event for more information about the parameters available.

flexloader.execute(function($, App) {

  App.on('event-name', function(param1, param2) {
    var event = this;
    // do something to handle event...
    event.myMessage = param1 + ' ' + param2;
  });

});

Whenever you write a handler function for an event with the App.on() method, the this keyword is a reference to the event object that will ultimately be returned to the caller of the event. This object is shared among all of the handlers that are attached to the event.

Trigger Events: App.trigger('event-name', param1, param2)

You can trigger your own app wide events by using the App.trigger() method. The first parameter sent should be the name of the event, and each parameter after that will be passed to any handlers attached to your event name via the App.on() method.

flexloader.execute(function($, App) {

  var param1 = "Hello";
  var param2 = "World";
  var eventData = App.trigger('event-name', param1, param2);
  alert(eventData.myMessage || 'No Message!');

});

The return value of the App.trigger() method will be an event object whose properties (if any) have been set by the handlers attached to the event. If the attached handlers do nothing to modify the event object properties (or if there are no handlers attached to the event), then the return result of the App.trigger() method will simply be an empty object.


Events Documentation