tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/689-problem-with-event-handling-in-mediatorRobotlegs: Discussion 2012-12-11T11:30:03Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-04T19:34:23Z2012-12-04T19:34:26ZProblem With Event Handling in Mediator.<div><p>Hi,<br>
I am new to RL .I have a problem which may be strange for newbi's
like me. in my project i am dispatching an event from model . and
i'm handling it in mediator.problem is most of the times the event
is not handled but some times it is handling. in the event handler
i wrote a trace in order to check whether the event is handling
correctly... but it is not handled all the times. But when i try to
map a command with the same event using commanMap.mapEvent().. the
respective command is getting called.. i am vexed . please help
me..Thanks in advance.</p></div>kommisettynagarajutag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-05T08:52:41Z2012-12-05T08:52:41ZProblem With Event Handling in Mediator.<div><p>Hello,</p>
<p>Without seeing the code, my guess is that the View mediated by
the Mediator in question gets added and removed to/from stage
repeatedly (changing states, transitions, re-parenting), thus the
mediator gets removed and destroyed as well, and the event from the
model is dispatched <strong>before</strong> the mappings occur.<br>
That could explain why the mediator can’t always hear the
event from model. Anyway, it sounds like some kind of race
condition.<br>
Please provide more details about your use case and some code
:)<br>
for the mappings for this Mediator in your context, how you add the
listener for the event and what else is happening inside
Mediator’s onRegister(), how are you dispatching the event
from the model and when, and if the custom event is overriding
clone(). How are you adding /removing the view?</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-05T19:30:05Z2012-12-05T19:30:05ZProblem With Event Handling in Mediator.<div><p>Hi Ondina,<br>
Thanks for the quick reply . i don have any states in my project. i
am attaching pieces of my code here..... please have a look .</p>
<p>Thank You<br>
Nagaraju.K</p></div>kommisettynagarajutag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-05T22:41:57Z2012-12-05T22:41:57ZProblem With Event Handling in Mediator.<div><p>Hi Nagaraju,</p>
<p>You're welcome.<br>
Somehow the event from your Model gets fired
<strong>before</strong> the mediator is able to listen for it.<br>
What happens inside your <em>loadMap() method? Are you, by any
chance, dispatching an event to trigger a command that would access
the model or are you accessing the model directly within this
method? If the answer is yes, then you'll have to map the listener
for the event dispatched by the model before calling</em>
loadMap().<br>
If the answer is no, then it means you're accessing the model from
another class, before the mediator has been created (maybe in your
context?)<br>
If that's also not your case, then <br>
you should try to describe the workflow, or show all the classes
involved in that scenario, or attach the entire project, if
possible. You didn't answer all my questions from my previous
post.</p>
<p>In any case, you have to make sure the Mediator has already been
created and the listener has been added before accessing the
Model. </p>
<p>Let me know how it goes:)<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-06T03:55:43Z2012-12-06T03:55:43ZProblem With Event Handling in Mediator.<div><p>Hi Ondina,<br>
I tried to map listener before loadMap() method but nothing is
changed. for God's sake<br>
I am not dispatching any event from loadMap() method i am just
trying to load Google map with all its properties given in this
method.and i am not even accessing the model from this method.<br>
Work Flow is<br>
I have a base context where all commands and models are mapped and
have a child context that extends base context and that contains
all view mediator mappings.<br>
I am enclosing pieces of my project some more clearly as i have no
rights to send the entire project to you .sorry for the
inconvenience .</p>
<p>Thank You for your help.<br>
Nagaraju.k</p></div>kommisettynagarajutag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-06T10:28:58Z2012-12-06T10:28:58ZProblem With Event Handling in Mediator.<div><p>Hi Nagaraju,</p>
<blockquote>
<p>for God's sake I am not dispatching any event from loadMap()
method i am just trying to load Google map with all its properties
given in this method.and i am not even accessing the model from
this method.</p>
</blockquote>
<p>Hehe, for God's sake?<br>
Anyway, it seems you’re starting an asynchronous process in
there before mapping event listeners further down in your
onRegister(). I don’t see the handler
“_addressMap_mapReady” for MapEvent.MAP_READY and
don’t know if it has anything to do with your Model, but
maybe it is interfering with the process of updating the model in
question, in a way or another.<br>
All I can say is that the order in which you are expecting things
to happen is not guaranteed when asynchronous processes/operations
are involved!</p>
<blockquote>
<p>Work Flow is I have a base context where all commands and models
are mapped and have a child context that extends base context and
that contains all view mediator mappings.</p>
</blockquote>
<p>That’s not a workflow :) Seeing the mappings is good, but
it’s impossible to say in which order the commands are
executed and what is the relationship between classes, or which are
the classes you are referring to. There are several mappings and it
seems there is a lot going on.<br>
I can’t see which Model is supposed to dispatch an event to
MainMenuMediator, and at which point in time in your app is this
happening.<br>
Is
commandMap.mapEvent(MappingEvent.POPULATE_ADDRESS_ON_MAP,LocationSelectedCommand,MappingEvent);<br>
the command that accesses the model? When is
LocationSelectedCommand executed? When is the Mediator created?
When are you making service calls and when and where are you
handling the results? Is GeolocationService the one loading the
data for the model in question or is it another service? Is it
ApplicationStateModel or another model you’re talking about?
See, not clear at all :) I don’t need to see
<strong>all</strong> the classes and <strong>all</strong> the
mappings, but I sure do need the names and mappings of the ones
causing trouble and their relationship:)</p>
<p>By workflow I meant something like this:<br>
1. Mediator/View mappings done.<br>
2. View added to stage.<br>
3. Mediator created.<br>
4. Listeners for event dispatched by a model added.<br>
5. User clicks on a button in a view<br>
6. View dispatches an event<br>
7. Mediator handles the event<br>
8. and re-dispatches it to trigger a command<br>
9. The command calls a service<br>
10. The service loads data<br>
11. and when data arrives a model gets updated with the results<br>
12. the model dispatches an event with the updated data<br>
13. the mediator handles the event<br>
14. and passes the data to the view.</p>
<p>This workflow would <strong>not work</strong>:<br>
1. somewhere an event triggers a command<br>
2. command accesses a service<br>
3. model gets updated<br>
4. model dispatches an event<br>
5. mediator gets created<br>
6. mediator adds listeners for the event dispatched by the
model<br>
7. nothing happens inside the mediator<br>
This workflow <strong>would work</strong>:<br>
1. mediator is created<br>
2. mediator adds event listener for an event that will be
dispatched by a model<br>
3. somewhere ( can be inside the mediator, too) an event gets
dispatched<br>
4. a command gets triggered<br>
5. a service loads data<br>
6. model dispatches an event with the updated data<br>
7. mediator handles event from model</p>
<p>You should be able to follow the flow of your application in a
way or another (trace statements, logging, tests, debugger) to see
the order of execution of your methods and/or creation of classes
and so on. Be aware of the asynchronous nature of services, or any
other operations and wait for their completion before making
another call or wanting to use the results. You cannot know which
operation will complete first.</p>
<p>Maybe you should have this order of execution in your Mediator:
Map the listener for model’s event, call loadMap, handle
MAP_READY, dispatch event, trigger a command, access the model, the
model dispatches the event, mediator hears it. But that’s
just pure guessing since I don’t know your real workflow.</p>
<p>Also, see if Stray’s RelaxedEventMap (<a href=
"https://github.com/Stray/robotlegs-utilities-RelaxedEventMap">https://github.com/Stray/robotlegs-utilities-RelaxedEventMap</a>)
is a solution for you.</p>
<blockquote>
<p>and have a child context that extends base context and that
contains all view mediator mappings.</p>
</blockquote>
<p>What do you mean by a ‘child context’? Are you using
multiple contexts in your app? Are you using modules? Or just the
fact that you’re sub-classing the base context?</p>
<blockquote>
<p>I am enclosing pieces of my project some more clearly as i have
no rights to send the entire project to you .sorry for the
inconvenience .</p>
</blockquote>
<p>Oh, I don’t want to see your project! In fact, looking at
an entire project is time consuming, and it’s just a favor to
users in order to give them a specific solution to their particular
issues :) Obviously, I meant code with any sensitive stuff stripped
out or a simplified example that reproduces the issues. Seeing the
code makes it (usually) easier to detect the issues, especially
when the question is not clear enough or the presented use case is
too generic.</p>
<p>Try to tell me step by step what’s going on with your
Model and Mediator and we’ll find a solution together.</p>
<p>Cheers,<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-07T13:52:20Z2012-12-07T13:52:20ZProblem With Event Handling in Mediator.<div><p>Hi Ondina ,<br>
Thanks for your reply .... i came to understand from your reply
that in my application MainMenuMediator has not been created when
the model is dispatching the event... how come i know is ....i
tried to handle the same event in another mediator
(LoginMediator)to check whether it is working or not with a trace
...yes it is working.... but my question is how do i know whether
the mediator is created at the time of dispatching an event by
model.....?<br>
Thank You,<br>
Nagaraju</p></div>kommisettynagarajutag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-07T15:59:37Z2012-12-07T15:59:37ZProblem With Event Handling in Mediator.<div><p>Hi Nagaraju,</p>
<p>Put trace statements inside each relevant method: in your
Model’s method where you dispatch the event, in the
onRegister() of your MainMenuMediator, in the handler method for
the event from model in your MainMenuMediator, and also in the
handler for your MapEvent.MAP_READY(addressMap_mapReady) and so on.
You’ll see the order of execution.</p>
<p>When you see the trace statement from your Mediator’s
onRegister(), it means the Mediator has been created.</p>
<p>If it works with your other Mediator (LoginMediator), it means
the logic you have in your MainMenuMediator’s onRegister() is
the cause of your problems. As I said, your loadMap() is most
probably the culprit ( asynchronous process!!!).</p>
<p>Maybe you can change the logic you have within onRegister(). I
can’t tell you how, because I still don’t know what you
expect to happen in there.</p>
<p>To me, loadMap() is kind of a service call, so maybe you should
use a Service class, accessed by a Command, triggered by an event?
When and where you call the service depends on your mysterious
workflow/ use case :)</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-07T16:33:31Z2012-12-07T16:33:31ZProblem With Event Handling in Mediator.<div><p>You should also try to make service calls or access your models
first after your Flex application has fired
FlexEvent.APPLICATION_COMPLETE. At this point in time you can be
sure that robotlegs has finished its wiring process and your
application is ready to consume external services and to respond to
user input.<br>
Is your MainMenu the main display object (Flex Application)?</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-07T19:37:51Z2012-12-07T19:37:51ZProblem With Event Handling in Mediator.<div><p>Hi Ondina,<br>
I put the trace statements in LoginCompleteCommand where i call the
service<br>
and in the model then in the mediator ... the order of trace is
Service ,<br>
then the Modle then the mediator.<br>
i don get why "FlexEven.APPLICATION_COMPLETE"? but we are mapping
the<br>
model and service in the<br>
context ... which i hope will be called first when the application
get<br>
loaded.perhaps i am wrong ... please tell me how and where to call
or<br>
access model. when the above event is fired .Could you provide me
with an<br>
example if you can.<br>
Thanks</p></div>kommisettynagarajutag:robotlegs.tenderapp.com,2009-10-18:Comment/220699482012-12-08T09:20:25Z2012-12-08T09:20:25ZProblem With Event Handling in Mediator.<div><blockquote>
<p>i don get why "FlexEven.APPLICATION_COMPLETE"? but we are
mapping the model and service in the<br>
context ... which i hope will be called first when the application
get<br>
loaded.perhaps i am wrong ... please tell me how and where to call
or<br>
access model. when the above event is fired .Could you provide me
with an<br>
example if you can.</p>
</blockquote>
<p>You can read a detailed explanation here:<br>
<a href=
"http://cloud.github.com/downloads/Ondina/robotlegs-incremental/robotlegs_incremental_slides.pdf">
http://cloud.github.com/downloads/Ondina/robotlegs-incremental/robo...</a></p>
<p>Examples used in the presentation:<br>
<a href=
"https://github.com/Ondina/robotlegs-incremental/tree/master/robotlegsincremental">
https://github.com/Ondina/robotlegs-incremental/tree/master/robotle...</a><br>
(robotlegs-step-1-A, robotlegs-step-1-B, robotlegs-step-2,
robotlegs-step-3)</p></div>Ondina D.F.