Prompting User on Close of Application: Flex Way

WM_CLOSE, windowClosing, closeQuery… Which one is closer to your heart? One way or the other you
have to give your user that last chance to save modified data when he or she is closing the application
window. Providing the same functionality in Flex requires help on the JavaScript/HTML side, so this post
will illustrate two techniques in one example.

Preventing the user from closing the browser window has been traditionally done via onbeforeunload event.
Here is the snippet of JavaScript code, which, once you put it in
./html-template/index.template.html will (IE & Mozilla)

  1. resolve reference to Flash control;
  2. tentatively invoke ActionScript method getUnsavedDataWarning() ;
  3. force browser to popup the alert “Are you sure you want to navigate away? ….. OK/Cancel”; with your custom text in the middle:

<!– index.template.html –>
. . . . .
<script language=”JavaScript” type=”text/javascript”>
<!–
// Give user a chance to save modified data
window.onbeforeunload = function() {
var warning=””;
var fxControl = document.${application} || window.${application};
if (typeof fxControl.getUnsavedDataWarning==”function”) {
warning = fxControl.getUnsavedDataWarning();
}
if (warning!=”)
return warning;
else
return;
}
// –>
</script>
Now, all of this is in vain, of course, if your Flex application does not advertise the method
getUnsavedDataWarning(). And here is where Flash ExternalInterface API comes very handy. You can
expose any method: static, instance or anonymous, like the one below:

import flash.external.ExternalInterface;

private const UNSAVED_DATA_WARNING:String = ‘You have unsaved changes. You will lose them if you continue.';

if ( ExternalInterface.available ) {
ExternalInterface.addCallback(
“getUnsavedDataWarning”,
function():String {
if (commitRequired) return UNSAVED_DATA_WARNING;
else return ”;
}
);
}

The complete MXML application is listed below. Do not forget to paste the JavaScript stuff in ./html-template/index.template.html:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”vertical” creationComplete=”onCreationComplete()”>
<mx:Script><![CDATA[

import flash.external.ExternalInterface;
private const UNSAVED_DATA_WARNING:String = ‘You have unsaved changes. You will lose them if you continue.';
[Bindable] private var commitRequired:Boolean;

private function onCreationComplete():void {
if ( ExternalInterface.available ) {
ExternalInterface.addCallback(
“getUnsavedDataWarning”,
function():String {
if (commitRequired) return UNSAVED_DATA_WARNING;
else return ”;
}
);
}
}
]]></mx:Script>
<mx:Text text=”Type something below to test” />
<mx:TextInput id=”input” change=”commitRequired=(input.text!=”)”/>
<mx:Text text=”Close window and you will{commitRequired?’ ‘:’ not’} be prompted” />

</mx:Application>

WM_CLOSE, windowClosing, closeQuery, onbeforeunload… Tell me who your friends are … ; )

Victor

One thought on “Prompting User on Close of Application: Flex Way

  1. The Javascript contains slightly irregular quote characters. var warning=””;

Comments are closed.