Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: How you remove a specific Mediator?  (Read 9697 times)
Oscar
Jr. Member
**
Posts: 12


View Profile Email
« on: September 21, 2007, 11:00:43 »

Hi,   

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:
:
facade.removeMediator(HelloSpriteMediator.getMediatorName());
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");
stage.removeChild(tmpSprite);

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

:

facade.removeMediator(HelloSpriteMediator.getMediatorName());


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?
« Last Edit: September 21, 2007, 11:08:30 by Oscar » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 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,
-=Cliff>
Logged
Oscar
Jr. Member
**
Posts: 12


View Profile Email
« Reply #2 on: September 21, 2007, 12:21:41 »

Thanks, that's a great Idea to solve this.  :)

Logged
Pages: [1]
Print