Add to context dynamically

arturo's Avatar


03 Sep, 2010 11:01 AM

Hi, I have a fairly simple question.
If I have a context that has been already created.
And I load a Module later that has a class definition for a Model/Service that I want to be able to add to an existing context so that Model/Service can be [Injected] on to other classes, that of course get loaded after the module that has the model is loaded. Can this be done with Robotlegs? Can I add Classes to the context way after it was initialized and have them still be available for Injection?


  1. Support Staff 1 Posted by Shaun Smith on 03 Sep, 2010 11:06 AM

    Shaun Smith's Avatar

    The context is simply responsible for creating the Robotlegs apparatus (the Command Map, Mediator Map etc), what you need access to is the injector. If it's not already being injected into the place you need it, you can simply:

    public var injector:IInjector;

    Hope that helps!

  2. 2 Posted by Stray on 03 Sep, 2010 11:25 AM

    Stray's Avatar

    Hi Arturo,

    You can add to your injections from other places, not just the context.

    Normally I would use a command for this - for example to map a model as a value once some data has been loaded. But you can also map the module's classes once they are available.

    As long as you don't instantiate the objects that need the injection before you've run the code that does the mapping then all will be ok.

    You do it in exactly the same way as in the context.

    public function execute():void
    injector.mapStuff /// use the mappings that you need just as you would in the context

    The only slightly awkward part is how you make sure the event / command is mapped, because you'll need to make sure the injector is the right one. If I'm thinking correctly then you can't map that Command/Event pairing (that will add the extra injections) because the classes referenced in the command won't yet be available.

    Anyone got specifics on that? I'm not making use of childInjectors yet as my app was in development before they were available. Joel maybe?

    It might be easiest to use the Module utils in this case, although in theory they're intended for the other kind of module (functional areas).


  3. 3 Posted by Arturo on 03 Sep, 2010 12:51 PM

    Arturo's Avatar

    Thanks. I've used Parsley and Cairngorm before and have both way in my head. I'm totally new to Robotlegs. I like Parsley for many things, particularly because the setup is really simple and it just works as you would expect in most cases, but it doesn't in this particular case. For making things simple, Parsley ties everything to the view, its really transparent, but that transparency comes as a bummer when you need a bit more control. Parsley does offer a "dynamic object" but it isn't managed by the framework in the same way as objects declared into the context when the context was created. That's a big bummer for this project. This project requires the ability to add managed objects at runtime, and still keep them full featured, and not have them tied to view children. I could used Caingorm style singletons but I really really really rather not. Been there, done that, really don't want to go back.

    I heard about Robotlegs in the Flex show, a while back, and quickly browsed through it, but certainly kept it in mind. At that time, Parsley fit our needs just fine, and it seemed simpler to use and a little more abstract, doesn't promote any particular pattern at all, which in our particular case was a good thing.

    Than you very much for your quick response, it certainly inspired me to take a much deeper dive into Robotlegs.

  4. Support Staff 4 Posted by Shaun Smith on 03 Sep, 2010 01:06 PM

    Shaun Smith's Avatar

    By default, Robotlegs is driven by SwiftSuspenders (see: ) which is a very nice dynamic DI/IoC library. All Robotlegs adds is some extra tools to tie the DI/IoC container back to things like event-triggered commands and view components. Parsley does treat "dynamic" dependencies differently, whereas all dependencies/configurations are dynamic in SwiftSuspenders/Robotlegs. Hope that helps!

  5. 5 Posted by Arturo on 03 Sep, 2010 10:34 PM

    Arturo's Avatar

    Hey, just wanted to say thanks.
    It passed my initial test with success, In my extended context I added some methods that add what needs to be added at runtime, with some sanity checks.

    To be honest, originally I though that RL was really complicated, once you figure it out it's actually quite simple to use. Here is a tip, get rid of the Hello Flex app in the sample and replace it with a barebones one. When you first take a look at it, it's scary to think that it's the Hello World app. The InsideRIA "tutorials" are good, but it's set up in a way that you have to read through it, it's not obvious at a glance where you are updating an existing file.

    I did my tests using mediators, to get the hang of it. I also ran into an example with PM's which is much more what I'm used to. I believe performance-wise, mediators are better (you tell me).

    One thing that I found interesting is that people complicate using Flex bindings with models through mediators and it's actually really simple. You can easily use a mediator to bind properties of your model with properties of your view and take advantage of Flex Bindings. I read through a few postings in here and they were very confused. Just use the mediator to inject the property into the view. It's really simple. The view doesn't know where it came from, doesn't care, yet you can still simply bind a list component to an arraycollection of the model if you actually want to do so.

    Anyways, thanks for the tip, RL certainly does what I need it to do.

  6. Stray closed this discussion on 12 Feb, 2011 10:55 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac