6 Replies Latest reply on Apr 10, 2007 8:16 PM by evPlanet00

    Event dispatch

    kruse
      Main – view
      |
      - Menu
      |
      - MenuDetails


      How do I in the view module catch that a click has happened in the MenuDetail?
      I think that this has something with event dispatching.
      I want to send a number from MenuDetails to view.
      How do I do that
        • 1. Re: Event dispatch
          ntsiii Level 3
          You can easily dispatch an event from a component by doing:
          dispatchEvent(new Event("myevent",true))// the true makes the event bubble

          In the main - view app/component, put a listener:
          this.addEventListener("myEvent",myHandlerFunction);

          private function myHandlerFunction(event):void

          There are a couple ways to pass data. The most flexible is to create a custom event. this is easier than it sounds, and it can carry any data you want.

          An easier solution is to have a public property on the MenuDetails compopnent, say myData. Just before you dispatch the event, set this variable as needed. Then in the hanlder function you can do:
          var myData:String = event.target.myData;

          Tracy
          • 2. Re: Event dispatch
            kruse Level 1
            I did find out how to dispatch an event and use bubble
            in the file MenuDetails i do this

            function menuClick():void {
            dispatchEvent(new Event("myEvent",true));
            }

            and in main I catch it

            addEventListener("myEvent",testHandlerFunction);


            private function myHandlerFunction(event):void {
            Alert.show('test');
            }

            This works. I do catch it in main but it is not in main I want to do something.
            Is it nessesary to call a function in the addListener?
            addEventListener("myEvent", testHandlerFunction);

            I want an action to happen in the module view.
            How do I listen in view for the event in Main?
            • 3. Re: Event dispatch
              kruse Level 1
              BUMP

              Anyone?
              • 4. Re: Event dispatch
                FlightGuy Level 1
                Two options:
                1. make the menuClick dispatch an event from the application (application.dispatchEvent...), and make the module view listen for the event on the application (application.addEventL...).
                2. let the menuClick remain dispatch the event with bubbles=true - this means you can listen for it anywhere in the hierarchy. Then let the module listen for it on the application like in 1. ie. in the module code:

                (Application.application as Application).addEventListener("myEvent", myEventHandler);

                Tim
                • 5. Re: Event dispatch
                  kruse Level 1
                  Thanks FlightGuy,

                  I did use the (Application.application as Application).addEventListener("myEvent", myEventHandler);

                  But now I have another problem.
                  When I click and dispatch an event in the module MenuDetails I also store a menuID.
                  But When I recieve the event in the view class I cannot use the variable MenuId stored in the MenuDetails module.
                  How do I share this data to anothe module?
                  • 6. Re: Event dispatch
                    evPlanet00
                    Hi,

                    I'm also interested in how to properly manage passing events from branch to branch via the main application.

                    I've tried a number of methods, including those mentioned above with not much luck.

                    When I try to listen for the event on the application via (Application.application as Application).addEventListener("myEvent", myEventHandler); I get an error: 'Access of undefined property Application'

                    When I tried to dispatch the event again from the application, I got a stack overflow error.

                    One difference for me is that I need to have both leaves (my custom components) broadcast as well as receive the event.

                    It may be that I have a flaw in the architecture of my app, but even if I end up solving the problem by changing that, I'd like to know what the best way to send events across to other components is.

                    Thanks to anyone who can offer some advice.

                    Evan