RobotLegs and Flash IDE CS4 Injection
Posted by Helmut Granda | Filed under RobotLegs
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:
- //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.
- 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.
- org.robotlegs.demos.hellowflash.view.BallMediator
org.robotlegs.demos.hellowflash.view.ReadoutMediator
and there are only 2 properties used in both instances
- view
statsModel
So with that information we very easily update our XML_CONFIG constant as follows:
- <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:
- 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
15 Responses to “RobotLegs and Flash IDE CS4 Injection”
-
jos Says:
December 2nd, 2009 at 11:09 pmPlease 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 -
Helmut Granda Says:
December 2nd, 2009 at 11:23 pmYour 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.
-
Till Schneidereit Says:
December 3rd, 2009 at 4:29 amHey 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 -
Helmut Granda Says:
December 4th, 2009 at 12:04 amHi 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.
-
Joe Says:
December 6th, 2009 at 1:12 pmHello,
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 !
-
Helmut Granda Says:
December 6th, 2009 at 11:18 pmHi 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.
-
Joe Says:
December 7th, 2009 at 3:49 pmHello 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 !
-
Helmut Granda Says:
December 7th, 2009 at 11:53 pmHi 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.
-
Brian Connatser Says:
December 28th, 2009 at 4:08 pmMany 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
-
Robert Penner Says:
January 2nd, 2010 at 8:03 pmAnother approach is to use constructor injection instead of [Inject]. You don’t have to use metadata to use Robotlegs.
-
Gerald Yeo Says:
January 7th, 2010 at 5:51 amThanks 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?
-
eco_bach Says:
April 15th, 2010 at 12:17 pmRobert
Any chance you could provide us with a demo using constructor injection? I’ve been trying *unsuccessfully) to get a working RL+AS3Signals template using the Flash IDE to compile. -
shaun Says:
May 12th, 2010 at 5:17 amHi Helmut,
It has recently been discovered that it is indeed possible to compile Robotlegs projects with the Flash IDE without this workaround. All that’s required is to select “Export SWC” from Publish Settings. That seems to force the compiler to keep all metadata.
-
Helmut Granda Says:
May 12th, 2010 at 9:18 pmThanks for the tip!
So do you mean to export your FLA to a compiled SWC? That would work if you are creating a component but how about when you are creating an app?
-
santhakumar Says:
May 20th, 2010 at 6:51 amHi Helmut,
Thanks for ur advice. As told by shaun, the exported SWF working fine after setting Export SWC option. I think the compiler keeps all metadata and export swf & swc without any issue.
-SK
