I accomplished this by extending the CairngormEvent Class and adding two properties:
package control.events
{
import com.adobe.cairngorm.control.CairngormEvent;
public class AbstractCairngormEvent extends CairngormEvent
{
public var successHandler:Function;
public var failureHandler:Function;
public function AbstractCairngormEvent(type:String)
{
super(type);
}
}
}
And creating a new Class as a base for all Commands, which implements ICommand:
package control.commands
{
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import control.events.AbstractCairngormEvent;
/* This class is a base for all Commands. It allows the user to set callback
functions for successful completion and/or failure of the Command logic (i.e.
a WebService call). */
public class CairngormCommand implements ICommand
{
private var successHandler:Function;
private var failureHandler:Function;
public function execute(event:CairngormEvent):void
{
if (event is AbstractCairngormEvent)
{
var commandEvent:AbstractCairngormEvent =
event as AbstractCairngormEvent;
successHandler = commandEvent.successHandler;
failureHandler = commandEvent.failureHandler;
}
}
public function notifyCallerOfSuccess():void
{
if (successHandler != null)
successHandler.call(this);
}
public function notifyCallerOfFailure():void
{
if (failureHandler != null)
failureHandler.call(this);
}
}
}
Then in each Command Class, when the necessary logic is complete, or there is an error/failure, I call the appropriate function in the CairngormCommand base Class. Here is an example:
// Something like this would be in your view:
private function callSomeWebService():void {
var event:WebServiceEvent = new WebServiceEvent();
myEvent.successHandler = callMyEventSuccessHandler;
myEvent.failureHandler = callMyEventFailureHandler;
}
private function callMyEventSuccessHandler():void {
Alert.show("SUCCESS!!!");
}
private function callMyEventFailureHandler():void {
Alert.show("FAILURE!!!");
}
// Here is the Event in your Controller:
package control.events
{
import control.events.AbstractCairngormEvent;
public class WebServiceEvent extends AbstractCairngormEvent
{
public static var EVENT_ID:String = "webService";
public function WebServiceEvent()
{
super(EVENT_ID);
}
}
}
// And here is the Command in your Controller:
package control.commands
{
import com.adobe.cairngorm.control.CairngormEvent;
import control.commands.CairngormCommand;
import control.events.WebServiceEvent;
public class WebServiceCommand extends CairngormCommand
{
override public function execute(event:CairngormEvent):void
{
super.execute(event);
... // Call WebServices
}
...
private function webServiceResultHandler():void
{
// Handle results
...
notifyCallerOfSuccess();
}
private function webServiceFaultHandler():void
{
// Handle fault
...
notifyCallerOfFailure();
}
}
}
I use this EXCLUSIVELY in my Cairngorm applications now. The best part is, if you don't need to have a callback for success and/or failure, you can just leave those properties out of the event instantiation and simply dispatch the event.
Example of loading data needed for display before the display is loaded:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
usePreloader="false" initialize="initializeHandler();">
<mx:Script>
<![CDATA[
import control.events.InitializeApplicationEvent;
private function initializeHandler():void
{
initializeApplication();
}
private function initializeApplication():void
{
var event:InitializeApplicationEvent =
new InitializeApplicationEvent();
event.successHandler = initializationSuccessHandler;
event.dispatch();
}
private function initializationSuccessHandler():void
{
applicationContainer.visible = true;
}
]]>
</mx:Script>
<control:Services xmlns:control="control.*" />
<control:Controller xmlns:control="control.*" />
<view:ApplicationContainer xmlns:view="view.*" id="applicationContainer"
width="100%" height="100%" visible="false" />
</mx:Application>
InitializeApplicationCommand (notice how you can chain the events and callers as many times as you'd like):
package control.commands
{
import com.adobe.cairngorm.control.CairngormEvent;
import control.events.GetEvenMoreDataEvent;
import control.events.GetSomeDataEvent;
import control.events.GetSomeMoreDataEvent;
import control.events.InitializeApplicationEvent;
public class InitializeApplicationCommand extends CairngormCommand
{
override public function execute(event:CairngormEvent):void
{
super.execute(event);
getSomeData();
}
private function getSomeData():void
{
var event:GetSomeDataEvent = new GetSomeDataEvent();
event.successHandler = getSomeMoreData;
event.failureHandler = errorHandler;
event.dispatch();
}
private function getSomeMoreData():void
{
var event:GetSomeMoreDataEvent = new GetSomeMoreDataEvent();
event.successHandler = getEvenMoreData;
event.failureHandler = errorHandler;
event.dispatch();
}
private function getEvenMoreData():void
{
var event:GetEvenMoreDataEvent = new GetEvenMoreDataEvent();
event.successHandler = notifyCallerOfSuccess;
event.failureHandler = errorHandler;
event.dispatch();
}
private function errorHandler():void
{
alert.show("error");
}
}
}