Flex: Sending parameters to Alert closeHandler

2019-01-26 08:42发布

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?

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

Thank you!

4条回答
Bombasti
2楼-- · 2019-01-26 09:07

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);
}
查看更多
Emotional °昔
3楼-- · 2019-01-26 09:09

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)
查看更多
We Are One
4楼-- · 2019-01-26 09:10

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);"/>
查看更多
叼着烟拽天下
5楼-- · 2019-01-26 09:12

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);
}
查看更多
登录 后发表回答