PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: marcink on February 27, 2009, 09:01:26



Title: question to deferred instantiation
Post by: marcink on February 27, 2009, 09:01:26
hi,

i would like to know if my approach to deferred instatiation goes along best practice:
here my mediator:
:
public class MyMediator extends Mediator
{
public static const NAME : String = "MyMediator";

public function MyMediator()
{
super(NAME);
}

override public function onRegister() : void
{
var vc : ViewComponent = new ViewComponent();
DisplayObject(vc).addEventListener(FlexEvent.CREATION_COMPLETE, this.addView);
this.sendNotification(AppFacade.ADD_CHILD_TO_STAGE, vc);
}

private function addView(e : FlexEvent) : void
{
this.viewComponent = e.target;
}
}

so basically, when i need a flex component, i register the mediator, and the mediator knows itself witch component to instantiate.

thanks


Title: Re: question to deferred instantiation
Post by: Tekool on February 28, 2009, 06:44:07
Be careful when you need to send a notification in the onRegister method. The onRegister method only guarantee that this mediator has been registered. If you have registered it through a prepViewCommand with other mediators, the next mediators in the list will not be registered at this time. If the mediators that aren't registered at this time are interested in the notification you send, they will not receive it.

You better have to send the notification when the view is added to stage. But you have to be sure that all PureMVC instances interested by this notification are already registered.


Title: Re: question to deferred instantiation
Post by: puremvc on February 28, 2009, 07:39:14
Assuming the AppFacade has been registered, though, this should work fine. You could also have a command create the component, pass it to the constructor of MyMediator, register MyMediator and send the ADD_TO_STAGE note, but that just exposes one more actor to the view component, which we want to avoid. This way keeps MyMediator as the sole touchpoint for the view component in the PureMVC apparatus, which is what we're shooting for generally.

-=Cliff> 


Title: Re: question to deferred instantiation
Post by: marcink on February 28, 2009, 05:24:25
thanks a lot,
i'm always amazed, how fast i get the answers...  :D