1
votes

Is there any way to dispatch event (Action Script 3.0) on some class that every object witch is in that class and have specified listener function could catch that event? . For example I have structure like this:

edit (update class):

class example extends EventDispatcher {
object1 = {subObject1 = {anotherObject1, anotherObbject2}, subObject2}
object2 = {subObject3}
object3

...
}

and I want to some of them have event listeners:

anotherObject1.addEventListener(MyEvent.PART3D_CHANGED, function (e: MyEvent){/*do something*/);
anotherObbject2.addEventListener(MyEvent.PART3D_CHANGED, function (e: MyEvent){/*do something*/);
object3.addEventListener(MyEvent.PART3D_CHANGED, function (e: MyEvent){/*do something*/);

edit: Then I want dispatch event within this class and that every object witch have listeners handle the Event, like this:

this.dispatchEvent(new MyEvent(MyEvent.PART3D_CHANGED))

Like You see I need to dispatch one type Event and I don't need to do it on every object.

From my studies I figured it out that I need to dispatch Event on every single object but having many sub objects the code will be untidy if I had to dispatch for every single one with need listener and another option for looping the children of every objects and adding listeners without care if is there need for listener will be no cost efficiency. p.s. Sorry for my English:-)

1
I do not understand what you want. Do you want to have an Object which can fire events with having other ones registered, listening for this special event? Or do you want to have a custom event object you can fire? - philipp
I updated my question. I need that my class example dispatched event and every object witch have listeners could catch that Event - eldi
extending EventDispatcher does the trick. var test = new example(); test.addEventListener( <name>, <callback> ); and finally test.dispatchEvent( new Event( <name> ) ); You can create your own Event Types by creating a class that extends Event. - philipp
I knew that. But I was looking for option that when I dispatch event: "test.dispatchEvent(new Event(<name>))" My sub-objects Could "catch" that event. From @philipp example only object test will handle this Event - eldi
To precise my question: Looking for something like Event.bubbles but in another direction: propagates from parent to every child - eldi

1 Answers

0
votes

I do not think that there's standard way for such behavior in the event model: http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

There's capturing phase (which is the reverse of bubbling), but it still occurs for the anscestors (just in different order). Events bubbling primary motivation was to support special nature of mouse events, not pass general events around. When you click something you click both button AND stage at the same time (so both have a natural right to handle it).

Can your children subscribe to the parent class directly instead? For example, it is not uncommon to do the following if object is interested in global mouse movements:

stage.addEventListener(MouseEvent.MOUSE_MOVE, watchMouse);

Similarly your anotherObject can do:

myBigMainParent.addEventListener(MyEvent.MY_EVENT, anotherObjectLocalHandler);

IMO, that will be pretty clean: event occurs on one particular point, but anyone from anywhere can subscribe to it.