Singleton Injection is null

Florian Tatzky's Avatar

Florian Tatzky

04 Apr, 2012 07:07 AM

I don't get any instance injected into my class, and no I'm not trying to access that class in the constructor of the class where it's created. These are my classes and I don't get it. I appreciate any help. Thank you!


public class Main extends Sprite
{

private var mainContext:MainContext;

public function Main():void 
{
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void 
{
    removeEventListener(Event.ADDED_TO_STAGE, init);
    // create context
    mainContext = new MainContext(this);
}

}


public class MainContext extends Context
{

public function MainContext(DisplayObjectContainer=null) 
{
    super(contextView);
}

override public function startup():void
{
    injector.mapSingleton(TestButton);
    contextView.addChild(new TestWindow());
}

}


public class TestButton extends PushButton
{

public function TestButton() 
{
    super(null, 0, 0, "Test Button");
}

}


public class TestWindow extends Window
{

[Inject]
public var testButton:TestButton;

public function TestWindow()
{
    super(null, 0, 0, "Test Window");
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

private function onAddedToStage(e:Event):void 
{
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    addChild(testButton);   // <--- NULL!!!!!!!
}

}

  1. 1 Posted by Jerome Maurey-D... on 04 Apr, 2012 09:23 AM

    Jerome Maurey-Delaunay's Avatar

    Hi Florian,

    IIRC this type of injection might go against the decoupling RL tries to impose. Your window and button are both view components and therefore should either be mediated or bypass RL altogether. I mostly use mapSingleton for models and mapSingletonOf for services, using an Interface.

    If I have a view component that needs to be shared by multiple views in my app, I would add a reference to it as part of the models for these views and pass that via the mediators for these views. You could use an interface to make the whole thing more loosely coupled. Ideally views should be as self contained as possible, right?

    Injecting a view within a View would create dependencies and therefore might go against what RL tries to avoid ;)

  2. 2 Posted by Florian Tatzky on 04 Apr, 2012 09:44 AM

    Florian Tatzky's Avatar

    Oh, my bad. I approached it from the wrong end. Thanks for your clarifications, it all makes sense now. My example was also badly chosen. I tried to inject an actor class into my view to have access to the data, where as the mediator class should be the one to mediate between actor and view.

    Thank you very much!

  3. Ondina D.F. closed this discussion on 11 Apr, 2012 11:05 AM.

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