Problem With Event Handling in Mediator.

kommisettynagaraju's Avatar

kommisettynagaraju

04 Dec, 2012 07:34 PM

Hi,
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.

  1. Support Staff 1 Posted by Ondina D.F. on 05 Dec, 2012 08:52 AM

    Ondina D.F.'s Avatar

    Hello,

    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 before the mappings occur.
    That could explain why the mediator can’t always hear the event from model. Anyway, it sounds like some kind of race condition.
    Please provide more details about your use case and some code :)
    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?

    Ondina

  2. 2 Posted by kommisettynagar... on 05 Dec, 2012 07:30 PM

    kommisettynagaraju's Avatar

    Hi Ondina,
    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 .

    Thank You
    Nagaraju.K

  3. Support Staff 3 Posted by Ondina D.F. on 05 Dec, 2012 10:41 PM

    Ondina D.F.'s Avatar

    Hi Nagaraju,

    You're welcome.
    Somehow the event from your Model gets fired before the mediator is able to listen for it.
    What happens inside your 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 loadMap().
    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?)
    If that's also not your case, then 
    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.

    In any case, you have to make sure the Mediator has already been created and the listener has been added before accessing the Model. 

    Let me know how it goes:)
    Ondina

  4. 4 Posted by kommisettynagar... on 06 Dec, 2012 03:55 AM

    kommisettynagaraju's Avatar

    Hi Ondina,
    I tried to map listener before loadMap() method but nothing is changed. 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.
    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.
    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 .

    Thank You for your help.
    Nagaraju.k

  5. Support Staff 5 Posted by Ondina D.F. on 06 Dec, 2012 10:28 AM

    Ondina D.F.'s Avatar

    Hi Nagaraju,

    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.

    Hehe, for God's sake?
    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.
    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!

    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.

    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.
    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.
    Is commandMap.mapEvent(MappingEvent.POPULATE_ADDRESS_ON_MAP,LocationSelectedCommand,MappingEvent);
    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 all the classes and all the mappings, but I sure do need the names and mappings of the ones causing trouble and their relationship:)

    By workflow I meant something like this:
    1. Mediator/View mappings done.
    2. View added to stage.
    3. Mediator created.
    4. Listeners for event dispatched by a model added.
    5. User clicks on a button in a view
    6. View dispatches an event
    7. Mediator handles the event
    8. and re-dispatches it to trigger a command
    9. The command calls a service
    10. The service loads data
    11. and when data arrives a model gets updated with the results
    12. the model dispatches an event with the updated data
    13. the mediator handles the event
    14. and passes the data to the view.

    This workflow would not work:
    1. somewhere an event triggers a command
    2. command accesses a service
    3. model gets updated
    4. model dispatches an event
    5. mediator gets created
    6. mediator adds listeners for the event dispatched by the model
    7. nothing happens inside the mediator
    This workflow would work:
    1. mediator is created
    2. mediator adds event listener for an event that will be dispatched by a model
    3. somewhere ( can be inside the mediator, too) an event gets dispatched
    4. a command gets triggered
    5. a service loads data
    6. model dispatches an event with the updated data
    7. mediator handles event from model

    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.

    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.

    Also, see if Stray’s RelaxedEventMap (https://github.com/Stray/robotlegs-utilities-RelaxedEventMap) is a solution for you.

    and have a child context that extends base context and that contains all view mediator mappings.

    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?

    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 .

    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.

    Try to tell me step by step what’s going on with your Model and Mediator and we’ll find a solution together.

    Cheers,
    Ondina

  6. 6 Posted by kommisettynagar... on 07 Dec, 2012 01:52 PM

    kommisettynagaraju's Avatar

    Hi Ondina ,
    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.....?
    Thank You,
    Nagaraju

  7. Support Staff 7 Posted by Ondina D.F. on 07 Dec, 2012 03:59 PM

    Ondina D.F.'s Avatar

    Hi Nagaraju,

    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.

    When you see the trace statement from your Mediator’s onRegister(), it means the Mediator has been created.

    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!!!).

    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.

    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 :)

    Ondina

  8. Support Staff 8 Posted by Ondina D.F. on 07 Dec, 2012 04:33 PM

    Ondina D.F.'s Avatar

    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.
    Is your MainMenu the main display object (Flex Application)?

  9. 9 Posted by kommisettynagar... on 07 Dec, 2012 07:37 PM

    kommisettynagaraju's Avatar

    Hi Ondina,
    I put the trace statements in LoginCompleteCommand where i call the service
    and in the model then in the mediator ... the order of trace is Service ,
    then the Modle then the mediator.
    i don get why "FlexEven.APPLICATION_COMPLETE"? but we are mapping the
    model and service in the
    context ... which i hope will be called first when the application get
    loaded.perhaps i am wrong ... please tell me how and where to call or
    access model. when the above event is fired .Could you provide me with an
    example if you can.
    Thanks

  10. Support Staff 10 Posted by Ondina D.F. on 08 Dec, 2012 09:20 AM

    Ondina D.F.'s Avatar

    i don get why "FlexEven.APPLICATION_COMPLETE"? but we are mapping the model and service in the
    context ... which i hope will be called first when the application get
    loaded.perhaps i am wrong ... please tell me how and where to call or
    access model. when the above event is fired .Could you provide me with an
    example if you can.

    You can read a detailed explanation here:
    http://cloud.github.com/downloads/Ondina/robotlegs-incremental/robo...

    Examples used in the presentation:
    https://github.com/Ondina/robotlegs-incremental/tree/master/robotle...
    (robotlegs-step-1-A, robotlegs-step-1-B, robotlegs-step-2, robotlegs-step-3)

  11. Ondina D.F. closed this discussion on 11 Dec, 2012 11:30 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