0

I'm trying to build a Flex application using the Robotlegs framework and don't know how to deal with the creation of mediators (-> onRegister being called) only after the creationComplete event of the view component.

My application loads several XML files at startup, models are created from the files and then dispatch events with the data. Problem: When "loading" embedded/local files at startup, the model events are dispatched way before the mediators listeneres are added, although they're mapped before triggering the initial data load in the main context.

Is anybody using robotlegs with flex and has foud a "cleaner" way around this, than manually dispatching an event in the mediators onRegister? As doing so the "automatic" mediation would not really be automatic anymore ...

Edit:
Minimal example code:

Context:
override public function startup( ):void{
mediatorMap.mapView( EditorMenu, EditorMenuMediator );
commandMap.mapEvent( ContextEvent.STARTUP, LoadConfigurationCommand );
dispatchEvent( new ContextEvent( ContextEvent.STARTUP ) );
}

LoadConfigurationCommand:
[Inject] public var configurationService:IXMLLoader;
override public function execute():void{
configurationService.loadXML();
}

ConfigurationService:
public function loadXML(){
trace( "xml loaded" );
dispatch( new XMLLoadedEvent( XMLLoadedEvent.CONFIGURATION_LOADED, result ) );
}

EditorMenuMediator:
override public function onRegister( ):void{
trace( "menu onregister" );
addContextListener( XMLLoadedEvent.CONFIGURATION_LOADED, handleXmlLoaded, XMLLoadedEvent); }

The trace "menu onregister" is happening way before the trace "xml loaded", so the mediator's not listening when the XmlLoadedEvent is dispatched.

vanillu
  • 3
  • 3

2 Answers2

1

I approach this with the StateMachine and grab control of the order of operations. Another approach here would be to add the listener, but also inject the model and check it for data in onRegister. Either approach should put you in front of the race conditions.

Joel Hooks
  • 6,465
  • 4
  • 33
  • 39
  • The StateMachine seems to be exactly what I'm looking for - perfect! So far, I'm trying to avoid injecting my model to the mediator. – vanillu Apr 19 '11 at 15:14
  • Many times I start with injecting (coupling) my mediators and models/services and abstract away from that as needed, but I *always* use the StateMachine to bootstrap a non-trivial application. I'm a control freak ;) – Joel Hooks Apr 19 '11 at 15:16
0

I'm not expert in RobotLegs (I'm actually a Parsley addict), however, if you're using a Mediator pattern, that means that your mediator has direct access to the view itself. How about you create an interface class for all your views that has an init public function which your mediator could call after the onRegister.

Maybe Robotlegs has another way of doing it with metadata or events or something, but this is still valid.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • Maybe my question was a little irritating - accessing the view from my mediator works just fine. What I'm doing is: Mapping View to Mediator -> adding eventlistener for Event.dataloaded event in the mediators onRegister -> Loading (local) Data, parsing it to a Model and dispatching Event.dataloaded. But the view doesn't notice the event, as rendering process takes too long and onRegister is called after Event.dataloaded is dispatched. Could solve this by either using the RelaxedEventMap,which stores the Event for me , or manually requesting the initial data load from the views onRegister... – vanillu Apr 19 '11 at 12:39
  • ... which I'd have to do for each view that needs some initial data to be loaded, to make sure they're all ready for receiving events. But that just doesn't feel right! – vanillu Apr 19 '11 at 12:44
  • Erm, show some code maybe? Help me out here, I'm completely in the dark. – J_A_X Apr 19 '11 at 12:48
  • Ah, now I get it. How I would fix this is by adding your xml to a model that the context is aware of it, then inject it within your mediator. Does that make sense? Create a model in the context, have it be injected in the service to which the server saves to it, and have the same model injected into the mediator. – J_A_X Apr 19 '11 at 14:34
  • That does make sense, but injecting the model to the mediator is the easy way out I'd like to avoid. It leads to tight coupling and one tends to put too much logic in the mediator, as directly accessing and manipulating the model is just more convenient than writing commands and dispatching events. That's at least what I read in the robotlegs docs and on the official forums, I'm not really experienced with the framework yet. – vanillu Apr 19 '11 at 19:36