PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: Oscar on September 21, 2007, 11:00:43

Title: How you remove a specific Mediator?
Post by: Oscar on September 21, 2007, 11:00:43

when you want remove an Mediator, you use the "removeMediator()" method from the facade instance. As paramter he takes the name of the Mediator, for example:
But what is, when you have more Objects of the same Mediator, and you want remove only one specific Mediator?

For example:


for(var i=0; i<=10; i++)
   var helloSprite:HelloSprite = new HelloSprite(...);
   helloSprite.name = "helloSprite" + i;
   facade.registerMediator(new HelloSpriteMediator( helloSprite ));
   stage.addChild( helloSprite );

This Code generate 10 Instances of "HelloSprite" Objects, and his Mediator, and put it in the stage Displaylist.

Now you want remove the "helloSprite5" instance.
To remove the "helloSprite5" DisplayObject, you can make this:

var tmpSprite:HelloSprite = stage.getChildByName("helloSprite5");

But how you remove only his corresponding "HelloSpriteMediator"?
Because, when you do this:



it would also remove the Mediators from the other instances, because all Mediators have the same Name, right?

A Mediator has a static constant Name. So every Mediator has the same name.

How you can now remove only an specific Mediator?

Title: Re: How you remove a specific Mediator?
Post by: puremvc on September 21, 2007, 12:08:35
Hi Oscar,

In most cases, the Mediator is not instantiated multiple times, and so the getMediatorName method may simply return the value of the Mediator's static constant NAME.

However if multiple instances will be created and registered, then the getMediatorName method of the Mediator must return a unique id for that instance.

Examine the code for the HelloFlash demo. In particular View the Source for the HelloSpriteMediator.

In the HelloFlash demo all the sprites that get created get a unique ID, and consequently the HelloSpriteMediator's getMediatorName method looks like this:

         * Get the Mediator name
         * <P>
         * Called by the framework to get the name of this
         * mediator. If there is only one instance, we may
         * define it in a constant and return it here. If
         * there are multiple instances, this method must
         * return the unique name of this instance.</P>
         * @return String the Mediator name
        override public function getMediatorName():String
            return helloSprite.id;

Hope this helps,

Title: Re: How you remove a specific Mediator?
Post by: Oscar on September 21, 2007, 12:21:41
Thanks, that's a great Idea to solve this.  :)