views:

1990

answers:

5

I'm somehow creating a stack overflow in Flex 3...I'm trying to get data out of a modal dialogue window as such:

Main application:

var myPopup:MyPopup;

function buttonClick( event:MouseEvent ):void
{
myPopup = MyPopup( PopUpManager.createPopUp( this, MyPopUp, true ) );
myPopup.addEventListener( CloseEvent.CLOSE, handler, false, 0, true );
}

function handler():void
{
//get data
}

MyPopup:

function buttonHandler( MouseEvent:event ):void
{
PopUpManager.remove( this );
this.dispatchEvent( new CloseEvent( CloseEvent.CLOSE ) );
}

If this is improper, what is the correct way to handle closing of the popup in a manner that allows me to use and retrieve data on the object?

A: 

Not absolutely certain on how the PopUpManager behaves, but you might want to switch the statements in your buttonHandler:

function buttonHandler(MouseEvent:event):void
{
    this.dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
    PopUpManager.remove(this);
}

The popup will stay up while your event code is running, but it should take care of the situation where your popup object is being disposed before you fire the code that tries to get data from it.

Justin Niessner
Still get the stack overflow.
Stefan Kendall
A: 

Hi,

I've recreated your code and it works fine for me :( This means that either I've misunderstood your problem or the bug is somewhere else in your code.

Any chance that you can post some more details about the problem?

Sam

PS Here is the code I used to test with :

Application.mxml :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <mx:Button x="10" y="10" label="Button" click="buttonClick(event)" id="popupButton"/>

    <mx:Script>
     <![CDATA[
      import mx.core.IFlexDisplayObject;
      import mx.managers.PopUpManager;

      private var popup:Popup;

      private function buttonClick(e:MouseEvent):void {
       popup = PopUpManager.createPopUp(this, Popup, true) as Popup;
       popup.addEventListener(Event.CLOSE, popupClose, false, 0, true);
      }

      private function popupClose(e:Event):void {
       trace(popup);
       popupButton.label = "Closed";
      }
     ]]>
    </mx:Script>

</mx:Application>

Popup.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
    <mx:Button x="167" y="123" label="Close me" click="buttonClick(event)"/>

    <mx:Script>
     <![CDATA[
      import mx.managers.PopUpManager;

      private function buttonClick(e:MouseEvent):void {
       dispatchEvent(new Event(Event.CLOSE));
       PopUpManager.removePopUp(this);
      }
     ]]>
    </mx:Script>

</mx:Canvas>
deanWombourne
This was somehow a stack overflow for me. I switched to handling everything in the main application with a custom event and closing the popup there, and my stackoverflow disappeared.
Stefan Kendall
A: 

Perhaps you could try adding an event parameter to your handler. I'm not so sure that ActionScript can always tolerate that not being provided. Example:

function handler(event:CloseEvent):void {
    // Handle away
}

I also second the practice of calling the handler before dismissing the popup as mentioned by Justin.

Jacob
A: 

In your sample, move PopUpManager.removePopUp(this); to the close event handler, i.e., popupClose(e:Event). You'll also need to replace the argument this with popup.

Peter Z
A: 

You also need to create a dispose function to clean event, models etc... in your pop up. Otherwise it will not be garbage collected and slow down your app.

Ced