tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/2988-how-to-add-a-view-multi-times-when-the-view-map-to-mediatorRobotlegs: Discussion 2013-06-10T14:58:30Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/272281582013-06-10T11:30:48Z2013-06-10T11:30:48Zhow to add a view multi times when the view maped to Mediator?<div><p>Hello yellow,</p>
<p>First of all, you <strong>can’t</strong> add the same
instance of a display object to the stage twice. A display object
can have only one parent at a time.</p>
<p>If you have something like this:</p>
<p>var someView:SomeView = new SomeView();</p>
<p>and try to do this:</p>
<p>someContainer.addElement(someView);<br>
someContainer.addElement(someView);</p>
<p>the second call would fail:</p>
<pre>
<code> public function addElement(element:IVisualElement):IVisualElement
{
var index:int = numElements;
// This handles the case where we call addElement on something
// that already is in the list. Let's just handle it silently
// and not throw up any errors.
if (element.parent == this)
index = numElements-1;
return addElementAt(element, index);
}</code>
</pre>
<p>If you’d add someView to someContainer and also to
anotherContainer, the same instance of someView would be removed
from someContainer and then added to anotherContainer – it
would be re-parented. (Adobe: If you add a visual element that
already has a different container as a parent, the element is
removed from the other container.)</p>
<p>So, you can’t avoid creating new instances of your
view.<br>
What exactly do you want to achieve? Could you give us more
details?</p>
<p>Concerning the error you’re getting, as it says, you have
overridden the first mapping for ContactView with the second
one:<br>
injector.map(ContactView).asSingleton();<br>
mediatorMap.map(ContactView).toMediator(ContactMediator);</p>
<p>For a workaround, see Shaun’s answer :<br>
<a href=
"http://knowledge.robotlegs.org/discussions/robotlegs-2/698-mapping-override-error">
http://knowledge.robotlegs.org/discussions/robotlegs-2/698-mapping-...</a></p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272281582013-06-10T13:02:02Z2013-06-10T13:02:02Zhow to add a view multi times when the view maped to Mediator?<div><p>JFYI<br>
You’re not the only one wanting to have a Single
DisplayObject in multiple containers ;)<br>
See: <a href=
"http://www.bytearray.org/?p=751">http://www.bytearray.org/?p=751</a></p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272281582013-06-10T13:44:44Z2013-06-10T13:51:22Zhow to add a view multi times when the view maped to Mediator?<div><p>hi, Ondina</p>
<p>thank you,<br>
sorry, I may have a problem narrative.</p>
<p>I think <code>ContactView</code> is a Singleton.</p>
<p>Click contact button to show <code>ContactView</code>, and there
is a close button in <code>ContactView</code>, when click the close
button, <code>ContactView</code> will remove from stage.</p>
<p>when I click the contact button second times, I want to show the
<code>ContactView</code> just has removed, not <code>new
ContactView</code>.</p>
<p>so, I need a way to get the same <code>ContactView</code>
instance in other Mediator</p></div>yellow.astag:robotlegs.tenderapp.com,2009-10-18:Comment/272281582013-06-10T14:38:07Z2013-06-10T14:38:07Zhow to add a view multi times when the view maped to Mediator?<div><blockquote>
<p>when I click the contact button second times, I want to show the
ContactView just has removed, not new ContactView.</p>
</blockquote>
<p>If I understood correctly, you want something like this:<br></p>
<pre>
<code>
private var contactView:ContactView = new ContactView();
private function createContactButton_clickHandler(event:MouseEvent):void
{
addElement(contactView);
}</code>
</pre>
<p>It creates just one instance of contactView. If you already
added it to the stage, and you click the createContactButton,
nothing will happen. If you removed it from stage, and you click
createContactButton, the same instance of contactView will be added
to the stage.</p>
<p>But, I don’t understand what you mean here:</p>
<blockquote>
<p>so, I need a way to get the same ContactView instance in other
Mediator</p>
</blockquote>
<p>You want to map it to another mediator, other than
ContactMediator, or do you want to inject it into a mediator of
another view? Could you explain what you need?</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/272281582013-06-10T14:51:54Z2013-06-10T14:53:09Zhow to add a view multi times when the view maped to Mediator?<div><p>hi, Ondina,</p>
<p>OMG, I forgot that.</p>
<p>when I used robotlegs, I just got [Inject] [Inject]
[Inject]....</p>
<pre>
<code>if(contactView == null){
contactView = new ContactView();
}</code>
</pre>
<p>I'm sorry to waste your time.</p>
<p>thank you very much!</p></div>yellow.astag:robotlegs.tenderapp.com,2009-10-18:Comment/272281582013-06-10T14:58:26Z2013-06-10T14:58:26Zhow to add a view multi times when the view maped to Mediator?<div><p>hehe, no problem; I'm glad you're back on track.</p></div>Ondina D.F.