在何处监听POPUP派发的事件

1283人阅读 评论(2) 收藏 举报

 Buble up Events向上传播的冒泡事件

In Adobe Flex it is easy to make a event bubble, which means that if a DisplayObject dispatches an event, the event will be re-dispatched by each parent of the DisplayObject until it reaches the root of the application.

This behavior is really useful.  One common function that I use this behavior for is handling the navigation within my applications.  I attach an event listener to the singleton application instance (Application.application) for navigation events, and handle them within the main application class.  This means that any control, anywhere in the application, can easily navigate to any other part of the application through one line of code ( dispatchEvent( new NavivationEvent(….) ).

大家都知道Event是沿着对象的继承树向上传播的,这部分就是讲这是个相当好的特性,当然是很好的特性,我们所有的人大概都爱死这个特性了,看看我们的作者碰到了什么问题.

The Problem问题
Bubbling events dispatched from popups within the Flex application are not heard by a listener attached to the application instance.  This is

 从弹出框派发的事件无法被添加在application对象的监听器捕获。

because DisplayObjects created through the PopupManager.createPopup() function are not children of the application container. How then, can

 因为,从PopupManager.createPopup() 创建的显示对象DisplayObjects 不是application的子类,按照冒泡事件特性,当然添加在application对象的监听器就没法捕获此事件了

 we cleanly handle events coming out of popups so that we still only have one pattern for handling navigation within the application?

 

The Solution
To solve this problem, we need to know a little bit about the structure of a Flex application as shown in the diagram below:

要解决这个问题,我们需要了解一些FLEX的框架,如下图所示:

Flex Application Structure

In this diagram we can clearly see that popups within a Flex application are not children of the application container, but are instead children of the SystemManger. 从这张图,我们可以清楚的看出,FLEX应用程序的弹出框不是application容器的子类,而是systemmananger的子类

I wont go into the functions of the system manager here, but every UIComponent contains a reference to the system manager of the current application so its easy to get a reference to it. 当然我在这里不会详述任何系统的函数,但是必须指出,每一个UICOMPONENT都包含了一个到当前SYSMANAGER的引用,因此很容易就能得到当前SYSMANAGER的引用。

This means that instead of attaching my event listener to:这意味着我们可以这样的挂接一个监听器
Application.application (不是这儿)

I can instead attach my listener to:挂接到这儿
Application.application.systemManager 挂接到这儿

Then my listener will hear all bubbling events within the system, regardless of whether they originate within a Popup or a standard UIComponent.

Problem solved. 这样在POPUP里冒泡出的事件就能被监听到了。

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:413336次
    • 积分:6688
    • 等级:
    • 排名:第3498名
    • 原创:218篇
    • 转载:17篇
    • 译文:19篇
    • 评论:68条
    文章分类
    最新评论
    RIA的朋友们
    我的站点