Prompting User on Close of Application: Flex Way

本文介绍如何在Flex应用程序中使用JavaScript与Flash的外部接口API结合的方式,为用户提供关闭窗口前保存修改数据的机会。通过定义`getUnsavedDataWarning`方法并在浏览器`onbeforeunload`事件中调用,确保用户不会意外丢失数据。

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

### Program of Thoughts Prompting 方法中的计算与推理分离 在数值推理任务中,Program of Thoughts (PoT) Prompting 方法通过生成可执行的编程代码来表示逻辑推理步骤,而不是直接提供完整的解决方案或计算路径。这种方法允许将复杂的推理过程分解成更易于管理和验证的小部分[^1]。 具体而言,在处理数值推理问题时,PoT 提出了一个框架,其中推理被编码为一系列可以由外部工具(如Python解释器)执行的具体操作指令。这意味着实际的算术运算和其他类型的数值处理是由专门设计用于此类任务的语言特性来承担的,而模型本身则专注于构建正确的算法结构和流程控制语句[^2]。 这种做法不仅简化了模型内部的工作负载,还使得整个系统的灵活性大大增加——因为只要改变所使用的脚本语言或者引入新的库函数就可以轻松扩展功能范围。此外,由于最终产生的是一段清晰易读且能够独立运行的源码文件,因此也便于后续的人工审查以及错误排查工作。 ```python def calculate_interest(principal, rate, time): """Calculate compound interest using the formula A=P(1+r)^t""" amount = principal * ((1 + rate) ** time) return round(amount, 2) # Example usage demonstrating separation between logic and computation logic_description = """ Given a principal P=1000 dollars at an annual interest rate r=0.05 over t=3 years, we want to find out how much money will accumulate after this period. """ print(f"{logic_description}\nThe accumulated value is ${calculate_interest(1000, 0.05, 3)}.") ``` 上述例子展示了如何利用 Python 函数 `calculate_interest` 来封装具体的利息计算细节,而在描述性的字符串变量 `logic_description` 中仅保留高层次的任务说明。这种方式体现了 PoT 如何有效地实现了计算过程同推理表述之间的解耦合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值