Title: Mediator best practice. Post by: valuetime on January 13, 2009, 06:15:51 Since day one of using PureMVC, I have passed through a display object container as a Mediator constructor's viewComponent parameter (instead of creating a UI component before creating the mediator, as in the HelloFlash code example). A colleague pointed out that this is "incorrect". An example:
Say I have a command, which creates a Mediator: SimpleCommand.as: override public function execute(note:INotification):void And in the constructor of said Mediator, I create a display object: SimpleCommand.as: private var _sprite:Sprite; If I do it this way, I only put a reference to my UI component inside its mediator, not inside the command that creates it. Also, I can remove it from the stage, even delete it and re-create it if necessary. Here's how I'm being told to do it: SimpleCommand.as: override public function execute(note:INotification):void So, my question is... Why does it work this way? If I remove my TestMediator, I am still left with a sprite that has no direct references and no instance name, and therefore, no way to remove it from the stage! Also, I much prefer to link my mediators to my UI components (which are intrinsically linked anyway) rather than putting them in stateless commands or unrelated mediators. Am I totally missing something here? Thanks. Title: Re: Mediator best practice. Post by: puremvc on January 13, 2009, 06:53:23 Generally speaking in Flash, you should have a StageMediator with the one and only reference to the stage. When you wand to add something to the stage you should send the object to be added in a note that the StageMediator is interested in.
Also, in your code above, there is no reason to declare a _sprite instance var. That's just one more ref to be nulled. The viewComponent is the sprite. Just write a getter that returns viewComponent cast to Sprite. As for creating the sprite to be added inside its Mediator, sure, no problem with that. Change your mediator to something like: public function MyMediator(){ Title: Re: Mediator best practice. Post by: Jason MacDonald on January 13, 2009, 07:34:22 Just thought I would add that you can still remove a mediators component without having a reference to the stage (or whatever parent) by calling parent.removeChild()
... in your mediator.... Though you really should have a stage mediator with a listener for a RemoveChild notification. The above works in a crunch though. |