Having problems with context listeners

Michael Bauers's Avatar

Michael Bauers

29 May, 2014 08:36 PM

First thing to know is that I am working on a large project I did not write. I am trying to clean up some code, and introduced a bug due to how the code was originally written.

The problem I am having is that I need to dispatch() for an event once I update the model. To be specific, I have a list in the model, and I need to send an event to indicate that I am selecting an item from that list.

The old code put the code to load the list and do the select in the mediator's onRegister function. The code then calls dispatch() to alert all the views that care that an item in the list is to be selected. But not all the views mediator's have been registered so that fails as some listeners are not yet registered.

How do I properly write initialization code which has to call dispatch() and to be sure all the mediators have registered their listeners? I tried to put a creationComplete handler in one of the views, but that does not work.

I have this book on Robot Legs, and I see nothing helpful in there. Google was also not helpful.

This seems like a simple problem - I just need a way to write code be it in a mediator, view or model such that I am sure that code will not run until all the mediators for all the views have registered their listeners.

  1. Support Staff 1 Posted by Ondina D.F. on 29 May, 2014 09:10 PM

    Ondina D.F.'s Avatar

    Hi Michael,

    take a look at the RelaxedEventMap:

    Let me know if it solves your problem.


  2. 2 Posted by Michael Bauers on 30 May, 2014 04:45 PM

    Michael Bauers's Avatar

    I am surprised I need to import some code for what seems to be a basic need.

    I tried to dispatch an event in startup, but that resulted in other problems.

    Seems like there should be some simple way to ensure all the RL legs stuff is in place, such that I can initialize any model data I need via issuing commands and then dispatch events so that views can update.

    But so far, I can either use creationcomplete or application complete to ensure my code runs after the view's component is functional, or dispatch an event in the RL context startup, but not ensure both the views and the mediators are fully functional.

    What did people do before relaxedevent map?

  3. Support Staff 3 Posted by Shaun Smith on 01 Jun, 2014 05:50 PM

    Shaun Smith's Avatar

    Hello Michael,

    I know it seems like a basic thing (and it is), but race-conditions happen when you use an event bus. If a listener is attached after an event has fired, that listener can not possibly catch that event.

    Mediators are only created for views when those views land on the stage - or, put another way, mediators are created lazily. In the case of a Flex UIComponent, the onRegister hook is only called once the UIComponent has actually initialised (even later).

    Robotlegs cannot know when you've finished adding all your views to the stage, and in fact, that doesn't even make much sense - unless every view in your app is created and added to the stage at startup (which is usually not what you want anyway).

    Generally, views/mediators are created on demand, at some later point. If some event has fired in the meantime, you cannot know about it. This is what models are for - they hold state. The events broadcast the fact that the state has changed. If a view cares about that state it needs to do two things:

    1. Check the state
    2. Listen for changes to the state.

    In your case it sounds like the problematic mediators are only doing the second step.

  4. Ondina D.F. closed this discussion on 02 Jul, 2014 07:18 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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