tags:

views:

38

answers:

2

Lets consider following example, I have "Document" object which contains collection of "Person" objects and each "Person" object has collection of "Address" objects. When one of the "Address" object changes I need to call server-side method to calculate optimal path between all addresses of a person(whose "Address" was changed) and update user interface. I need to know that one of addresses was changed and person whose address was changed. The only solution I have so far is to implement some sort of event bubbling. When "Address" object changes it notifies parent("Person" object) about the change, "Person" notifies "Document". Now we can add listener to "Document" class and make required actions. Downside of this approach - I have to manage "parent" links for all the objects in the hierarchy. Can somebody comment on this solution? is it good? Or maybe I am doing something wrong here?

Thanks.

A: 

You are on the right track.

When you create the event, just set the bubbles property to true.

http://www.igorcosta.com/flex3/doc/flash/events/Event.html#bubbles

You'll have to add your event listener on the document class in ActionScript [unless you define the event metadata on the Document class], but it should work and bubble w/o problems.

If there is no compelling reason for you to "Catch / re-dispatch" I wouldn't bother to re-create the wheel.

www.Flextras.com
Flex event bubbling works only for UI elements. I need bubbling in my model classes. So this will not work.
Aleksey
www.Flextras.com
Sorry for the confusion, I meant Event bubbling works only for the DisplayObject's in visual tree. I can't find proof to this statement in documentation, but in my opinion this is obvious - how does IEventDispatcher will know who is holding reference to it, what if two references in different objects?
Aleksey
@Aleksey It was not obvious to me. I put together a quick test and you are right; events don't bubble on objects. Good point on two references to different objects. I guess you're stuck doing what you original stated, manually bubbling the events on each level of the heirarchy.
www.Flextras.com
A: 

You can manage your own hierarchy using "parent" fields, or you can make the parent object listen to its children and re-dispatch events. This could probably be managed mostly via some shared helper code.

Sophistifunk