Upload
yennick-trevels
View
132
Download
3
Embed Size (px)
DESCRIPTION
Robotlegs is a popular framework to build Flex applications. This presentation will walk you through some handy extensions which can be used with Robotlegs.
Citation preview
ROBOTLEGSExtensions & Utilities
Who am I
Yennick TrevelsRich Application Consultant @ iDA MediaFoundry
Adobe Flex 4 Certified Expert
Robotlegs
3rd generation MVC(S) Micro architecture
Lightweight
Dependency Injection
Signals extension
Use signals to trigger commands
Automatic injection of signal parameters into the commands
AS3-Signals: examplevar contactsLoaded:Signal = new Signal(ArrayCollection);
contactsLoaded.add(contactsLoadedHandler);
contactsLoaded.dispatch(contacts);
private function contactsLoadedHandler(contacts:ArrayCollection):void
{
// handler code
}
Signals extension: Usage
Contextpublic class MyContext extends SignalContext {
override public function startup():void {
… //other injection mappings
injector.mapSingleton(SaveContactSignal);
signalCommandMap.mapSignalClass( SaveContactSignal, SaveContactCommand);
}
}
Signals extension: Usage
Mediatorpublic class ContactFormMediator extends Mediator{
[Inject]public var saveContact:SaveContactSignal;
...
private function saveButton_click(event:MouseEvent):void {
saveContact.dispatch(contact);
}
}
Signals extension: Usage
Commandpublic class SaveContactCommand extends SignalCommand {
[Inject]public var contact:Contact;
...
override public function execute():void {
contactService.saveContact(contact);
} }
ModuleContext
Modular extension
Without the extension: One Context per module Each module works independently No communication between modules
Facilitates communication between modules
Shell
IModuleEventDispatcherModuleContext
Module
ModuleContext
Module
Modular extension: Usage
Module Contextpublic class MyModuleContext extends ModuleContext { ... }
Module general Mediatorpublic class MyModuleMediator extends ModuleMediator {
...
private function exitModuleHandler():void {
var exitModuleEvent:ModuleEvent = new ModuleEvent(ModuleEvent.EXIT_MODULE);
dispatchToModules(exitModuleEvent);
}
}
Modular extension: Usage
Shell/Module Mediatorpublic class ShellMediator extends ModuleMediator {
override public function onRegister():void {
eventMap.mapListener(moduleDispatcher, ModuleEvent.EXIT_MODULE, moduleExitHandler, ModuleEvent);
}
...
}
Signals & Modular extension?Not possible by default
Solution1. Take the SignalContext code and copy it into a new
ModuleSignalContext class
2. Take the ModularContext code and merge it into the new class
3. Done & works like a charm
OptionCommandMap utilityRun different command based on selected option
Use case:1. User has the choice between several options
2. Based on the chosen option a different command will run
Automatic removal of mappings after one option has been processed
Removes the need for an custom event per option
OptionCommandMap utility
Streamlines the following:SetupcommandMap.mapEvent(SelectCharacterEvent.MAGE,
SelectMageCommand, SelectCharacterEvent); commandMap.mapEvent(SelectCharacterEvent.WARRIOR,
SelectWarriorCommand, SelectCharacterEvent);
Once a command has been processedcommandMap.unmapEvent(SelectCharacterEvent.MAGE,
SelectMageCommand, SelectCharacterEvent); commandMap.unmapEvent(SelectCharacterEvent.WARRIOR, SelectWarriorCommand, SelectCharacterEvent);
OptionCommandMap: UsageContextoverride protected function mapInjections():void { super.mapInjections(); injector.mapValue(IOptionCommandMap, new OptionCommandMap(eventDispatcher, injector, reflector));
}
Inside Mediator or Command//map the options
optionCommandMap.mapOption(1, SelectMageCommand); optionCommandMap.mapOption(2, SelectWarriorCommand);
OptionCommandMap: UsageTrigger an optionvar event:OptionEvent = new OptionEvent(OptionEvent[“OPTION_1”]);
dispatch(event);
Macrobot utility
execute commands in:
Sequence: SequenceCommand
Parallel: ParallelCommand
Macrobot: Usage
Parallelvar command:ParallelCommand = new ParallelCommand();
command.addCommand(FirstCommand);
command.addCommand(SecondCommand);
Command.addCompletionListener(command_completeHandler);
command.execute();
private function command_completeHandler(success:boolean):void
{
//perform completion code here
}
Macrobot: Usage
Sequencevar command:SequenceCommand = new SequenceCommand();
command.addCommand(FirstCommand);
command.addCommand(SecondCommand);
Command.addCompletionListener(command_completeHandler);
command.execute();
private function command_completeHandler(success:boolean):void
{
//perform completion code here
}
ResourcesRobotlegshttp://www.robotlegs.org/
Signals extensionhttps://github.com/joelhooks/signals-extensions-CommandSignal
Modular extensionhttps://github.com/joelhooks/robotlegs-utilities-Modular
OptionCommandMap utilityhttps://github.com/Stray/robotlegs-utilities-OptionCommandMap
Macrobot utilityhttps://github.com/Aaronius/robotlegs-utilities-Macrobot