tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/1152-view-as-a-singletonRobotlegs: Discussion 2018-10-18T16:35:47Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/260562752013-03-25T11:05:57Z2013-03-25T11:05:57ZView as a Singleton<div><p>Hey Matej,</p>
<blockquote>
<p>When we map a view inside the mediator map, is the reference
going to be there as long as the view is on stage, like<br>
singletons?</p>
</blockquote>
<p>That’s right, as long as SomeView is on stage,
SomeMediator will have a reference to that instance of SomeView. If
you remove SomeView from stage, the Mediator gets removed too, if
you don’t set autoRemove to false when you map the view to
the mediator.</p>
<p>If you add a new instance of SomeView to the stage, a new
SomeMediator will be created, and it will have a reference to
SomeView’s new instance.</p>
<p>If you had 2 Mediators for the same View, mapped like
this:<br></p>
<pre>
<code>mediatorMap.map(SomeView).toMediator(FirstMediator);
mediatorMap.map(SomeView).toMediator(SecondMediator);</code>
</pre>
both mediators would have a reference to the same instance of
SomeView.
<p>Does this answer your other questions as well?</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/260562752013-03-25T11:18:00Z2013-03-25T11:19:52ZView as a Singleton<div><p>Yes, thank you.</p>
<p>edit:</p>
<p>Another one.</p>
<p>I have separate view for each button and a mediator that puts
signal to empty variable inside.<br>
What happens when I inject that button inside the mediator of view
container?<br>
How to ensure that the same instance of the button is there?</p></div>matejtag:robotlegs.tenderapp.com,2009-10-18:Comment/260562752013-03-25T11:19:18Z2013-03-25T11:19:18ZView as a Singleton<div><p>You're welcome!</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/260562752013-03-25T12:20:07Z2013-03-25T12:20:07ZView as a Singleton<div><p>And one more, what if I want to inject the view inside the
command. How can I get the same view that is mapped to the
mediator?</p></div>matejtag:robotlegs.tenderapp.com,2009-10-18:Comment/260562752013-03-25T17:07:28Z2013-03-25T17:07:28ZView as a Singleton<div><p>It doesn’t make much sense in my opinion, but if you want,
you can try this:<br>
If SomeView is already on stage and you wanted to inject the same
instance of SomeView into a Command, you’d have to map it
like this inside SomeMediator:</p>
<p>[Inject] public var view:SomeView;</p>
<p>then:<br>
if(!injector.hasMapping(SomeView, "someview"))<br>
injector.map(SomeView, "someview").toValue(view);</p>
<p>then dispatch an event to trigger SomeCommand:<br>
dispatch(new SomeEvent(SomeEvent.LOAD_SOMETHING));</p>
<p>In SomeCommand you inject SomeView:</p>
<p>[Inject (name = "someview")] public var someView:SomeView;</p>
<p>Injecting SomeView into ParentMediator would require something
similar to the above mapping, but, if, for some reason,
ParentMediator gets initialized after SomeMediator, that
wouldn’t work, I think.<br>
If you added/removed views dynamically, none of the above would
work, I guess. You’d have to try it out.</p>
<p>Anyway, I don’t think it’s a good idea to inject
child views into their parent view’s mediator. Can you
explain why you need to do this?<br>
Also, why would you like to inject views into commands? If you
really need to work on views inside a command, you can pass the
views via the event’s payload. Similarly, you could let a
ParentMediator be informed about its view’s children via
events.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/260562752013-03-27T13:44:50Z2013-03-27T13:44:50ZView as a Singleton<div><p>Thank you, i didnt think of passing view as a event's payload or
in signal.</p></div>matej