views:

31

answers:

1

Within an event handler I have an Alert.show(...) that prompts a user for confirmation. How can I capture the selection of the alert prompt and use it within the event handler. For example:

private function mainEvtHandler(event:DynamicEvent):void {
 var alert:Alert = Alert.show("Are you sure?", "Confirmation", Alert.YES|Alert.NO, this, alertHandler);
 // How can I retrieve the selection and use it within this event handler?
 // i.e. if (alert == Alert.Yes) { ...
 var index:int = arrayColl.getItemIndex(event.data)
 ...
 ...
A: 

You can declare the alertHandler as nested function ...

private function mainEvtHandler(event:DynamicEvent):void {

  var alertResult: int = -1;

  function alertHandler(evt:CloseEvent):void {
     alertResult = evt.detail;
  }

  var alert:Alert = Alert.show("Are you sure?", "Confirmation", Alert.YES|Alert.NO, this, alertHandler);
  if (alertResult == Alert.Yes) {
     var index:int = arrayColl.getItemIndex(event.data);
  ...

}

... or you can use an anonymous function

private function mainEvtHandler(event:DynamicEvent):void {
  Alert.show("Are you sure?", "Confirmation", Alert.YES|Alert.NO, this, 
      function (nestedCloseEvent:CloseEvent):void {
         if (nestedCloseEvent.detail == Alert.Yes) {
            var index:int = arrayColl.getItemIndex(event.data);
            ...
         }
      }
  );
}
splash
Don't foget that alertHandler can also be it's own individual function, defined outside of mainEvtHandler.
Wade
@Wade, I think the root of the matter is the direct access to `event.data` which could be made global and handled outside, but I guess that's what the OP wants to spare.
splash
Yes, my preference is to keep the "event" var local to the handler rather than making it global. Thanks.
MarkPenn