如何更改/确定 Flex 中函数执行的优先级?

发布于 2024-12-07 09:42:28 字数 640 浏览 1 评论 0原文

所以基本上我有一个已调度事件的组件:

<components:MyComp id="Id" myDispatchedEvent(event)/>

在脚本标记中我有该函数:

private function myDispatchedEvent(event:Event):void  
{  
  //Here I have my static function with title and handler function showConfirmation 
      Calculate.showConfirmation("String Title", function(event:Close):void  
      {  
          if(bla bla bla)  
          //lots of code etc. ...  
      });  
//myDispatchEvent function continues here..  
}

所以问题出在我的静态函数的 showConfirmation 处理程序上,如果我进行调试,它只会跳过该函数并继续执行 myDispatchedEvent。为什么 showConfirmation 函数中的匿名函数不执行?
谢谢

So basically I have a component with my event dispatched:

<components:MyComp id="Id" myDispatchedEvent(event)/>

In script tags I have that function:

private function myDispatchedEvent(event:Event):void  
{  
  //Here I have my static function with title and handler function showConfirmation 
      Calculate.showConfirmation("String Title", function(event:Close):void  
      {  
          if(bla bla bla)  
          //lots of code etc. ...  
      });  
//myDispatchEvent function continues here..  
}

So problem is with my static function's showConfirmation handler, if I go through debug, it just skips that function and continues doing myDispatchedEvent. Why doesn't anonymous function inside showConfirmation function execute?
Thanks

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

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

发布评论

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

评论(2

污味仙女 2024-12-14 09:42:28

函数在调用时执行。就你而言,你只需声明它。在 Calculate.showConfirmation 内的某处调用此函数,它将被执行。

像下面这样:

public class Calculate
{
    public static function showConfirmation(title:String, func:Function):void
    {
        // The call I'm talking about is here
        func(new Close());
    }
}

Functions are executing upon call. In your case you have just declaration of it. Call this function somewhere inside Calculate.showConfirmation and it will be executed.

Something like the following:

public class Calculate
{
    public static function showConfirmation(title:String, func:Function):void
    {
        // The call I'm talking about is here
        func(new Close());
    }
}
涫野音 2024-12-14 09:42:28

首先我要说的是,你想做的事情很奇怪。我会尝试编写不同的解决方案,但这取决于您想要做什么。如果您告诉我们更多相关信息,我们可以找到更好的方法来实现您的目标。顺便说一句,你可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"
               minHeight="600" minWidth="955">
    <fx:Script>
        <![CDATA[
            import mx.events.CloseEvent;

            public static function myFunction(param:String, func:Function):void {
                trace("executing");
                func.apply();
            }

            protected function labelx_clickHandler(event:MouseEvent):void {
                trace("click");
                Tests.myFunction("Test", function():void {
                    if (event.localX > 0) {
                        trace("Test");
                    }
                    else {
                        trace("No");
                    }
                });

            }
        ]]>
    </fx:Script>
    <s:Button id="labelx"
              label="Click me"
              click="labelx_clickHandler(event)"/>
</s:Application>

类似于康斯坦丁已经告诉过你的事情。如果您不执行作为静态函数内的参数传递给静态函数的函数,则该函数将不会被执行。

Let me say first that what you're trying to do is quite strange. I'd try to code a different solution, but this depends on what you're trying to do. It you tell us a more about it we could find a better way to reach your goal. BTW, you can do something like this:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"
               minHeight="600" minWidth="955">
    <fx:Script>
        <![CDATA[
            import mx.events.CloseEvent;

            public static function myFunction(param:String, func:Function):void {
                trace("executing");
                func.apply();
            }

            protected function labelx_clickHandler(event:MouseEvent):void {
                trace("click");
                Tests.myFunction("Test", function():void {
                    if (event.localX > 0) {
                        trace("Test");
                    }
                    else {
                        trace("No");
                    }
                });

            }
        ]]>
    </fx:Script>
    <s:Button id="labelx"
              label="Click me"
              click="labelx_clickHandler(event)"/>
</s:Application>

Something similar what Constantiner has already told you. If you don't execute the function that you're passing to your static function as a parameter inside this static function, it won't be executed.

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