Synchronous dialogs in Flex

apache-flexdialogfunction-callssynchronous

How can I open a synchronous dialog in Flex? I need to call a function from an External Interface (JavaScript) that will open a simple dialog in the Flex application and returns an value according to the button the user has clicked (OK/Cancel).

So it should by a synchronous call to a dialog, i.e. the call waits until the user has closed the dialog like this.

//This function is called by JavaScript
function onApplicationUnload():Boolean
{
  var result:Boolean;
  result = showDialogAndWaitForResult();
  return result
}

Does anybody know how I can do this? I could write a loop that waits until the dialog has set a flag and then reads the result to return it, but there must be something that is way more elegant and reusable for waiting of the completion of other asynchronous calls.

EDIT:
Unfortunately a callback does not work as the JavaScript function that calls onApplicationUnload() itself has to return a value (similar to the onApplicationUnload() function in Flex). This JavaScript function has a fixed signature as it is called by a framework and I cannot change it. Or in other words: The call from JavaScript to Flex must also be synchronous.

Best Answer

Flex doesn't work in a synchronous fashion, as it is a single thread application and so needs your code to hand execution back to the "core" in order to handle user input etc.

The way to do it is to make your dialogue's behaviour asynchronous:

function onApplicationUnload():void
{
    showDialog(resultMethod);
}

function resultMethod(result:Boolean):void
{
    ExternalInterface.call("javaScriptCallback", [result]);
}
Related Topic