tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/686-a-mediator-cannot-listen-to-an-event-dispatched-by-another-mediatorRobotlegs: Discussion 2018-10-18T16:35:32Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T08:18:56Z2011-10-03T08:18:56ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Hello,</p>
<p>Robotlegs has a really nice eventDispatcher which is available
in every mediator. For example in your first Mediator:<br>
eventDispatcher.dispatch(new
YourCustomEvent(YourCustomEvent.EVENT))</p>
<p>in your second Mediator:<br>
eventDispatcher.addEventListener(YourCustomEvent.EVENT,
handler)</p>
<p>I strongly recommend to read more about eventMap.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T08:25:11Z2011-10-03T08:25:11ZA mediator cannot listen to an event dispatched by another mediator.<div><p>@krasimir, thanks for the quick reply. I was about to say
"gotcha!" reading your solution but when I tried it, doesn't work
as well.</p>
<p>Here's my code in my first mediator:<br>
dispatch(new LoginEvent(LoginEvent.STARTUP));</p>
<p>second mediator:<br>
eventMap.mapListener(eventDispatcher, LoginEvent.STARTUP,
loadLoginModule);</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T08:29:13Z2011-10-03T08:31:29ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Hm ... strange. There are several reasons to not work:<br>
1. You missed to map your mediator to your view (which means that
the onRegister method is never called) by using mediatorMap.<br>
2. The dispatching of the event is called when the view for the
second mediator is not added to the stage. Which means that the
onRegister method is called later and the listener is added after
the dispatching.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T08:40:33Z2011-10-03T08:45:41ZA mediator cannot listen to an event dispatched by another mediator.<div><p>I guess <a href="/discussions/questions/2" title=
"Discussion #2">#2</a> is my case here. The first mediator is
actually the mediator of my main application and the second
mediator is for the window component to be loaded.</p>
<p>But in my Context, this is the order of mapping my
mediators:<br>
mediatorMap.mapView(LoginWindow, LoginWindowMediator);<br>
mediatorMap.mapView(OptionsSoftware, MainViewMediator);</p>
<p>So how may I able to check in my application that all these
mediators are done being mapped? So when the timer is done, I'll
check for it first before dispatching the event on my main
application.</p>
<p>Thanks</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T08:47:28Z2011-10-03T08:47:28ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Basically you should give us more information about the logic of
your application. As far as I can see the problem is more like a
conceptual rather then technical. Isn't it possible to map your
LoginEvent.STARTUP to a command which actually shows your
window.</p>
<p>From my point of view you are trying to show a view from itself.
I.e. sounds like trying to add the view/window to the stage from
the mediator of that view. Which, by my opinion, is not really the
correct flow.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T09:17:46Z2011-10-03T09:19:04ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Thanks krasimir. I just realized that only my main app mediator
gets the onRegister method fired and the rest is not being
registered. I put a trace statement on each onRegister methods but
only my main app mediator provided the output yet I have them
registered correctly on my Context. :(</p>
<p>I've read articles here on the issue of their mediator not
firing the onRegister also and I've read one of your comments that
the issue was the view wasn't added on stage before the event was
dispatched. But how may I able to achieve this to my case since the
components aren't supposed to be loaded on stage because they have
to wait for a module loader to load them?</p>
<p>Thanks</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T09:27:45Z2011-10-03T09:27:45ZA mediator cannot listen to an event dispatched by another mediator.<div><p>For me, it looks like a good idea to map your event to a
command. I.e.:<br>
- map your event to a command - dispatch your event when the time
does its job - the command is executed and you know that you have
to add/load a specific view to the stage - add your view If you use
this flow the logic which will add the view to the stage is outside
the mediator. I like to think that the mediators are objects which
operates with the views when the views are added on the stage.<br>
If that still not work for you, can you please give us a little bit
more information on what exactly you are trying to achieve.</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T10:34:20Z2011-10-03T10:35:21ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Yes, thank you krasimir. That makes a lot of sense. I should use
a command to do the job of loading a component to stage so that I
can then have its mediator working. And thanks about the way of
thinking what a mediator is. That gives me light to how this
framework really works.</p>
<p>However, on trying to achieve the goal by using a commandMap on
a Context,<br>
commandMap.mapEvent(LoginEvent.STARTUP, OpenLoginWindowCommand,
LoginEvent, true);</p>
<p>... I got the following error: Error: Injector is missing a rule
to handle injection into target [object OpenLoginWindowCommand].
Target dependency: modules.login::LoginWindow</p>
<p>What does that 'missing' mean? I did the [Inject] on my command
class:<br>
[Inject] public var loginWindow:LoginWindow;</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T10:38:10Z2011-10-03T10:39:41ZA mediator cannot listen to an event dispatched by another mediator.<div><p>I'm glad that I helped.<br>
How and where did you map the LoginWindow class? Did you use
injector in your context or ...</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T11:01:16Z2011-10-03T11:01:16ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Well, your question really rings a bell but I can't yet figure
out exactly what made the bell rang and how to see the light for
I'm really new to Robotlegs.</p>
<p>Ok, so these are the the lines inside my startup on context:</p>
<p>mediatorMap.mapView(LoginWindow, LoginWindowMediator);<br>
mediatorMap.mapView(Main, MainViewMediator);<br>
commandMap.mapEvent(LoginEvent.STARTUP, OpenLoginWindowCommand,
LoginEvent, true);</p>
<p>.. and no, I haven't used any injectors in my context.</p>
<p>The error is gone when I tried removing the [Inject] metadata
inside my command class and it did open my new window component
however its mediator didn't fire up the onRegister method since my
trace statement inside it wasn't displaying an output. Is it maybe
because of the "new" keyword inside the execute method that
confuses the mapping?</p>
<p>loginWindow = new LoginWindow(); // removing this line will
throw an error on the next line.<br>
loginWindow.open();</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T11:20:50Z2011-10-03T11:21:55ZA mediator cannot listen to an event dispatched by another mediator.<div><p>If you want to use the [Inject] tag you should map what you want
to inject before that. I.e. Robotlegs is telling you that you are
trying to inject something that the framework is not informed for.
To make the things work with the inject tag, i.e.<br>
[Inject] public var loginWindow:LoginWindow; You should add this in
your context for example:<br>
injector.mapClass(LoginWindow)<br>
Then when you use [Inject] the framework will populate your
variable with an instance of LoginWindow. I.e. there is no need to
use "new LoginWindow" if your injection works.</p>
<p>I'm not sure that you need injection In your case. My advice is
to leave the flow without [Inject] tag, i.e. like you wrote it
above loginWindow = new LoginWindow();<br>
The onRegister method is called if the loginWindow is added on the
stage. So, you should check what is going on when you call .open()
method and is the LoginWindow instance actually added.</p>
<p>P.S.<br>
I strongly recommend <a href=
"http://www.amazon.com/ActionScript-Developers-Guide-Robotlegs-Hooks/dp/1449308902">
http://www.amazon.com/ActionScript-Developers-Guide-Robotlegs-Hooks...</a><br>
It covers everything about Robotlegs ;)</p></div>krasimirtag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T12:37:25Z2011-10-03T12:37:25ZA mediator cannot listen to an event dispatched by another mediator.<div><p>I didn't know there is really an "injector" exist aside from the
[Inject]. Your suggestion does give me now the idea as to why the
bell rang earlier from my comment. When I added
"injector.mapSingleton(LoginWindow);" to the context, it
successfully injected the login window to my command class and does
not throw an error whether I use "new LoginWindow()" or not.</p>
<p>But the problem is the onRegister still doesn't fire up. Then I
reflected on this,<br>
"The onRegister method is called if the loginWindow is added on the
stage." and realized to ask the question whether loading a window
really adds the window to the stage of the app that is calling?
Since the native window has its own stage, I think that is why the
onRegister does not fire up since it isn't being loaded to the
original stage.</p>
<p>My bad, this issue isn't a Robotleg issue but rather a Flex
issue. I guess I'll open a discussion for this issue at the Flex
forum than here.</p>
<p>I learned a lot from your comments krasimir :) Thanks so much
for the time. I'm happy to choose Robotlegs over the others as to
its simplicity, lightweight-ness and great community support. And
thanks for the book recommendation. I'm excited to have a copy of
it since it is still fresh - published this year. :)</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T12:42:44Z2011-10-03T12:42:44ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Or, does this mean I have to use another context in this case -
for the new window? Is it possible to have more than one context in
an application? Since I'll be using lots of windows in this air
project and will be a modular approach.</p></div>edeventnertag:robotlegs.tenderapp.com,2009-10-18:Comment/103689662011-10-03T12:55:24Z2011-10-03T12:55:24ZA mediator cannot listen to an event dispatched by another mediator.<div><p>Wow! Haha I'm grateful to Robotleg's great community support. :)
It solved my problem now from here: <a href=
"http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/how-to-mediate-a-flex-popup">
http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/how...</a></p>
<p>Flex pop-ups and opening new windows works similarly. I applied
the solution from that post and my loginWindowMediator now firing
up the onRegister method :)</p>
<p>I'll mark this as resolved.</p>
<p>Big thanks,<br>
-Ed</p></div>edeventner