how to add a view multi times when the view maped to Mediator?

yellow.as's Avatar

yellow.as

09 Jun, 2013 09:29 AM

I have a view named ContactView,and it have Mediator named ContactMediator,when I click the contact button,it will be show.

I used

mediatorMap.map(ContactView).toMediator(ContactMediator); in Config

and

addChild(new ContactView()); in the button listener

but the ContactView will be create every time when I addChild, I need it just single,so I add these code in Config

injector.map(ContactView).asSingleton();

and

[Inject] public var contact:ContactView;

when I add the view to stage use addChild(contact);, have a error

Warning: Injector already has a mapping for ContactView|. If you have overridden this mapping intentionally you can use "injector.unmap()" prior to your replacement mapping in order to avoid seeing this message.


How can I got a view or mediator instance from somewhere?

  1. Support Staff 1 Posted by Ondina D.F. on 10 Jun, 2013 11:30 AM

    Ondina D.F.'s Avatar

    Hello yellow,

    First of all, you can’t add the same instance of a display object to the stage twice. A display object can have only one parent at a time.

    If you have something like this:

    var someView:SomeView = new SomeView();

    and try to do this:

    someContainer.addElement(someView);
    someContainer.addElement(someView);

    the second call would fail:

        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);
        }
    

    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.)

    So, you can’t avoid creating new instances of your view.
    What exactly do you want to achieve? Could you give us more details?

    Concerning the error you’re getting, as it says, you have overridden the first mapping for ContactView with the second one:
    injector.map(ContactView).asSingleton();
    mediatorMap.map(ContactView).toMediator(ContactMediator);

    For a workaround, see Shaun’s answer :
    http://knowledge.robotlegs.org/discussions/robotlegs-2/698-mapping-...

    Ondina

  2. Support Staff 2 Posted by Ondina D.F. on 10 Jun, 2013 01:02 PM

    Ondina D.F.'s Avatar

    JFYI
    You’re not the only one wanting to have a Single DisplayObject in multiple containers ;)
    See: http://www.bytearray.org/?p=751

  3. 3 Posted by yellow.as on 10 Jun, 2013 01:44 PM

    yellow.as's Avatar

    hi, Ondina

    thank you,
    sorry, I may have a problem narrative.

    I think ContactView is a Singleton.

    Click contact button to show ContactView, and there is a close button in ContactView, when click the close button, ContactView will remove from stage.

    when I click the contact button second times, I want to show the ContactView just has removed, not new ContactView.

    so, I need a way to get the same ContactView instance in other Mediator

  4. Support Staff 4 Posted by Ondina D.F. on 10 Jun, 2013 02:38 PM

    Ondina D.F.'s Avatar

    when I click the contact button second times, I want to show the ContactView just has removed, not new ContactView.

    If I understood correctly, you want something like this:

    
    private var contactView:ContactView = new ContactView();
    private function createContactButton_clickHandler(event:MouseEvent):void
    {
      addElement(contactView);
    }
    

    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.

    But, I don’t understand what you mean here:

    so, I need a way to get the same ContactView instance in other Mediator

    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?

  5. 5 Posted by yellow.as on 10 Jun, 2013 02:51 PM

    yellow.as's Avatar

    hi, Ondina,

    OMG, I forgot that.

    when I used robotlegs, I just got [Inject] [Inject] [Inject]....

    if(contactView == null){
        contactView = new ContactView();
    }
    

    I'm sorry to waste your time.

    thank you very much!

  6. Support Staff 6 Posted by Ondina D.F. on 10 Jun, 2013 02:58 PM

    Ondina D.F.'s Avatar

    hehe, no problem; I'm glad you're back on track.

  7. Ondina D.F. closed this discussion on 10 Jun, 2013 02:58 PM.

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