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: trouble with dynamically created mediators  (Read 3036 times)
digitaldogbyte
Newbie
*
Posts: 2


View Profile Email
« on: March 28, 2009, 01:52:39 »

Hoping that some of you guys can give me some insight on why my mediators are failing to register.

In my ApplicationMediator, I have the following code:

:
var xOffset:Number = Constants.ITEM_STARTING_X_COORDINATE;

for (var i:int = 0; i <= maxNumOfItems; i++)
{
var mc:mcItem = new mcItem();
mc.name = i.toString();
viewComponent.addChild(mc);
mc.x = xOffset;
xOffset += mc.width;
facade.registerMediator (new mcItemMediator(mc));
}

From my trace statements in my code I can see that the constructor for mcItemMediator is being called maxNumOfItems times but I am only getting the onRegister being triggered for the first instance of mcItemMediator.

Here is the code for the mcItemMediator:

:
    public class mcItemMediator extends Mediator implements IMediator
    {
                public static const NAME:String = "mcItemMediator";

                public function mcItemMediator(viewComponent:Object):void
                {
                        trace("mcItemMediator");
                        super(NAME, viewComponent);
                }

override public function onRegister():void
{
trace("onRegister");
}
}
Logged
digitaldogbyte
Newbie
*
Posts: 2


View Profile Email
« Reply #1 on: March 28, 2009, 08:06:44 »

Found the solution! I check out the view.registerMediator code and found that a mediator must have a unique name for it to be registered.

So I had to change my code in the ApplicationMediator to:

:
var xOffset:Number = Constants.ITEM_STARTING_X_COORDINATE;

for (var i:int = 0; i <= maxNumOfItems; i++)
{
var mc:mcItem = new mcItem();
mc.name = i.toString();
viewComponent.addChild(mc);
mc.x = xOffset;
xOffset += mc.width;
facade.registerMediator (new mcItemMediator(mc, "mcItemMediator" + i.toString()));
}

And then I also needed to modify the mcItemMediator code to the following:

:
    public class mcItemMediator extends Mediator implements IMediator
    {
                public static var NAME:String = "mcItemMediator";

                public function mcItemMediator(viewComponent:Object, strName:String):void
                {
                        trace("mcItemMediator");
                        NAME = strName;
                        super(NAME, viewComponent);
                }

override public function onRegister():void
{
trace("onRegister");
}
}
Logged
Pages: [1]
Print