Robotlegs is a popular framework to build Flex applications. This presentation will walk you through some handy extensions which can be used with Robotlegs.
6. AS3-Signals: example
var contactsLoaded:Signal = new Signal(ArrayCollection);
contactsLoaded.add(contactsLoadedHandler);
contactsLoaded.dispatch(contacts);
private function contactsLoadedHandler(contacts:ArrayCollection):void
{
// handler code
}
7. Signals extension: Usage
Context
public class MyContext extends SignalContext {
override public function startup():void {
… //other injection mappings
injector.mapSingleton(SaveContactSignal);
signalCommandMap.mapSignalClass(
SaveContactSignal, SaveContactCommand);
}
}
8. Signals extension: Usage
Mediator
public class ContactFormMediator extends Mediator{
[Inject]
public var saveContact:SaveContactSignal;
...
private function saveButton_click(event:MouseEvent):void {
saveContact.dispatch(contact);
}
}
9. Signals extension: Usage
Command
public class SaveContactCommand extends SignalCommand {
[Inject]
public var contact:Contact;
...
override public function execute():void {
contactService.saveContact(contact);
}
}
10. ModuleContext
Modular extension
Without the extension:
One Context per module
Each module works independently
No communication between modules
Facilitates communication between modules
Shell
IModuleEventDispatcher
ModuleContext
Module
ModuleContext
Module
11. Modular extension: Usage
Module Context
public class MyModuleContext extends ModuleContext { ... }
Module general Mediator
public class MyModuleMediator extends ModuleMediator {
...
private function exitModuleHandler():void {
var exitModuleEvent:ModuleEvent = new
ModuleEvent(ModuleEvent.EXIT_MODULE);
dispatchToModules(exitModuleEvent);
}
}
12. Modular extension: Usage
Shell/Module Mediator
public class ShellMediator extends ModuleMediator {
override public function onRegister():void {
eventMap.mapListener(moduleDispatcher,
ModuleEvent.EXIT_MODULE, moduleExitHandler,
ModuleEvent);
}
...
}
13. Signals & Modular
extension?
Not possible by default
Solution
1. 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
14. OptionCommandMap utility
Run 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
15. OptionCommandMap utility
Streamlines the following:
Setup
commandMap.mapEvent(SelectCharacterEvent.MAGE,
SelectMageCommand, SelectCharacterEvent);
commandMap.mapEvent(SelectCharacterEvent.WARRIOR,
SelectWarriorCommand, SelectCharacterEvent);
Once a command has been processed
commandMap.unmapEvent(SelectCharacterEvent.MAGE,
SelectMageCommand, SelectCharacterEvent);
commandMap.unmapEvent(SelectCharacterEvent.WARRIOR,
SelectWarriorCommand, SelectCharacterEvent);
16. OptionCommandMap:
Usage
Context
override 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);
Implement Enterprise RIA applications with Java & Flex
Following robotlegs for a while now
Short overview of Robotlegs structure
Alternative to events in actionscript
SignalCommand = actually just a Robotlegs Command, but with the advantage that parameters from the signal dispatch call will be automatically injected
Possible to work in a modular fashion with robotlegs without the extension
Mediator which mediates the main view of your module exists as long as your module
can dispatch module events or listener to events from other modules/shell application
Why OptionCommandMap:
- Removes the need for an event class per option
- automatic cleanup
Why OptionCommandMap:
- Removes the need for an event class per option
- automatic cleanup
Dependency injection is performed before executing the commands no need to worry about that