Adding a check for ICommand to addCommand() in Cairngorm
ActionScript, Cairngorm, Design Patterns, Flex, Prana Add commentsOnce 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:
-
addCommand(UserEvent.LOAD, LoadUserEvent);
while it really should be:
-
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:
-
public function addCommand(commandName:String, commandRef:Class, useWeakReference:Boolean = true ):void {
-
if (commands[ commandName ] != null )
-
throw new CairngormError( CairngormMessageCodes.COMMAND_ALREADY_REGISTERED, commandName );
-
-
if (null == commandRef) {
-
throw new Error("The commandRef argument cannot be null");
-
}
-
else {
-
var classDescription:XML = describeType(commandRef) as XML;
-
var implementsICommand:Boolean = (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(ICommand)).length() != 0);
-
if (!implementsICommand)
-
throw new Error("The commandRef argument '" + commandRef + "' should implement the ICommand interface");
-
}
-
-
commands[ commandName ] = commandRef;
-
CairngormEventDispatcher.getInstance().addEventListener( commandName, executeCommand, false, 0, useWeakReference );
-
}
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
Christophe Herreman is a software developer living in Belgium. He's working on high-end Flex and AIR solutions at 
October 10th, 2007 at 10:02 pm
Christophe, I’m interested in hearing about the alternative approach to chaining commands. I’m really interested in Prana too, thanks for your work.
tony.hillerson AT effectiveui DOT com
October 10th, 2007 at 11:33 pm
Christophe; consider engaging with the Cairngorm team through cairngorm-devel@yahoogroups.com and having your thoughts shared there and possibly merged into cairngorm itself. I’d love to try and avoid multiple branches and forks that are an interface or so different, and have the community contribute back into the main project.