O'Reilly logo

Professional Cairngorm™ by Jeremy Wischusen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.4. How Do You Use Them?

The most common way events are used is in views, to announce some sort of user gesture.

As discussed in Chapter 5, before an event will do anything, it must be registered in the FrontController with a corresponding command class. You register events using the addCommand function of the FrontController, as in the following:

addCommand(ExampleEvent.EXAMPLE_TYPE, ExampleCommand);

Once the event and command pairing has been registered, you are free to use the event in your application.

In Cairngorm 2.1 events can be dispatched differently than in previous versions. Each event now has its own dispatch function. So you can now do something like the following:

//event where no data needs to be passed
private function initApp():void
{
var event:LoadItemsEvent=new LoadItemsEvent();
event.dispatch();
}
//event where data is being passed
private function itemSelectedHandler(event:ItemEvent):void
{
var addEvent:AddItemToCartEvent=
new AddItemToCartEvent(event.selectedItem);
addEvent.dispatch();
}

In older versions of Cairngorm, you had to directly call the CairngormEventDispatcher dispatchEvent function, as can be seen in the following function, taken from the ShippingInformation.mxml view from the CairngormStore:

private function updateShippingOptions( cost : Number ) : void{
  var event : UpdateShippingCostEvent = new UpdateShippingCostEvent();
  event.cost = cost;
  CairngormEventDispatcher.getInstance().dispatchEvent( event );
}

You no longer have to do this, but you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required