views:

348

answers:

2

I'm a complete Flex/Flash noob, running Adobe Flash Builder 4 Beta 2. I have a main component that needs to be able to call several popups, each mostly the same except for one function and a few labels. Obviously, I'd prefer to be able to define this function and change those labels when calling the popup instead of having tons of .mxml files with nearly the same code, I just don't know how to do it. I figured out how I can change the labels, but not sure how to redefine the function.

For simplicity's sake, let's say my code looks like this:

main.mxml:

<?xml version="1.0" encoding="utf-8"?>
    <mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" :s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="init()">
        <fx:Script>
            <![CDATA[
                import mx.controls.Alert;
                import mx.managers.PopUpManager;

                protected function init():void
                {
                    var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true);
                    PopUpManager.centerPopUp(alertWindow);
                    var popInstance:transmitRoundPop = alertWindow as transmitRoundPop;
                    popInstance.btnTest.label = "NEW";
                }
            ]]>
        </fx:Script>
    </mx:Module>

popup.mxml:

<?xml version="1.0" encoding="utf-8"?>
    <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" width="400" height="300" xmlns:flash="services.flash.*">
        <fx:Script>
            <![CDATA[  
                import mx.controls.Alert;  
                import mx.managers.PopUpManager;  

                public function test():void
                {
                    Alert.show("ORIGINAL");
                    PopUpManager.removePopUp(this);
                }
            ]]>
        </fx:Script>
        <s:Panel x="10" y="10" width="380" height="280" title="Test" id="pnlTest">
                <s:Button x="131" y="104" label="OLD" id="btnTest" click="test()"/>
        </s:Panel>
    </s:Group>

Now say I want to change test() in popup.mxml when calling it in main.mxml...how do I do that? Please include detail...remember I'm a noob :-)

A: 

If I get what you're asking, you can make test() a function variable and give it public access so that other components can change it.

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

            // "test" is now a function variable, 
            // you change it just like any other variable, 
            // but you can call it as well, just like before.
            public var test:Function;

            public function defaultTest():void
            {
                Alert.show("ORIGINAL");
                PopUpManager.removePopUp(this);
            }

            protected function init():void
            {
            // Setting a default value for test
            // otherwise it would give you an error when calling
            // an unassigned function variable.
                this.test = defaultTest; 
                ...
            }
        ]]>
    </fx:Script>

Now in another component:

public function mainTest():void
{
    ...
}

...
myPopup.test = mainTest;  // setting the variable, note that there are no parentheses.
myPopup.test();   // running the function, note that there are now parentheses.
CookieOfFortune
I tried this out as well, but it didn't exactly work for me. The function is called when the user clicks a button in the popup, so the function needs to be called from popup.mxml, not main.mxml. Even after doing myPopup.test = mainTest it was still calling defaultTest(). I suspect (could be wrong) that it's because the popup has already been created at that point. I can't change the function before the popup is created because I need the popup to do it. Then again it's also possible that I just didn't do it right...your way makes a lot of sense to me as well.
Travesty3
Yes, I believe the PopupManager creates its own popup. I believe your code above it is part of alertWindow.
CookieOfFortune
+1  A: 

Of course, immediately after posting the question the idea pops into my head. I figured I'd post the solution here instead of just removing the question in case it helps anyone else.

I just took test() out completely from popup.mxml and modified init() in main.mxml to look like this:

protected function init():void
{
    var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true);
    PopUpManager.centerPopUp(alertWindow);
    var popInstance:transmitRoundPop = alertWindow as transmitRoundPop;
    popInstance.btnTest.label = "NEW";
    popInstance.btnTest.addEventListener("click", function():void { Alert.show("REDEFINED"); });
}
Travesty3