在用户关闭浏览器时执行 Flex 清理功能 [英] To execute Flex cleanup function when browser is closed by user

查看:25
本文介绍了在用户关闭浏览器时执行 Flex 清理功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Flex 客户端应用程序.当用户关闭浏览器时,我需要一个清理功能在 Flex 中运行.我在网上找到了以下解决方案,但它只对我有用.我怎么能修好呢?提前感谢您的回复!

I have a Flex client application. I need a clean up function to run in Flex when the user closes the browser. I found the following solution on the net, but it only works half-way for me. How could I fix it? Thanks in advance for any responses!

  • CustomEvent 已触发,但未执行.
    >> CustomEvent.SEND_EVENTS 的 EventHandler 由 Mate EventMap 定义.处理程序所做的只是调用 HTTPServiceInvoker.在调试控制台中,我能够看到处理程序和 HTTPServiceInvoker 被触发,但是 resultHandlersfaultHandlers 都没有被调用.我知道这个事件处理程序没有问题,因为当我在按钮单击处理程序中调度相同的 CustomEvent.SEND_EVENTS 时,它的行为与我预期的完全一样)
  • 浏览器似乎在关闭之前等待清理功能完成.(所有痕迹都在浏览器关闭之前打印)
  • CustomEvent triggered, but not executed.
    >> EventHandler for CustomEvent.SEND_EVENTS is defined by a Mate EventMap. All the handler does is to call an HTTPServiceInvoker. In debug console, I'm able to see the handler and HTTPServiceInvoker being triggered, but neither the resultHandlers nor the faultHandlers were called. I know this event handler has no problem because when I dispatch the same CustomEvent.SEND_EVENTS in a button click handler, it behaves exactly as I expected)
  • Browser seems to wait for cleanUp function to complete before it closes. (all traces were printed before browser closes down)

我在 index.template.html 中添加了以下内容

I added the following into the index.template.html

window.onbeforeunload = clean_up;

function clean_up()
{
 var flex = document.${application} || window.${application};
 flex.cleanUp();
}

并在应用程序 MXML 文件中使用以下内容

And used the following in the application MXML file

import flash.external.ExternalInterface;

public function init():void {
ExternalInterface.addCallback("cleanUp",cleanUp);
}

public function cleanUp():void {   

   var newEvent:CustomEvent = new CustomEvent(CustomEvent.SEND_EVENTS);
   newEvent.requestObj = myFormModel;

   dispatchEvent(newEvent);

   // for testing purposes
   // to see whether the browser waits for Flex cleanup to finish before closing down   
   var i:int;
   for (i=0; i<10000; i++){
        trace(i);
   }    

}

我的设置

  • FlexBuilder 3
  • Mate MVC 框架 (Mate_08_9.swc)
  • FlashPlayer 10

    My Setup

    • FlexBuilder 3
    • Mate MVC Framework (Mate_08_9.swc)
    • FlashPlayer 10

      推荐答案

      不幸的是,没有可靠的方法来执行这种异步执行的清理函数.HTTPService的result/fault事件在cleanUp方法返回后异步发生.浏览器只等待 onbeforeunload 函数(js clean_up 函数)返回.除非您从该函数调用 event.preventDefault(),否则页面将被关闭.请注意,调用 preventDefault() 将导致一个 ok/cancel 弹出窗口询问:

      Unfortunately, there is no solid way of doing such clean up functions that execute asynchronously. The result/fault events of the HTTPService occur asynchronously after the cleanUp method is returned. The browser waits only till the onbeforeunload function (the js clean_up function) returns. Unless you call event.preventDefault() from that function, the page will be closed. Note that calling preventDefault() will result in an ok/cancel popup asking:

      您确定要离开此页面吗?

      Are you sure you want to navigate away from this page?

      按确定继续,或按取消留在当前页面.

      Press OK to continue, or Cancel to stay on the current page.

      如果用户选择确定,浏览器仍将关闭.您可以使用 event.returnValue 属性向弹出窗口添加自定义消息.

      If the user selects OK, the browser will be closed nevertheless. You can use the event.returnValue property to add a custom message to the popop.

      //tested only in Firefox
      window.addEventListener("beforeunload", onUnload, false);
      function onUnload(e)
      {
         e.returnValue = "Some text that you want inserted between " +
           "'Are you sure' and 'Press OK' lines";
         e.preventDefault();
      }
      

      这篇关于在用户关闭浏览器时执行 Flex 清理功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆