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