开发者

Handle flex events without using a framework?

I'm developing an AIR application with Flash Builder 4 Beta 2 (nightly SDK) an am not using a framework like mate. I have a couple of views and components in my application. An outline might look like this:

root
|- mainview
|-开发者_C百科- toolbar
|-- catalogue view
|--- tree
|--- datagrid
|-- statusbar

This is not 100% the structure of my project, but may suffice as an example. When the app get's initialized, the tree loads it's data which in my case is a list of categories. When the users clicks on an item in the tree, the data for the datagrid loads.

On every click on a tree item I dispatch an event. This event bubbles and carries the VO of the clicked item. Now I want my datagrid to catch this event. Currenty I register the event listener for this event like this (using a class and function):

FlexGlobals.topLevelApplication.addEventListener(eventName, this.eventHandler);

This code is executed in the datagrid's creation complete Listener. Somehow, this seems a bit buggy to mee (altough it works). Or is it a common technic to register a component's event listener at the top level application?


Yeah this is a perfectly good solution. Some of the frameworks add event listeners to the top most display object, like Mate and Swiz, so they can do exactly this. Check out the Source for Mate's GlobalDispatcher for just that. Swiz uses something like that, plus a Cairngorm-style CentralDispatcher for non-display object global events: Source for Swiz's CentralDispatcher.

If you want to listen for display object events globally, registering an event listener on the top level Application is best; anything else would make it too complicated to follow. The frameworks tend to just abstract all that away from you, through metadata (Swiz, Cairngorm 3, Parsley), and EventMaps (Mate).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜