Stefan Kendall
Stefan Kendall

Reputation: 67892

Flex Popup Window: Stack Overflow?

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?

Upvotes: 2

Views: 5987

Answers (5)

Peter Z
Peter Z

Reputation: 21

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.

Upvotes: 1

Ced
Ced

Reputation: 142

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.

Upvotes: 1

Jacob
Jacob

Reputation: 78920

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.

Upvotes: 1

deanWombourne
deanWombourne

Reputation: 38485

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>

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245499

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.

Upvotes: 0

Related Questions