onRemove function not called fast enough

rivella50's Avatar

rivella50

21 Mar, 2013 08:25 AM

Hi there,

i have 2 views with 2 Mediators (a thumbnail overview and a detail view). For transitions between them i use a sliding effect.
When getting back from detail view i encounter that the onRemove() function of the detail Mediator is not immediately called, with the effect that (if i'm fast enough) i can click a thumbnail on the overview and am getting back to the same detail view as before (since onRemove() and the whole removal from Stage didn't happen).
I tried listening to the TRANSITION_COMPLETE event in the detail Mediator (which is actually called before onRemove()), but then i'm just getting an empty view when getting back as described above, since the onRegister() function is not called.
Is there a way to force Robotlegs to initiate the detail view removal faster, i.e. right after i know that the transition to the overview is over ?

  1. Support Staff 1 Posted by Ondina D.F. on 22 Mar, 2013 01:05 PM

    Ondina D.F.'s Avatar

    Hey Valley,

    First of all, sorry for the late response.

    That’s a bit tricky. When using effects, components get re-parented. removedEffect causes the components to be first removed from stage, then added to the stage and then removed again.
    So, in your case by the time you click fast on a new thumbnail a new view is added to the stage, while the old one hasn’t finished transitioning and its mediator hasn’t been destroyed yet.

    A workaround could be the following:

    In the DetailsView you add events handlers for EffectEvent.effectEnd.

    protected function removedEffect_effectEndHandler(event:EffectEvent):void
    {
            removedCompletely = true;
    }
    
    protected function addedEffect_effectEndHandler(event:EffectEvent):void
    {
            dispatchEvent(new SomeEvent(SomeEvent.VIEW_ADDED, this.name));
    }
    

    DetailsMediator

    override public function onRegister():void
    {
            addViewListener(SomeEvent.VIEW_ADDED, onViewAdded, SomeEvent);
    }
    
    private function onViewAdded(event:SomeEvent):void
    {
            eventMap.unmapListener(view, SomeEvent.VIEW_ADDED, onViewAdded, SomeEvent);
            dispatch(new SomeEvent(SomeEvent.VIEW_ADDED));
    }
    
    override public function onRemove():void
    {
            if(view.removedCompletely)
            dispatch(new SomeEvent(SomeEvent.VIEW_REMOVED));
    }
    

    When addedEffect ends, you dispatch an event, say, SomeEvent.VIEW_ADDED.
    DetailsMediator listens for it and re-dispatches it:

    ThumbsMediator listens for SomeEvent.VIEW_ADDED and sets thumbsContainer.enabled to true, which was set to false in ThumbsView when a thumbnail was clicked to prevent clicking on other thumbs while a DetailView is about to be added.

    If you want to add the new view first after the old one has been removed from stage, you can listen to SomeEvent.VIEW_REMOVED, dispatched by the DetailsView first after the removedEffect has finished.

    ThumbsMediator

    override public function onRegister():void
    {
            addContextListener(SomeEvent.VIEW_ADDED, onViewAdded, SomeEvent);
            addContextListener(SomeEvent.VIEW_REMOVED, onViewRemoved, SomeEvent);
    }
    
    private function onViewAdded(event:SomeEvent):void
    {
            view.thumbsContainer.enabled=true;
    }
    
    private function onViewRemoved(event:SomeEvent):void
    {
            view.addAwaitingView();
    }
    

    ThumbsView

    protected function addDetailView(event:MouseEvent):void
    {
            thumbsContainer.enabled=false;
            
           // var viewToAdd:SomeViewToAdd = new SomeViewToAdd();
           [Edit]
           var viewToAdd:DetailsView = new DetailsView ();
    
            if (detailsContainer.numElements==0)
            {
                    detailsContainer.addElement(viewToAdd);
                    return;
            }
            if (awaitingView.indexOf(viewToAdd) == -1)
            {
                    awaitingView.push(viewToAdd);
                    detailsContainer.removeAllElements();
            }
    
    public function addAwaitingView():void
    {
            if (awaitingView.length > 0)
            {
                    detailsContainer.addElement(awaitingView[0]);
                    awaitingView = [];
            }
    }
    

    Does this help?

    Ondina

  2. 2 Posted by rivella50 on 22 Mar, 2013 02:47 PM

    rivella50's Avatar

    Hi Ondina,

    no problem, thanks for answering.
    Speaking clear this means when DetailsMediator dispatches the VIEW_REMOVED Event in onRemove() the ThumbsMediator receives that one (i hope the onRegister() function has already been passed so that context listener is ready) and can then check if the user meanwhile has already clicked on another thumbnail (stored in awaitingView list) which can then be served.
    Clever idea. I will test this.
    Hopefully when i dispatch the VIEW_REMOVED Event at last in onRemove() there isn't another time delay for really removing the DetailsMediator so that the new details request will be correctly served.

    Thanks so far, i let you know if that worked.
    valley

  3. Support Staff 3 Posted by Ondina D.F. on 22 Mar, 2013 02:55 PM

    Ondina D.F.'s Avatar

    You’re welcome! Yes, try it out, and please let me know how it goes :)

    Oh, now I see this line in my code snippet:
    var viewToAdd:SomeViewToAdd = new SomeViewToAdd();

    It has to be
    var viewToAdd:DetailsView = new DetailsView ();

  4. Support Staff 4 Posted by Ondina D.F. on 28 Mar, 2013 05:12 PM

    Ondina D.F.'s Avatar

    I guess this is resolved as well, right?

  5. 5 Posted by rivella50 on 28 Mar, 2013 09:12 PM

    rivella50's Avatar

    I couldn't test it yet, but it sounds promising. So yes, you can close it.
    As soon as i have time i will try it.

    Thank you.
    valley

  6. Support Staff 6 Posted by Ondina D.F. on 29 Mar, 2013 09:20 AM

    Ondina D.F.'s Avatar

    No problem!

  7. Ondina D.F. closed this discussion on 29 Mar, 2013 09:20 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