Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex: Sending parameters to Alert closeHandler

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!

like image 493
cili Avatar asked Feb 21 '11 21:02

cili


2 Answers

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);
}
like image 147
Christophe Herreman Avatar answered Nov 15 '22 20:11

Christophe Herreman


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);"/>
like image 31
Jason Towne Avatar answered Nov 15 '22 20:11

Jason Towne