21
ROBOTLEGS Extensions & Utilities

Robotlegs Extensions

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

Page 1: Robotlegs Extensions

ROBOTLEGSExtensions & Utilities

Page 2: Robotlegs Extensions

Who am I

Yennick TrevelsRich Application Consultant @ iDA MediaFoundry

Adobe Flex 4 Certified Expert

Page 3: Robotlegs Extensions

Robotlegs

3rd generation MVC(S) Micro architecture

Lightweight

Dependency Injection

Page 4: Robotlegs Extensions
Page 5: Robotlegs Extensions

Signals extension

Use signals to trigger commands

Automatic injection of signal parameters into the commands

Page 6: Robotlegs Extensions

AS3-Signals: examplevar contactsLoaded:Signal = new Signal(ArrayCollection);

contactsLoaded.add(contactsLoadedHandler);

contactsLoaded.dispatch(contacts);

private function contactsLoadedHandler(contacts:ArrayCollection):void

{

// handler code

}

Page 7: Robotlegs Extensions

Signals extension: Usage

Contextpublic class MyContext extends SignalContext {

override public function startup():void {

… //other injection mappings

injector.mapSingleton(SaveContactSignal);

signalCommandMap.mapSignalClass( SaveContactSignal, SaveContactCommand);

}

}

Page 8: Robotlegs Extensions

Signals extension: Usage

Mediatorpublic class ContactFormMediator extends Mediator{

[Inject]public var saveContact:SaveContactSignal;

...

private function saveButton_click(event:MouseEvent):void {

saveContact.dispatch(contact);

}

}

Page 9: Robotlegs Extensions

Signals extension: Usage

Commandpublic class SaveContactCommand extends SignalCommand {

[Inject]public var contact:Contact;

...

override public function execute():void {

contactService.saveContact(contact);

} }

Page 10: Robotlegs Extensions

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

Page 11: Robotlegs Extensions

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);

}

}

Page 12: Robotlegs Extensions

Modular extension: Usage

Shell/Module Mediatorpublic class ShellMediator extends ModuleMediator {

override public function onRegister():void {

eventMap.mapListener(moduleDispatcher, ModuleEvent.EXIT_MODULE, moduleExitHandler, ModuleEvent);

}

...

}

Page 13: Robotlegs Extensions

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

Page 14: Robotlegs Extensions

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

Page 15: Robotlegs Extensions

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);

Page 16: Robotlegs Extensions

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);

Page 17: Robotlegs Extensions

OptionCommandMap: UsageTrigger an optionvar event:OptionEvent = new OptionEvent(OptionEvent[“OPTION_1”]);

dispatch(event);

Page 18: Robotlegs Extensions

Macrobot utility

execute commands in:

Sequence: SequenceCommand

Parallel: ParallelCommand

Page 19: Robotlegs Extensions

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

}

Page 20: Robotlegs Extensions

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

}

Page 21: Robotlegs Extensions

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