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:
  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 … ; )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值