tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/535-singleton-injection-is-nullRobotlegs: Discussion 2018-10-18T16:35:38Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/150093612012-04-04T07:07:34Z2012-04-04T07:07:36ZSingleton Injection is null<div><p>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!</p>
<hr>
<p>public class Main extends Sprite<br>
{</p>
<pre>
<code>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);
}</code>
</pre>
<p>}</p>
<hr>
<p>public class MainContext extends Context<br>
{</p>
<pre>
<code>public function MainContext(DisplayObjectContainer=null)
{
super(contextView);
}
override public function startup():void
{
injector.mapSingleton(TestButton);
contextView.addChild(new TestWindow());
}</code>
</pre>
<p>}</p>
<hr>
<p>public class TestButton extends PushButton<br>
{</p>
<pre>
<code>public function TestButton()
{
super(null, 0, 0, "Test Button");
}</code>
</pre>
<p>}</p>
<hr>
<p>public class TestWindow extends Window<br>
{</p>
<pre>
<code>[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!!!!!!!
}</code>
</pre>
<p>}</p></div>Florian Tatzkytag:robotlegs.tenderapp.com,2009-10-18:Comment/150093612012-04-04T09:23:05Z2012-04-27T07:52:58ZSingleton Injection is null<div><p>Hi Florian,</p>
<p>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.</p>
<p>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?</p>
<p>Injecting a view within a View would create dependencies and
therefore might go against what RL tries to avoid ;)</p></div>Jerome Maurey-Delaunaytag:robotlegs.tenderapp.com,2009-10-18:Comment/150093612012-04-04T09:44:28Z2012-04-04T09:44:29ZSingleton Injection is null<div><p>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.</p>
<p>Thank you very much!</p></div>Florian Tatzky