【问题标题】:Flex: Sending parameters to Alert closeHandlerFlex:向警报 closeHandler 发送参数
【发布时间】:2011-07-01 14:01:35
【问题描述】:

是否可以向 closeHandler Alert 函数发送参数? 函数获取的第一个参数是CloseEvent,但是如何发送另一个呢?

<s:Button id="btnLoadLocalData" label="Load data"
          click="Alert.show('Populate list with local data?', '', Alert.YES | Alert.CANCEL, this, loadLocalData(???parameters???), null, Alert.OK);"/>

谢谢!

【问题讨论】:

    标签: apache-flex parameters alert send


    【解决方案1】:

    一种方法可能是在警报创建范围内创建 closeHandler。

    这是一个例子:

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

    【讨论】:

    • 很棒的使用闭包和作用域来很好地提取一个急需的技巧
    【解决方案2】:

    这应该可以使用 Flex 的动态函数构造来实现。类似的问题被问到here

    这是一个例子:

    参数和处理程序:

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

    您的组件:

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

    【讨论】:

    • 正是我需要的!我想尽可能减少处理函数的数量。谢谢!
    【解决方案3】:

    我处理此用例的典型方法是将数据添加到警报表单。例如

    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;
    

    然后在警报的关闭处理程序中

    private function OnAlertResult(event:CloseEvent):void {
        trace(event.target.data);
    }
    

    【讨论】:

      【解决方案4】:

      我通常使用匿名函数来包装带参数的函数调用:

      Alert.show("Are you sure?", Alert.YES | Alert.CANCEL, null, function(event:CloseEvent):void{doSomething(event.detail, param1, param2);}, null, Alert.CANCEL)
      

      【讨论】:

        猜你喜欢
        • 2023-01-28
        • 2022-06-18
        • 2022-07-13
        • 1970-01-01
        • 2021-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多