I find sometimes seeing several examples helps a concept become clearer. I was stalled on a similar issue til I discovered the onRegister function. I'll include an account here for reference.
Scenario:
In the constructor for myMediator I retrieve a proxy which handles a web service and then call a method named 'send' on the proxy.
Currently, since I don't have my web service set up yet I have hard coded some test data for the proxy to send as a notification body. This notification is sent in the 'send' method. When the web service is set up and working, the 'send' method on the proxy will add a listener to the web service and invoke it. An 'onResult' handler in the proxy will then send the notification with the data as body.
Currently the code executes in this order:
- view component fires creationComplete event
- a mediator listening to this event registers a mediator for the new view component
- in the new mediator's constructor the proxy is retrieved
- the 'send' method is invoked
- the 'send' method sends a 'dataReady' notification (with the test data)
- the new mediator's constructor exits and it is registered with the view
- as part of the registration with the view, the mediator is interrogated for it's notification interests, which happen to include the 'dataReady' notification (so that it can display said data)
You see my problem. In the final version, the mediator will have finished being registered with the View and interrogated before the web service comes back, thus allowing the mediator to respond to the 'dataReady' notification.
Moving the call to the web service to the onReady function of my mediator means that the mediator has been interrogated and is ready to respond to the notification by the time the proxy returns data (be it hardcoded or from the actual service itself). This seems to work just fine.