RobotLegs and Flash IDE CS4 Injection
So you heard of the RobotLegs framework and downloade their demos so you could compile them on your own computer but the only thing that you have available is CS4, not a problem you can still use the framework and I'm going to update the "Hello Flash" demo to show you how. After this article you should be able to update any other demo or create your own with CS4.

Accordingly to the Knowledge Base we get a good kick start by letting us know that we need the following:
ActionScript:
- //Initializing the injector with XML is done by adding this line to your context's constructor, before the super() call:
-
- injector = new SwiftSuspendersInjector(xmlConfiguration);
-
- //Where xmlConfiguration is your XML object.
-
So in our HelloFlashContext file we are going to add our "xmlConfiguration" property that will be passed to the SwiftSuspendersInjector.
So I am going to borrow the XML_CONFIG from the SwiftSuspendersInjector and use it as a guide for my HelloFlashContext class.
ActionScript:
- protected static const XML_CONFIG:XML =
- <types>
- <type name='org.robotlegs.mvcs::Actor'>
- <field name='eventDispatcher'/>
- </type>
- <type name='org.robotlegs.mvcs::Command'>
- <field name='contextView'/>
- <field name='mediatorMap'/>
- <field name='eventDispatcher'/>
- <field name='injector'/>
- <field name='mediatorMap'/>
- </type>
- <type name='org.robotlegs.mvcs::Mediator'>
- <field name='contextView'/>
- <field name='mediatorMap'/>
- <field name='eventDispatcher'/>
- </type>
- </types>;
-
- public function HelloFlashContext(contextView:DisplayObjectContainer)
- {
- injector = new SwiftSuspendersInjector(XML_CONFIG);
- super(contextView);
- }
Now that we know how our XML has to be formed we can start adding our custom properties that will substitute the [Inject] annotations.
There are only two locations where the inject annotation is being used.
ActionScript:
- org.robotlegs.demos.hellowflash.view.BallMediator
- org.robotlegs.demos.hellowflash.view.ReadoutMediator
and there are only 2 properties used in both instances
So with that information we very easily update our XML_CONFIG constant as follows:
ActionScript:
- <type name='org.robotlegs.demos.helloflash.view::BallMediator'>
- <field name='view'/>
- <field name='statsModel'/>
- </type>
- <type name='org.robotlegs.demos.helloflash.view::ReadoutMediator'>
- <field name='view'/>
- <field name='statsModel'/>
- </type>
And here you have the complete HellowFlashContext class updated:
ActionScript:
- package org.robotlegs.demos.helloflash
- {
- import flash.display.DisplayObjectContainer;
-
- import org.robotlegs.base.ContextEvent;
- import org.robotlegs.demos.helloflash.controller.CreateBallCommand;
- import org.robotlegs.demos.helloflash.controller.HelloFlashEvent;
- import org.robotlegs.demos.helloflash.model.StatsModel;
- import org.robotlegs.demos.helloflash.view.Ball;
- import org.robotlegs.demos.helloflash.view.BallMediator;
- import org.robotlegs.demos.helloflash.view.Readout;
- import org.robotlegs.demos.helloflash.view.ReadoutMediator;
- import org.robotlegs.mvcs.Context;
-
-
- import org.robotlegs.adapters.SwiftSuspendersInjector;
-
- public class HelloFlashContext extends Context
- {
- protected static const XML_CONFIG:XML =
- <types>
- <type name='org.robotlegs.demos.helloflash.view::BallMediator'>
- <field name='view'/>
- <field name='statsModel'/>
- </type>
- <type name='org.robotlegs.demos.helloflash.view::ReadoutMediator'>
- <field name='view'/>
- <field name='statsModel'/>
- </type>
-
- </types>;
-
- public function HelloFlashContext(contextView:DisplayObjectContainer)
- {
- injector = new SwiftSuspendersInjector(XML_CONFIG);
- super(contextView);
- }
-
- override public function startup():void
- {
- // Map some Commands to Events
- commandMap.mapEvent(ContextEvent.STARTUP_COMPLETE, CreateBallCommand, ContextEvent, true);
- commandMap.mapEvent(HelloFlashEvent.BALL_CLICKED, CreateBallCommand, HelloFlashEvent );
-
- // Create a rule for Dependency Injection
- injector.mapSingleton(StatsModel);
-
- // Here we bind Mediator Classes to View Classes:
- // Mediators will be created automatically when
- // view instances arrive on stage (anywhere inside the context view)
- mediatorMap.mapView(Ball, BallMediator);
- mediatorMap.mapView(Readout, ReadoutMediator);
-
- // Manually add something to stage
- contextView.addChild(new Readout());
-
- // And we're done
- super.startup();
- }
-
- }
- }
This way you should be able to update any demo and compile with the Flash IDE.
Updated: Thanks to Jos Yule for pointing out that the XML will be concatenated on the existing XML so no need to recreate it all
This entry was posted on December 2, 2009, 5:58 pm and is filed under
RobotLegs. You can follow any responses to this entry through
RSS 2.0.
You can skip to the end and leave a response. Pinging is currently not allowed.
December 2, 2009 - 11:09 pm
Please note that you do not have to pull out the XML from the swiftsuspendersinjector class – it will concatenate your xml to the already existing xml.
So, in fact, the only xml you need is:
Thanks for posting this!
jos
December 2, 2009 - 11:23 pm
Your XML didn’t make it through but I get the idea. I didn’t think the XML would be concatenated at the end so I figured I had to create one every time I needed some custom parameters.
Thanks for the tip.
December 3, 2009 - 4:29 am
Hey Helmut,
thanks for blogging about the XML configuration option! More sources of information are always handy and I guess the documentation is a little compressed. The SwiftSuspenders README contains at least a little bit more information, but it, too, doesn’t do justice to such a complex topic.
thanks again,
till
December 4, 2009 - 12:04 am
Hi Till,
For some one with experience might not need this level or explanation and might see it as redundant but I hope it helps others who “can’t” get it right away and lets not forget the designers and venture to the AS3 world who might find this useful.
December 6, 2009 - 1:12 pm
Hello,
I’m really new to RobotLegs. I never used metadata tags in MXMLC. I check some weeks ago this demo. I try it, and it doesn’t work. So, if I understand right, the FlashDemo never works before, because the Flash CS4 compiler can’t recognize the [Inject] metadata tag. That’s right ?
So the demo never works before your patch ?
And your patch (protected static const XML_CONFIG:XML + injector = new SwiftSuspendersInjector) is what the MXMLC compiler add when you use the [Inject] metadata. That’s right ?
Thanks !
December 6, 2009 - 11:18 pm
Hi Joe
You are correct, Flash CS4 can’t recognize the metadata tags that is why we use the XML and you will run into compiling issues.
The code I provide is not a “patch” but its merely a way to follow the instructions given in the RobotLegs site to be able to compile in Flash, give it a try on one of their demos and you will see how easy it is.
December 7, 2009 - 3:49 pm
Hello again,
When I say “patch” that’s because, when you just download the Flash Demo (http://github.com/joelhooks/robotlegsdemos/tree/master/HelloFlash/) and try to compile it, it doesn’t works, and it’s just strange. Because your “patching” lines are missing.
I have another question
When you compile a demo with Flex and the Flex compiler (for ex: http://github.com/joelhooks/robotlegsdemos/tree/master/HelloFlex/). You use the [Inject] metadata. I try to decompile the generated SWF. But I can’t find the code “generated” par the use of the [Inject] metadata. Can you tell me what/where is physically generated by the [Inject] metadata at the compilation ?
Thanks again !
December 7, 2009 - 11:53 pm
Hi Joe,
I don’t have a decompiler so I wouldn’t know exactly how the code will appear once you decompiler your project. I would visit the site that creates the compiler and see if they have a forum to have this answered by one of their developers since this specific item might be treated different than a regular SWF.
December 28, 2009 - 4:08 pm
Many thanks Helmut for this post! I fought this for a while and started searching on the error and came across your post. This did the trick.
Brian
January 2, 2010 - 8:03 pm
Another approach is to use constructor injection instead of [Inject]. You don’t have to use metadata to use Robotlegs.
January 7, 2010 - 5:51 am
Thanks for the tip. I noticed, however, that the commandMap variable is null in the Command instances when published through the CS4 IDE. It’s fine when I run as app on FDT.
I saw that XML_CONFIG in the class SwiftSuspendersInjector specified 2 occurances of mediatorMap. Could that be the problem?