Archive for the ‘RobotLegs’ tag
Swiz 1.0 Command pattern API explained
As you may know the Swiz team has released RC2 of the upcoming 1.0 version. Among many improvements and bugfixes there is one new feature that brings a built-in command pattern to Swiz. As part of my prep work for Adobe MAX where I will be speaking about AS3/Flex Frameworks I wanted to investigate it in more depth. As you can read in the Swiz docs, this implementation borrows heavily from the RobotLegs framework, which I will be also covering together with Parsley. This approach is really clean and simple but to be honest I kinda hope that it will evolve in future releases to be more metadata-driven, which is actually what Swiz is all about
In the example below you can see it in action (right-click to view the full source code).
So how do we get started?
If, when you read this post, Swiz is still in RC2, then you will need to grab the latest source code from github and build it yourself, the released RC2 has some quirks in regards to its command pattern implementation.
The first thing you do is create a SaveUserCommand class that implements the IEventAwareCommand interface and has one execute function. Obviously this is the function that will implement the command logic. It doesn’t accept any parameters, so if you need any you should provide them by injection. UPDATE 23.09.2010: Triggering event is available through event setter function when you implement IEventAwareCommand instead of ICommand.
package com.riaspace.max.swiz.commands { import com.riaspace.max.swiz.events.UserEvent; import flash.events.Event; import mx.controls.Alert; import org.swizframework.utils.commands.IEventAwareCommand; public class SaveUserCommand implements IEventAwareCommand { private var _event:UserEvent; public function set event(value:Event):void { _event = value as UserEvent; } public function execute():void { Alert.show("SaveUserCommand executed, userName: " + _event.userName); } } }
Next you can map/configure our command using a MyCommands class that extends the Swiz specific CommandMap. Just a hint here for the Swiz team that it would be cool to have the ability to configure commands directly in BeanProvider in future releases
package com.riaspace.swiz.configs { import com.riaspace.swiz.commands.SaveUserCommand; import com.riaspace.swiz.events.UserEvent; import org.swizframework.utils.commands.CommandMap; public class MyCommands extends CommandMap { override protected function mapCommands():void { // Mapping UserEvent.SAVE to SaveUserCommand class mapCommand(UserEvent.SAVE, SaveUserCommand, UserEvent, false); } } }
Now you can instantiate the MyCommands class in the BeanProvider like any other bean:
<?xml version="1.0" encoding="utf-8"?> <swiz:BeanProvider xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:swiz="http://swiz.swizframework.org" xmlns:commands="org.swizframework.utils.commands.*" xmlns:configs="com.riaspace.swiz.configs.*"> <fx:Script> <![CDATA[ import com.riaspace.swiz.models.presentation.MainPM; ]]> </fx:Script> <fx:Declarations> <configs:MyCommands /> <swiz:Prototype type="{MainPM}" /> </fx:Declarations> </swiz:BeanProvider>
At last you can dispatch the UserEvent.SAVE event in your PM class and the SaveUserCommand.execute will be called.
package com.riaspace.swiz.models.presentation { import com.riaspace.swiz.events.UserEvent; import flash.events.IEventDispatcher; public class MainPM { [Dispatcher] public var dispatcher:IEventDispatcher; public function btnExecuteCommand_clickHandler():void { dispatcher.dispatchEvent(new UserEvent(UserEvent.SAVE, "John")); } } }

