« Securing AMFPHP 1.9 via Authentication - PureMVC Pipes »

PureMVC + Lazily [late] Instantiated Components

6 June 2008

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


One Response to ' PureMVC + Lazily [late] Instantiated Components '

Subscribe to comments with RSS or TrackBack to ' PureMVC + Lazily [late] Instantiated Components '.

  1. diamondTearz said,

    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!

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word