The Way To Great



 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(….) ).


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


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 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里冒泡出的事件就能被监听到了。

下一篇FLEX 中英文姓名校验器 VALIDATOR
想对作者说点什么? 我来说一句