开发者

How to change/prioritize function execution in flex?

开发者 https://www.devze.com 2023-04-09 03:32 出处:网络
So basically I have a component with my event dispatched: <components:MyComp id=\"Id\" myDispatchedEvent(event)/>

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", functio开发者_如何学Gon(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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消