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)
- 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 … ; )
- Prompting User on Close of Application: Flex Way
- MySQL: Forcing close of thread **** user:''
- Forcing close of thread 1395 user: 'senke'
- Flex 4 is on the way
- Application.Close
- Mysql:Forcing close of thread 756 user: 'root'
- Mysql:Forcing close of thread 756 user: 'root' 的解决
- The proper way of implementing user login system(mark learn)
- Deploying Flex on WebSphere Application Server
- Checking the close event of browser in flex
- RFC3117 - On the Design of Application Protocols
- Configure User Profile Service Application on SharePoint Server 2013
- Enabling User-Controlled Collection of Application Crash Data With DTrace
- Building application Interleaved way
- clearcase on the way!
- Notes on the Way
- On the way
- Start,on my way.
- 以文本流输出griview到客户端
- 如何算是掌握了一门编程语言
- C#Windows程序的生与死
- Google推广需要知道的细节
- LR的COM/DCOM协议时,关于ADO相兼容问题?
- Prompting User on Close of Application: Flex Way
- SQL DateTime 处理资料
- 有新博了
- 一个企业员工对大学毕业生的衷告
- joseph
- html向flash传递参数的例子
- CORBA公共对象请求代理体系结构
- 停车管理
- 常用FTP命令详解