Flex - 如何在一个组件中定义另一个组件的功能?

时间:2010-02-01 17:38:16

标签: flex function components dynamic redefine

我是一个完整的Flex / Flash菜鸟,运行Adobe Flash Builder 4 Beta 2.我有一个主要组件需要能够调用几个弹出窗口,除了一个功能和一些标签之外,每个弹出窗口大致相同。显然,我更喜欢能够在调用弹出窗口时定义此函数并更改这些标签,而不是使用几乎相同代码的大量.mxml文件,我只是不知道该怎么做。我想出了如何更改标签,但不知道如何重新定义功能。

为简单起见,让我们说我的代码如下:

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>

现在说我想在main.mxml中调用它时更改popup.mxml中的test() ...我该怎么做?请详细说明......记住我是菜鸟: - )

2 个答案:

答案 0 :(得分:1)

当然,在发布问题之后,这个想法立即浮现在我脑海中。我想我会在这里发布解决方案而不是仅仅删除问题,以防它帮助其他任何人。

我刚从popup.mxml中取出test()并在main.mxml中修改了init(),如下所示:

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"); });
}

答案 1 :(得分:0)

如果我得到你要求的东西,你可以让test()成为一个函数变量并赋予它公共访问权限,以便其他组件可以改变它。

    <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>

现在在另一个组成部分:

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.