tags:

views:

112

answers:

3

Hey guys,

I ran into a problem.I'm doing a GIS program using flex.

There's a map in my Application,when I click one icon on the map,windowA pops up,when I click a link inside windowA,windowB pops up,but here my problem coming out,when I close windowB and click the link inside windowA another time,Two windowB pop up...

In my windowA,I have

...
var windowBEvt:WindowBEvent = new WindowBEvent();
CairngormEventDispatcher.getInstance().dispatchEvent(windowBEvt);
...
<control:WindowBControl id='control1'>

In WindowBControl,I have

addCommand(WindowBControl.EVENT_POPUPWindowB,WindowBCommand);

In WindowBCommand,I have

public function execute(event:CairngormEvent):void
{
    ...
    var windowB:WindowB = new WindowB();
    PopUpManager.addPopUp(windowB);
    ...
}

Could anyone give me a hand?

Much Thanks!

Best,Shuo

+1  A: 

First

// Shouldn't this be in a FrontController class?
addCommand(WindowBControl.EVENT_POPUPWindowB,WindowBCommand);

I think you have to somehow call PopupManager.removePopup because the Flex 3 ActionScript reference states:

Pops up a top-level window. It is good practice to call removePopUp() to remove popups created by using the addPopUp() method. If the class implements IFocusManagerContainer, the window will have its own FocusManager so that, if the user uses the TAB key to navigate between controls, only the controls in the window will be accessed

Hey friend,First,WindowBControl inherits from FrontController.Second,I do call PopupManager.removePopup when closing windowB.
Shuo
+1  A: 

Does WindowBControl inherit from FrontController? If so, you're probably instantiating it more than once on accident. This:

<control:WindowBControl id='control1'>

is going to create an instance of this front controller. Since you've put this line of code in WindowA, you're going to create a new instance of this controller every time you create a new instance of WindowA. This will then result in commands being called once for each instance of the controller every time your event fires.

You should only instantiate front controllers where you're positive they will only be instantiated a single time. The main application mxml is a good place for this.

Tim
You're right,Tim!WindowBControl is instantiated more than once,while I debugging,I notice the execute method inside WindowBCommand is running more than once.And Your answer resolves my question.I put <control:WindowBControl id='control1'> inside the main application,it works correctly.Thanks a million!Maybe I have some misunderstanding on Cairngorm.Usually I put the FrontController in the same mxml with the action dispatching the CairngormEvent,I thought the FrontController would act quickly.Could the dispatching CairngormEvent be listened across the whole program?
Shuo
A: 

I find another solution.

In windowA's closing method,I write

private function onClose():void
{
     PopUpManager.removePopUp(this);
     if(CairngormEventDispatcher.getInstance().hasEventListener(WindowBControl.EVENT_POPUPWindowB))
     {
         control1.removeCommand(WindowBControl.EVENT_POPUPWindowB);
     }

}
Shuo