View as a Singleton

matej's Avatar

matej

25 Mar, 2013 09:34 AM

Hello,

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 singletons?

For example, I have buttons in the hud that are allways there. Is there a need of maping them as singletons?

Also, I want to get only one instance of that button when I inject it.

  1. Support Staff 1 Posted by Ondina D.F. on 25 Mar, 2013 11:05 AM

    Ondina D.F.'s Avatar

    Hey Matej,

    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
    singletons?

    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.

    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.

    If you had 2 Mediators for the same View, mapped like this:

    mediatorMap.map(SomeView).toMediator(FirstMediator);
    mediatorMap.map(SomeView).toMediator(SecondMediator);
    
    both mediators would have a reference to the same instance of SomeView.

    Does this answer your other questions as well?

    Ondina

  2. 2 Posted by matej on 25 Mar, 2013 11:18 AM

    matej's Avatar

    Yes, thank you.

    edit:

    Another one.

    I have separate view for each button and a mediator that puts signal to empty variable inside.
    What happens when I inject that button inside the mediator of view container?
    How to ensure that the same instance of the button is there?

  3. Support Staff 3 Posted by Ondina D.F. on 25 Mar, 2013 11:19 AM

    Ondina D.F.'s Avatar

    You're welcome!

  4. Ondina D.F. closed this discussion on 25 Mar, 2013 11:19 AM.

  5. matej re-opened this discussion on 25 Mar, 2013 11:20 AM

  6. 4 Posted by matej on 25 Mar, 2013 12:20 PM

    matej's Avatar

    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?

  7. Support Staff 5 Posted by Ondina D.F. on 25 Mar, 2013 05:07 PM

    Ondina D.F.'s Avatar

    It doesn’t make much sense in my opinion, but if you want, you can try this:
    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:

    [Inject] public var view:SomeView;

    then:
    if(!injector.hasMapping(SomeView, "someview"))
    injector.map(SomeView, "someview").toValue(view);

    then dispatch an event to trigger SomeCommand:
    dispatch(new SomeEvent(SomeEvent.LOAD_SOMETHING));

    In SomeCommand you inject SomeView:

    [Inject (name = "someview")] public var someView:SomeView;

    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.
    If you added/removed views dynamically, none of the above would work, I guess. You’d have to try it out.

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

  8. 6 Posted by matej on 27 Mar, 2013 01:44 PM

    matej's Avatar

    Thank you, i didnt think of passing view as a event's payload or in signal.

  9. matej closed this discussion on 27 Mar, 2013 01:45 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