Flex: Sending parameters to Alert closeHandler

后端 未结 4 2119
天涯浪人
天涯浪人 2020-12-19 03:37

Is it possible to send parameters to a closeHandler Alert function? The fisrt parameter the function gets is the CloseEvent, but how to send another one?

<         


        
相关标签:
4条回答
  • 2020-12-19 03:54

    An approach might be to create the closeHandler in the scope of the alert creation.

    Here's an example:

    <s:Button id="btnLoadLocalData" label="Load data" click="btnLoadLocalData_clickHandler(event)"/>
    
    function btnLoadLocalData_clickHandler(event:Event):void {
      var someVar:Object = someCalculation();
      var closeHandler:Function = function(closeEvent:CloseEvent):void {
        // someVar is available here
      };
      Alert.show('Populate list with local data?', '', Alert.YES | Alert.CANCEL, this, closeHandler, null, Alert.OK);
    }
    
    0 讨论(0)
  • 2020-12-19 03:58

    This should be possible using Flex's dynamic function construction. A similar question was asked here.

    Here's an example:

    The parameters and handler:

    var parameters:String = "Some parameter I want to pass";
    
    private function loadLocalData(e:Event, parameter:String):void
    {
      // voila, here's your parameter
    }
    
    private function addArguments(method:Function, additionalArguments:Array):Function 
    {
      return function(event:Event):void {method.apply(null, [event].concat(additionalArguments));}
    }
    

    Your component:

    <s:Button id="btnLoadLocalData" label="Load data"
              click="Alert.show('Populate list with local data?', '', Alert.YES | Alert.CANCEL, this, addArguments(loadLocalData, [parameters])), null, Alert.OK);"/>
    
    0 讨论(0)
  • 2020-12-19 03:58

    I usually use an anonymous function to wrap a function call with parameters:

    Alert.show("Are you sure?", Alert.YES | Alert.CANCEL, null, function(event:CloseEvent):void{doSomething(event.detail, param1, param2);}, null, Alert.CANCEL)
    
    0 讨论(0)
  • 2020-12-19 04:03

    My typical method of handling this use case is to add the data to the Alert form. For example

    var o:Object = new Object();
    o["stuff"] = "quick brown fox";
    
    var alert:Alert = Alert.show("Message", "Title", mx.controls.Alert.YES | mx.controls.Alert.NO, null, OnAlertResult);
    alert.data = o;
    

    And then in the close handler for the Alert

    private function OnAlertResult(event:CloseEvent):void {
        trace(event.target.data);
    }
    
    0 讨论(0)
提交回复
热议问题