RobotLegs and Flash IDE CS4 Injection

“UPDATE: It has been discovered that Flash CS3/4 can be instructed to keep metadata after all: Simply select “Export SWC” in your publish settings. Doing so will keep all metadata in tact in your SWF!” – Thanks to shaun for this clarification.

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:

[as]//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.[/as]

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.

[as]protected static const XML_CONFIG:XML =
















;

public function HelloFlashContext(contextView:DisplayObjectContainer)
{
injector = new SwiftSuspendersInjector(XML_CONFIG);
super(contextView);
}[/as]

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.

[as]org.robotlegs.demos.hellowflash.view.BallMediator
org.robotlegs.demos.hellowflash.view.ReadoutMediator[/as]

and there are only 2 properties used in both instances

[as]view
statsModel[/as]

So with that information we very easily update our XML_CONFIG constant as follows:

[as]






[/as]

And here you have the complete HellowFlashContext class updated:

[as]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 =








;

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

}
}[/as]

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

16 thoughts on “RobotLegs and Flash IDE CS4 Injection”

  1. 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

  2. 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.

  3. 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

  4. 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.

  5. 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 !

  6. 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.

  7. 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 !

  8. 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.

  9. 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?

  10. Robert
    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.

  11. Hi 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.

    1. Thanks 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?

  12. Hi 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

  13. Hi Helmut,
    I’m having some difficulty to get the xml config to make it work. The option of exporting the swc it does the trick, but I can’t export with that option on as it breaks some validation I must comply.
    I’m missing something for sure, maybe you or someone else give me hand. I’ve uploaded the the RL helloFlash source, RL, Swiftsuspenders and a CS4 fla:
    http://dl.dropbox.com/u/1329053/HelloFlash.zip
    In case you have some example that works out of the box I would appreciate if you could mail me.

    Thank you,
    am

Comments are closed.