Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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>

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top