PureMVC + Lazily [late] Instantiated Components
6 June 2008One small caveat when working with PureMVC - If your [view] component is defined at design time, but not created at application startup you’ll need to defer the
facade.registerMediator( )
This might happen, for example, when your component resides in a viewstack.
A simple work around is to dispatch an event (make sure it bubbles) on the component’s creationCompelete.
creationComplete="dispatchEvent(new Event(CommonEvents.COMPONENT_INITIALIZED,true))"
In the ‘application level’ mediator, create an event listener for this event
viewComponent.addEventListener(
CommonEvents.COMPONENT_INITIALIZED,onComponentReady,false,0,true);
When the event is picked up you’ll have to determine which [lazy] component is ready
public function onComponentReady(event:Event):void
{
var comp:UIComponent = event.target as UIComponent;
switch(comp.id)
{
case "userSearch":
facade.registerMediator(new UserSearchMediator(comp));
break;
case "serviceRecord":
facade.registerMediator(new ServiceRecordMediator(comp));
break;
case "appToolbar":
facade.registerMediator(new AppToolbarMediator(comp));
break;
}
}
So, in short, we simply defer creating the component’s mediator until the view component is ready ![]()










on August 2nd, 2008 at 6:52 pm
Wow! And where were you Thursday night at 3:00 AM when I was looking for a workaround?!?!? I eventually hacked up something similar but I felt really guilty about it. This would have been very therapeutic. Great post though!