是否可以将参数发送到closeHandler警报功能? 函数获取的fisrt参数是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);"/>
谢谢!
答案 0 :(得分:13)
方法可能是在警报创建范围内创建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);
}
答案 1 :(得分:11)
这应该可以使用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);"/>
答案 2 :(得分:10)
我处理此用例的典型方法是将数据添加到“警报”表单。例如
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;
然后在Alert
的关闭处理程序中private function OnAlertResult(event:CloseEvent):void {
trace(event.target.data);
}
答案 3 :(得分:2)
我通常使用匿名函数来包装带参数的函数调用:
Alert.show("Are you sure?", Alert.YES | Alert.CANCEL, null, function(event:CloseEvent):void{doSomething(event.detail, param1, param2);}, null, Alert.CANCEL)