问题描述
我有一个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.SEND_EVENTS
由一个伴侣EventMap定义。所有的处理程序是调用一个HTTPServiceInvoker
。在调试控制台,我能看到的处理程序和HTTPServiceInvoker被触发,但无论是resultHandlers
还是faultHandlers
被称为。我知道这个事件处理程序,因为当我分派同一CustomEvent.SEND_EVENTS
在一个按钮单击处理程序没有问题,它的行为完全如我所料) - 在浏览器似乎等待清理函数来完成关闭之前。 (所有的痕迹印的浏览器关机前)
CustomEvent
triggered, but not executed.
>> EventHandler forCustomEvent.SEND_EVENTS
is defined by a Mate EventMap. All the handler does is to call anHTTPServiceInvoker
. In debug console, I'm able to see the handler and HTTPServiceInvoker being triggered, but neither theresultHandlers
nor thefaultHandlers
were called. I know this event handler has no problem because when I dispatch the sameCustomEvent.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);
}
}
我的设置
- 弹性生成3
- 配合MVC框架(Mate_08_9.swc)
- flash播放器10
My Setup
- FlexBuilder 3
- Mate MVC Framework (Mate_08_9.swc)
- FlashPlayer 10
推荐答案
不幸的是,在做这样清理的功能,异步执行的没有坚实的方法。该
结果
/故障
中的HTTPService的事件清理后异步发生
返回方法。浏览器只能等待,直到onbeforeunload的
函数(JS * CLEAN_UP *功能)的回报。除非你调用事件。preventDefault()
从功能,页面将关闭。请注意,调用的 preventDefault()的会导致确定/取消弹出问: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 thecleanUp
method is returned. The browser waits only till theonbeforeunload
function (the js *clean_up* function) returns. Unless you callevent.preventDefault()
from that function, the page will be closed. Note that calling preventDefault() will result in an ok/cancel popup asking:您确定要离开此页?
preSS确定继续,或取消留在当前页面。
Press OK to continue, or Cancel to stay on the current page.
如果用户选择OK,浏览器将仍然关闭。您可以使用
event.returnValue
属性添加自定义消息,POPOP。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清除功能的浏览器是由用户关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!