Deferred instantiation of mediators in a PureMVC Flex application
I'm creating my first project using PureMVC after using Cairngorm for a few projects. Once I started understanding the concepts and intent behind each of the architecture components (Proxies and Models, Views and Mediators, Controllers and Commands), it's been great in it's relative simplicity. I did printed out the docs and read over them everyday at lunch for a few days in a row just make it all really sink it, but it didn't take too long to click.
The best part is that I've had a few questions so I decided to check out the PureMVC forums on the PureMVC site. So far everything I've searched has returned a thread with a very detailed, extensive answer from Cliff! The days go so much easier when there's easy to find and good information about whatever your working with.
One of the problems I ran into pretty quickly was that I had a TabNavigator with 3 tabs where the contents aren't to be drawn or loaded until they are clicked on.
I initially tried to register the mediators for those 3 view components at startup, which to no surprise threw runtime errors because the views didn't yet exist. Not being sure what the best solution was here, I searched the forums and found this thread.
I went with Cliff's recommendation (deferred instantiation) and here's what I have now:
My TabNavigator has 3 tabs each containing one custom component named "tags", "groups", "info".
In each of these components I have a creationComplete event which calls an onCreationComplete function, which in turn dispatches a custom Event:
dispatchEvent( new ComponentLoadedEvent( ComponentLoadedEvent.COMPONENT_LOADED, true, false, this ) );
The mediator of the TabNavigator itself listens for this event and calls handleLoad when it hears it:
mainComponent.addEventListener( ComponentLoadedEvent.COMPONENT_LOADED, handleLoad );
The handleLoad() registers the appropriate mediator for the loaded component:
public function handleLoad( e : Event ):void
{
var component :Object = e["component"].name;
switch(component)
{
case "tags":
facade.registerMediator( new TagsMediator( mainComponent.tags ) );
break;
case "groups":
facade.registerMediator( new GroupsMediator( mainComponent.groups ) );
break;
case "info":
facade.registerMediator( new UploadStatusMediator( mainComponent.info ) );
break;
}
}
Save This Page