4 Replies Latest reply on Oct 31, 2008 11:42 AM by Newsgroup_User

    Custom events top down

    archemedia Level 4
      I have an application with 2 identical components. In the component, I declared a custom event named "testEvent". In the initializing of the component I added an event listener for this event.
      Now, in the main application, I added a button which dispatches the testEvent. Why won't the two components listen to this event? Normally, when an event is dispatched, shouldn't it pass every child node in the capturing phase?
        • 1. Re: Custom events top down
          Level 7

          "archemedia" <webforumsuser@macromedia.com> wrote in message
          news:gdmn9i$b8u$1@forums.macromedia.com...
          >I have an application with 2 identical components. In the component, I
          >declared
          > a custom event named "testEvent". In the initializing of the component I
          > added
          > an event listener for this event.
          > Now, in the main application, I added a button which dispatches the
          > testEvent.
          > Why won't the two components listen to this event? Normally, when an event
          > is
          > dispatched, shouldn't it pass every child node in the capturing phase?
          >
          > This is the code for the main application
          >
          > <?xml version="1.0" ?>
          > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          > verticalAlign="middle" xmlns:local="*">
          > <mx:Script>
          > <![CDATA[
          > import flash.events.Event;
          >
          > public function doEvent(e:Event):void
          > {
          > dispatchEvent(new Event("testEvent", true));
          > }
          >
          > ]]>
          > </mx:Script>
          > <mx:Button label="Click" click="doEvent(event)"/>
          > <local:testComp/>
          > <local:testComp/>
          > </mx:Application>
          >
          >
          > This is the code for the component
          >
          > <?xml version="1.0" encoding="utf-8"?>
          > <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="400"
          > height="300"
          > initialize="init()" backgroundColor="#DE1C1C">
          >
          > <mx:Script>
          > <![CDATA[
          > public function init():void
          > {
          > this.addEventListener("testEvent", testEventHandler)
          > }
          >
          > public function testEventHandler(e:Event):void
          > {
          > feedback.text = "Arrived!";
          > }
          > ]]>
          > </mx:Script>
          > <mx:Text id="feedback" text="Waiting..."/>
          > </mx:Canvas>

          Try this.parent.addEventListener(...)

          But I'm not sure why you want to have a child listening to the parent, since
          the parent can directly set properties and call methods on its children.


          • 2. Re: Custom events top down
            Gregory Lafrance Level 6
            During the capture phase, events pass from the top of the display list down through all objects in the display list in the object chain to the object that dispatched the event.

            So in your case the components cannot listen for this event.

            Amy is right to question your logic in this case. There may be another way to achieve the communication you desire between the app and the components.

            Perhaps if you describe with greater clarity what you are trying to achieve a suggestion might bubble up (slight pun intended).
            1 person found this helpful
            • 3. Re: Custom events top down
              archemedia Level 4
              Sorry Greg and Amy for not replying sooner but I was abroad.
              You're right that I can communicate with childs directly but what I wanted to do is to communicate with ALL childs at the same time! If you haven an application with an HBox in it and in this HBox are 20 identical components, it should be possible to dispatch an event in the application which drops down to all child objects. I worked around this by calling the getChildrenArray of the HBox and dispatching the event to each of these childs. Wouldn't it be nice if the dispatchEvent function would have a property iDontKnowWhatNameItShouldHave which, when set to true would descend in the whole child structure and dispatch the event to all child objects?
              • 4. Re: Custom events top down
                Level 7

                "archemedia" <webforumsuser@macromedia.com> wrote in message
                news:gefein$6bv$1@forums.macromedia.com...
                > Sorry Greg and Amy for not replying sooner but I was abroad.
                > You're right that I can communicate with childs directly but what I wanted
                > to
                > do is to communicate with ALL childs at the same time! If you haven an
                > application with an HBox in it and in this HBox are 20 identical
                > components, it
                > should be possible to dispatch an event in the application which drops
                > down to
                > all child objects. I worked around this by calling the getChildrenArray of
                > the
                > HBox and dispatching the event to each of these childs. Wouldn't it be
                > nice if
                > the dispatchEvent function would have a property
                > iDontKnowWhatNameItShouldHave
                > which, when set to true would descend in the whole child structure and
                > dispatch
                > the event to all child objects?

                You might want to look at the Observer pattern. I strongly recommend the
                book "Actionscript 3 Design Patterns" for information on these kinds of
                problems.

                http://safari.oreilly.com/9780596528461/observer_pattern
                http://www.as3dp.com/2008/07/07/the-mediator-design-pattern-a-minimalist-example/

                HTH;

                Amy


                1 person found this helpful