Chaining commands in Cairngorm with Prana

ActionScript, Cairngorm, Flex, Prana 14 Comments »

Problem

We want to be able to execute commands in a sequence, where each command needs to be executed after the previous command has finished. This can be right after a call to execute() in case the command only implements ICommand, or after the invocation of the result() or fault() method, in case the command implements the IResponder interface and fetches some remote data.

Read the rest of this entry »


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

Adding a check for ICommand to addCommand() in Cairngorm

ActionScript, Cairngorm, Design Patterns, Flex, Prana 2 Comments »

Once in a while, when adding event/command mappings in Cairngorm's frontcontroller, I mistakingly add an event instead of a command class to the addCommand() method. Here's an example:

Actionscript:
  1. addCommand(UserEvent.LOAD, LoadUserEvent);

while it really should be:

Actionscript:
  1. addCommand(UserEvent.LOAD, LoadUserCommand);

This is just a mistake when typing fast and with code completion but it can take a while until you find out what you did wrong. To prevent this, I decided to override addCommand() in Prana's CairngormFrontController and add in a check to see if the commandRef argument actually is an implementation of ICommand.

You can checkout this code from Prana's SVN if you like, or you can build a custom version of Cairngorm with the following code:

Actionscript:
  1. public function addCommand(commandName:String, commandRef:Class, useWeakReference:Boolean = true ):void {
  2.   if (commands[ commandName ] != null )
  3.     throw new CairngormError( CairngormMessageCodes.COMMAND_ALREADY_REGISTERED, commandName );
  4.  
  5.   if (null == commandRef) {
  6.     throw new Error("The commandRef argument cannot be null");
  7.   }
  8.   else {
  9.     var classDescription:XML = describeType(commandRef) as XML;
  10.     var implementsICommand:Boolean = (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(ICommand)).length() != 0);
  11.     if (!implementsICommand)
  12.       throw new Error("The commandRef argument '" + commandRef + "' should implement the ICommand interface");
  13.   }
  14.  
  15.   commands[ commandName ] = commandRef;
  16.   CairngormEventDispatcher.getInstance().addEventListener( commandName, executeCommand, false, 0, useWeakReference );
  17. }

This should save you some time in the future.

On a side note, I have been working on an alternative approach to chaining events/commands in Cairngorm. The code is already in Prana's SVN but I haven't had the time to blog about this. Check it out if you're interested and mail me if you want a code example. In the meantime, I'll work a blogpost with an example on how to use it and will publish this by the end of the week.

Happy coding!


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in