tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/11602-why-get-singleton-displayobject-class-stage-is-nullRobotlegs: Discussion 2014-09-01T12:31:38Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/340022712014-08-03T09:03:36Z2014-08-03T09:03:42ZWhy get singleton DisplayObject Class stage is null?<div><p>I use <code>injector.map(StageLoadingBar).asSingleton();</code>
to create a singleton class.<br>
when i try to use it , I can not get the stage.</p>
<p><code>[Inject]</code> <code>public var
loadingBar:StageLoadingBar;</code></p>
<p>some where i use:<br>
<code>trace(loadingBar.stage)</code> is null</p>
<p>but I put the same class to stage init class<br>
<code>static public var stageLoadingBar:StageLoadingBar;</code></p>
<p>and in StageLoadingBar<br>
<code>MyProject.stageLoadingBar = this;</code></p>
<p>then I trace these at same place:<br>
<code>trace(loadingBar.stage)</code> //null
<code>trace(MyProject.stageLoadingBar)</code> // [object Stage]</p>
<p>crazy</p></div>yellow.astag:robotlegs.tenderapp.com,2009-10-18:Comment/340022712014-08-04T10:08:49Z2014-08-04T10:08:49ZWhy get singleton DisplayObject Class stage is null?<div><p>Hello,</p>
<ul>
<li>Firstly, let's recapitulate a few things about Stage.</li>
</ul>
<p>"The Stage class represents the main drawing area.</p>
<p>The Stage object is not globally accessible. You need to access
it through the stage property of a DisplayObject instance."</p>
<p>"When you instantiate a display object, it will not appear
on-screen (on the Stage) until you add the display object instance
to a display object container that is on the display list.</p>
<p>When you add any visual element to the Stage, that element
becomes a child of the Stage object." [Adobe]</p>
<p><a href=
"http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e3e.html">
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b...</a></p>
<p>There is only ONE Stage! The Stage is the base container of
display objects. Each application has one Stage object, which
contains all on-screen display objects. The Stage is the top-level
container and is at the top of the display list hierarchy</p>
<p><strong>If a display object is not yet on Stage</strong>, as a
direct child of the Stage or of another display object container,
<strong>its stage property is null</strong>.</p>
<p>var someView:SomeView = new SomeView();<br>
Right now, someView.stage is null.</p>
<p>After adding someView to the display list like this:<br>
parentDisplayObject.addChild(someView); or like this:
parentDisplayObject.addElement (someView);</p>
<p>someView.stage is [object Stage], which is shared by all display
objects in the display list.</p>
<ul>
<li>Secondly, the issue you're encountering is due to the fact that
when you inject a view into its mediator and into other classes as
well, the injected objects (views) are not the same. The view
injected into its mapped mediator is the view that has been added
to the stage, where as the view injected into other classes is
(probably) not the same as the one on the stage. I'll give an
example to make it more clear.<br>
You should try out the following code, to see what's going on.</li>
</ul>
<p>Say, you've mapped SomeView like this:</p>
<pre>
<code>injector.map(SomeView).asSingleton();
viewProcessorMap.map(ParentMediator).toInjection();
viewProcessorMap.map(SomeView).toProcess(new MediatorCreator(SomeMediator));</code>
</pre>
<p>ParentView adds SomeView either through mxml or script:</p>
<pre>
<code>var someView:SomeView = new SomeView();
addElement(someView);</code>
</pre>
<p>In SomeMediator someView will have a stage property:</p>
<pre>
<code>[Inject]
public var view:SomeView;
override public function initialize():void
{
trace("view: "+view+" view.stage "+view.stage);// [object Stage]
}</code>
</pre>
<p>In ParenMediator:</p>
<pre>
<code>[Inject]
public var someView:SomeView;
[Inject]
public var view:ParentView;
override public function initialize():void
{
trace("someView: "+ someView +" someView.stage "+ someView.stage);//null
}</code>
</pre>
<p>someView's stage will be null in ParenMediator! If you look at
the traces, you'll see that someView inside ParentMediator is not
the same as the one in SomeMediator. SomeView inside SomeMediator
has been already added to the stage, thus it has a stage property.
Since SomeView inside ParentMediator is not the same instance as
SomeView in SomeMediator and because it hasn't been added to the
stage, its stage is null.</p>
<p>However, you can try this:</p>
<pre>
<code>//var someView:SomeView = new SomeView();
//injector.map(SomeView).toValue(someView);
injector.map(SomeView).asSingleton();
viewProcessorMap.map(ParenMediator).toInjection();
viewProcessorMap.map(SomeView).toProcess(new MediatorCreator(SomeMediator));</code>
</pre>
<p>Then in ParentMediator you add someView to parentView:</p>
<p>view.addElement(someView);</p>
<p>Or, better, use ParentView's API to pass someView, and let
ParentView add SomeView to its stage:</p>
<p>view.parentViewMethod(someView);</p>
<p>After that, both ParentMediator and SomeMediator will have
access to SomeView, and someView will have a stage property.</p>
<p><strong>But</strong>, you have to know that ParentMediator will
keep a reference to SomeView, so SomeView won't get garbage
collected after removing it from stage. Just saying, in case you
run into gc issues:)</p>
<p>Hope that helped.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/340022712014-09-01T12:31:35Z2014-09-01T12:31:35ZWhy get singleton DisplayObject Class stage is null?<div><p>Closing this thread for now. You can re-open it at any time, if
need be.</p></div>Ondina D.F.