Brand new RL2 (test) project, injection issue

Quentin's Avatar

Quentin

26 Apr, 2012 03:06 PM

Hi everyone!
I decided I had to test RL2 on a fake project before using it on a real one...

So I grabbed the sources from GitHub, grabbed Hamcrest's ones and SwiftSuspender's SWC (v2.0.0b3) and added all that to my IntelliJ IDEA Flex project.

After looking around to know how to set things up in RL2 (as opposed to RL) I had these files:

RL2FirstApp.mxml

<?xml version="1.0"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:rl2="robotlegs.bender.mxml.*" xmlns:rl2firstapp="net.tw.test.rl2firstapp.*"
                   xmlns:views="net.tw.test.rl2firstapp.views.*" xmlns:mvcs="robotlegs.bender.bundles.mvcs.*">

    <fx:Declarations>
        <rl2:ContextBuilderTag>
            <mvcs:MVCSBundle/>
            <rl2firstapp:RL2FirstAppConfig/>
        </rl2:ContextBuilderTag>
    </fx:Declarations>

    <views:MainView horizontalCenter="0" verticalCenter="0"/>

</s:WindowedApplication>

RL2FirstAppConfig.as

package net.tw.test.rl2firstapp {
    import net.tw.test.rl2firstapp.views.MainView;
    import net.tw.test.rl2firstapp.views.MainViewMediator;

    import org.swiftsuspenders.Injector;

    import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;

    public class RL2FirstAppConfig {

        [Inject]
        public var mediatorMap:IMediatorMap;

        [Inject]
        public var injector:Injector;

        public function RL2FirstAppConfig() {
            trace('RL2FirstAppConfig', mediatorMap, injector);
            //mediatorMap.mapView(MainView).toMediator(MainViewMediator);
        }
    }
}

Problem is: my trace statement outputs RL2FirstAppConfig null null

Do I have to do something special to get [Inject] instructions to work?
Or is my setup wrong?

If you can, help an RL2 newbie out!
Thanks.

  1. Support Staff 1 Posted by Shaun Smith on 26 Apr, 2012 03:15 PM

    Shaun Smith's Avatar

    Hi Quentin,

    The problem is that values supplied by setter injection are not available in constructors - this is the same with RL1 - as Swiftsuspenders has to construct the instance before it can set any properties on it. There are two solutions:

    1 - Move the dependencies into constructor arguments:

    public function RL2FirstAppConfig(mediatorMap:IMediatorMap, injector:Injector)
    

    2 - Leave them as they are and use [PostConstruct] to trigger an init method:

    [PostConstruct]
    public function init():void
    {
      // dependencies supplied via setter injection are now available
    }
    

    The readme in the RL2 repo shows the first approach (under Application & Module Configuration), but I'll add an example of the second.

    Hope that helps!

  2. 2 Posted by Quentin on 26 Apr, 2012 03:19 PM

    Quentin's Avatar

    Oh boy!

    I was so sure I was lost with RL2's new API that I didn't even see this stupid error of mine!

    Thanks for the help, app works, now.
    And I feel dumb, too!

  3. Quentin closed this discussion on 26 Apr, 2012 03:20 PM.

  4. Shaun Smith re-opened this discussion on 26 Apr, 2012 03:35 PM

  5. Support Staff 3 Posted by Shaun Smith on 26 Apr, 2012 03:35 PM

    Shaun Smith's Avatar

    Haha, no worries - it happens to all of us!

    Still, you've motivated me to update the README, so it's been productive after all :)

    https://github.com/robotlegs/robotlegs-framework/blob/version2/read...

    Hopefully that will prevent others from experiencing the same issue.

  6. Shaun Smith closed this discussion on 26 Apr, 2012 03:35 PM.

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

Keyboard shortcuts

Generic

? 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