View with mediator as singleton

mateo's Avatar

mateo

23 Jul, 2013 02:28 PM

I am trying to map a view with a mediator as a singleton so i would be able to inject it.
But with no success. I have tried mapping a mediator to the views interface and then mapping the view as singleton, but i still get a injector error.

How could I achieve this? It seems like this kind of thing would be useful for optimising usage of heavy views 

  1. Support Staff 1 Posted by Ondina D.F. on 23 Jul, 2013 03:34 PM

    Ondina D.F.'s Avatar

    Hi Mateo,

    It doesn't make sense to map a view as singleton. What is your use case?

    Either you map it like this:

    mediatorMap.map(SomeView).toMediator(SomeMediator);
    

    and inject it:

    [Inject]
    public var someView:SomeView;
    

    or you map it like this:

    mediatorMap.map(ISomeView).toMediator(SomeMediator);
    

    and inject it:

    [Inject]
    public var someView:ISomeView;
    

    As long as the view is on stage, the mediator will have a reference to that instance of SomeView.

    If you remove the view from stage, the mediator will be destroyed.
    Now, it depends on how you're adding the view to the stage if the view is also null or not.

    If you create the view like this:

    private var someView: SomeView;//this will keep the view alive
    private function addSomeView ():void
    {
        if (!simpleView)
            someView = new SomeView ();
    
        someContainer.addElement(someView);
    }
    

    when you do
    someContainer.removeElement(someView);

    the view will be removed from someContainer, but it won't get garbage collected, because there is still a reference to it.
    If you add it again to someContainer, the same instance will be injected into SomeMediator.

    If, on the contrary, you wanted to have a new instance of someView injected into your mediator every time a new instance of someView is added to the stage, you'd have to nullify someView after removing it from someContainer, if you don't want to get into trouble (gc-issues)

  2. 2 Posted by mateo on 24 Jul, 2013 09:57 AM

    mateo's Avatar

    Thanks for your answer.
    I have no trouble with mediator usage and injections, and that was not my question.

    Let me elaborate a little more. I have a heavy view that makes my app freeze for 2 seconds when i am instantiating it and adding it to stage.
    But since i need only one instance of that view i wanted to map it as a singleton, create it on the beginning, and be able to inject it in commands that will add that view to the stage.

    From your answer i see that cannot be done, and you're saying make the view be kinda singleton?

  3. Support Staff 3 Posted by creynders on 24 Jul, 2013 10:16 AM

    creynders's Avatar

    Then you need to map it as a value

    //wherever you're instantiating the view
    var massiveView:Massiview = new MassiveView();
    injector.map(MassiveView).toValue(massiveView);
    
    //AddMassiveViewToStageCommand
    [Inject]
    public var massiveView:MassiveView;
    [Inject]
    public var contextView:ContextView;
    public function execute():void{
        contextView.view.addChild(contextView);
    }
    

    But TBH, I'm not sure it's worth the trouble, you might as wel store the instance in the container view and let it add it to the display list, whenever necessary.

  4. Support Staff 4 Posted by Ondina D.F. on 24 Jul, 2013 10:47 AM

    Ondina D.F.'s Avatar

    I have no trouble with mediator usage and injections, and that was not my question.

    It's alright:) It sounded like you had some trouble injecting a view as a singleton into a mediator.

    But since i need only one instance of that view i wanted to map it as a singleton, create it on the beginning, and be able to inject it in commands that will add that view to the stage.

    You should have said that from the beginning. It wasn't clear that you wanted to inject the view into a command instead of a mediator.

    But TBH, I'm not sure it's worth the trouble, you might as wel store the instance in the container view and let it add it to the display list, whenever necessary.

    I agree with creynders. In my example that's what is happening.

    From your answer i see that cannot be done, and you're saying make the view be kinda singleton?

    I said to keep a reference to an instance of someView in case you need to add the same instance to the stage.

  5. 5 Posted by mateo on 24 Jul, 2013 10:57 AM

    mateo's Avatar

    thank you both for your answers!

    Creynders, i tried your solution, but it is not possible to have a view mapped to a mediator, and mapped to value in injector in the same time...

  6. Ondina D.F. closed this discussion on 30 Aug, 2013 07:55 AM.

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