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)
- resolve reference to Flash control;
- tentatively invoke ActionScript method getUnsavedDataWarning() ;
- 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 … ; )
本文介绍如何在Flex应用程序中使用JavaScript与Flash的外部接口API结合的方式,为用户提供关闭窗口前保存修改数据的机会。通过定义`getUnsavedDataWarning`方法并在浏览器`onbeforeunload`事件中调用,确保用户不会意外丢失数据。
358

被折叠的 条评论
为什么被折叠?



