-
Notifications
You must be signed in to change notification settings - Fork 261
Overview
Robotlegs is a lightweight AS3 MVCS Micro-Architecture.
Grab the latest SWC file here:
Flex/FlashBuilder:
Drop the Robotlegs Framework SWC into your “libs” folder.
If you are building a plain ActionScript project you might need to create the “libs” folder manually:
Right click the project, and create a New Folder called “libs”.
Right click the project, open “properties”, “Flex Build Path”, “Library path”, “Add SWC Folder…”, and add the folder “libs”.
Other IDEs or Editors:
Include the SWC in your build path.
please note Robotlegs is currently not compatible with projects compiled with the Flash IDE. This is due to a limitation with the IDE, as it will not recognize custom metadata tags.
important note There is actually a way to using custom metadata tags in Flash CS3/4/5. Check the “Export SWC” option in the “Publish Settings” dialog. See this Blog Post for details.
Contexts, Mediators, View Components, Models, Services and Commands.
Collectively, these are referred to as Framework Actors or simply “actors”.
The Context
Typically, when starting a new project, you extend the default mvcs Context and override the startup() method.
Inside the startup() method you bind a couple of Commands to a startup event and then dispatch that event.
public class HelloFlexContext extends Context { public function HelloFlexContext( contextView:DisplayObjectContainer ) { super( contextView ); } override public function startup():void { commandMap.mapEvent( StartupCommand, ContextEvent.STARTUP, ContextEvent, true ); dispatchEvent( new ContextEvent( ContextEvent.STARTUP ) ); } }
Instantiate the Context and pass it a reference to your view. For a Flex application it might look like this:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:helloflex="org.robotlegs.demos.helloflex.*"> <helloflex:HelloFlexContext contextView="{this}" /> </mx:Application>
If you are building a plain ActionScript application, your main Sprite (entry point) might look like this:
package { import flash.display.Sprite; import org.robotlegs.demos.helloflash.HelloContext; public class HelloActionScript extends Sprite { private var context:HelloContext; public function HelloActionScript() { context = new HelloContext( this ); } } }
By default, a Context will automatically execute its startup() method when its View Component (the “contextView”) is added to the Stage.
Commands
Robotlegs make use of native Flash Player events for framework communication. Commands can be bound to events.
No parameters are passed to a Command’s execute method however. Instead, you define the concrete event that will be passed to the Command as a dependency. This relieves you from having to cast the event.
Multiple Commands can be bound to an event type. They will be executed in the order that they were mapped. This is very handy for mapping your startup commands.
To get a reference to the concrete event that triggered a Command, you must declare the event as a Dependency:
public class TryClearMessages extends Command { [Inject] public var event:SystemEvent; [Inject] public var userProxy:UserProxy; [Inject] public var messageProxy:MessageProxy; override public function execute():void { if ( userProxy.userLoggedIn ) { messageProxy.clearMessages(); } else { contextView.addChild( new LoginPage() ); } } }
Mediators
Robotlegs makes it easy to work with deeply-nested, lazily-instantiated View Components.
You map Mediator classes to View Component classes during startup, or later during runtime, and RobotLegs creates and registers Mediator instances automatically as View Components arrive on the stage (as children of the Context View).
A Mediator is only ready to be interacted with when its onRegister method gets called. This is where you should register your listeners.
The default Mediator implementation provides a handy utility method called addEventListenerTo(). You should use this method to register listeners in your Mediator. Doing so allows RobotLegs to automatically remove any listeners when a Mediator gets removed.
A Mediator might look something like this:
public class HelloFormMediator extends Mediator { [Inject] public var helloForm:HelloForm; [Inject] public var messageProxy:MessageProxy; override public function onRegister():void { // View Listeners helloForm.addEventListener( HelloFormEvent.FORM_SUBMITTED, onFormSubmitted ); // Context Listeners eventDispatcher.addEventListener( MessageProxyEvent.MESSAGE_ADDED, whenMessageAdded ); } private function onFormSubmitted( e:HelloFormEvent ):void { messageProxy.addMessage( helloForm.getMessage() ); } private function whenMessageAdded( e:MessageProxyEvent ):void { helloForm.messageTxt.setFocus(); } }
The Mediator above has two dependencies:
- Its View Component: HelloForm
- The MessageProxy
It listens to events from the view, invokes methods on the MessageProxy’s API, and listens to the Context’s event bus for MessageProxyEvent events.
Models
Models are similar to Mediators, but instead of wrapping View Components, they manage access to data (or Models). To configure a Model for use in a context, you simply create an injection rule like this:
injector.mapSingleton( SomeModel )
You can do that in a Command, or simply in the Context’s startup() method.
Services
Services are like Models, but instead of managing date or state, they manage access to remote services.
injector.mapSingletonOf( ISomeService, SomeXmlService )