Prompting User on Close of Application: Flex Way

来源:互联网 发布:trados翻译软件多少钱 编辑:程序博客网 时间:2024/06/15 16:35

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:
  1. <!– index.template.html –>
  2. <script language=”JavaScript” type=”text/javascript”>
  3. <!–
  4. // Give user a chance to save modified data
  5. window.onbeforeunload = function() {
  6. var warning=”";
  7. var fxControl = document.${application} || window.${application};
  8. if (typeof fxControl.getUnsavedDataWarning==”function”) {
  9. warning = fxControl.getUnsavedDataWarning();
  10. }
  11. if (warning!=”)
  12. return warning;
  13. else
  14. return;
  15. }
  16. // –>
  17. </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:

 

  1. import flash.external.ExternalInterface;
  2. private const UNSAVED_DATA_WARNING:String = ‘You have unsaved changes. You will lose them if you continue.’;
  3. if ( ExternalInterface.available ) {
  4. ExternalInterface.addCallback(
  5. “getUnsavedDataWarning”,
  6. function():String {
  7. if (commitRequired) return UNSAVED_DATA_WARNING;
  8. else return ”;
  9. }
  10. );
  11. }

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

 

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
  3. layout=”vertical” creationComplete=”onCreationComplete()”>
  4. <mx:Script><![CDATA[
  5. import flash.external.ExternalInterface;
  6. private const UNSAVED_DATA_WARNING:String = 'You have unsaved changes. You will lose them if you continue.';
  7. [Bindable] private var commitRequired:Boolean;
  8. private function onCreationComplete():void {
  9. if ( ExternalInterface.available ) {
  10. ExternalInterface.addCallback(
  11. “getUnsavedDataWarning”,
  12. function():String {
  13. if (commitRequired) return UNSAVED_DATA_WARNING;
  14. else return ”;
  15. }
  16. );
  17. }
  18. }
  19. ]]></mx:Script>
  20. <mx:Text text=”Type something below to test” />
  21. <mx:TextInput id=”input” change=”commitRequired=(input.text!=”)”/>
  22. <mx:Text text=”Close window and you will{commitRequired?’ ‘:’ not’} be prompted” />
  23. </mx:Application>

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

原创粉丝点击