Flex - 如何在一个组件中定义另一个组件的函数?

发布于 2024-08-20 01:29:15 字数 2135 浏览 5 评论 0原文

我是一名 Flex/Flash 菜鸟,正在运行 Adob​​e 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 时更改 popup.mxml 中的 test()...我该怎么做?请提供详细信息...记住我是菜鸟:-)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-08-27 01:29:16

如果我明白您的要求,您可以将 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.

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.
向地狱狂奔 2024-08-27 01:29:15

当然,在发布问题后,这个想法立即出现在我的脑海中。我想我应该在这里发布解决方案,而不是仅仅删除问题,以防它对其他人有帮助。

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

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"); });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文