tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/5-how-to-wait-until-all-mediators-are-instanciatedRobotlegs: Discussion 2018-10-18T16:35:36Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/137463612012-02-16T15:31:21Z2012-02-16T15:31:43ZHow to wait until all Mediators are instanciated? <div><p>Hello,</p>
<p>you should avoid such a setTimeout solutions. Try to put your
entry points in onRegister method of your mediators. I'm not sure
that I understood your problem, but that</p>
<p>The GameMediator sends out an event to retrieve the buttons
stored in a model<br>
dispatch(new GameEvent(GameEvent.RETRIEVE_ALL_BUTTONS));</p>
<p>should be done in the onRegister method of GameMediator . And
before to make that dispatch add your listeners.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/137463612012-02-16T15:55:20Z2012-02-16T15:55:21ZHow to wait until all Mediators are instanciated? <div><p>Thanks for your answer. I forgot to write that the
GameEvent.RETRIEVE_ALL_BUTTONS is already dispatched in the
onRegister/initialize method of the GameMediator.</p>
<p>The <code>dispatch(new
GameEvent(GameEvent.PLAY_SIMONS_TURN));</code> runs an other
PlaySimonsTurnCommand that pushes the MusicButton by sending an
event. The corresponding MusicButtonMediator is listen to the
context <code>addContextListener(MusicButtonEvent.PUSH, pushButton,
MusicButtonEvent);</code> for that Event.<br>
But as I wrote before, the MusicButtonMediator is created to late
so the MusicButtonEvent.PUSH from Simons turn has no effect.</p>
<p>So I think the best solution may be to wait/listen until all
MusicButtonMediators are instanciated and than doing the first
turn.</p></div>Holgertag:robotlegs.tenderapp.com,2009-10-18:Comment/137463612012-02-16T16:12:11Z2012-02-16T16:12:11ZHow to wait until all Mediators are instanciated? <div><p>So, as far as I understood you need to catch an event in a
mediator which is still not initialized, because its view is not
added on the stage. What if you catch that event in a command then
add the view and then dispatch another event.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/137463612012-02-17T10:22:07Z2012-02-17T10:23:00ZHow to wait until all Mediators are instanciated? <div><p>Good point. I thought it's not the best way to add an context
event listener in a command.</p>
<p>I've extended the BaseActor with an addContextListener and
removeContextListener method, dispatch an event in the
MusicButtonMediator.initialize() method and catch this event by the
RetrieveAllMusicButtonsCommand. If all mediators have dispatched
their events the counter is zero and the GameEvent is dispatched
for further actions.</p>
<p>So here is my current solution:</p>
<pre>
<code>public class RetrieveAllMusicButtonsCommand extends BaseActor
{
[Inject]
public var buttons:MusicButtonModel;
private var _counter:uint;
public function execute():void
{
_counter = buttons.buttons.length;
addContextListener(MusicButtonEvent.INIT_MEDIATOR_COMPLETE, initMediatorComplete, MusicButtonEvent);
for each(var vo:MusicButtonVO in buttons.buttons.source)
{
dispatch(new MusicButtonEvent(MusicButtonEvent.ADD, vo));
}
}
private function initMediatorComplete(event:MusicButtonEvent):void
{
_counter--;
if(_counter == 0)
{
removeContextListener(MusicButtonEvent.INIT_MEDIATOR_COMPLETE, initMediatorComplete, MusicButtonEvent);
dispatch(new GameEvent(GameEvent.PLAY_SIMONS_TURN));
}
}
}</code>
</pre>
<p>Do you have any tip how to handle/structure all the events and
their listeners? I mean, the events can be dispatched from nearly
every class of the framework. And (context) event listener can be
defined in the config class, mediators and commands. So in a larger
project it will be very difficult to watch the application flow and
know the places where an event comes from and goes to.</p></div>Holger