6 Replies Latest reply on Sep 4, 2009 6:43 PM by PeakDigital

    Send a custom event downward through hierarchy?

    PeakDigital Level 1

      I have been struggling for several days to work with custom events across custom components and .as classes.  (earlier thread at http://forums.adobe.com/message/2222573)


      With the help of members on this board I have learned a lot, but am still struggling with some basics regarding event propagation and what can and can't "hear" events based on placement in the app hierarchy. I have learned that systemManager is not an option in some contexts.


      Here is an extremely simplified pseudo-model of the construction of the portion of my project I am working with. Each mxml component also has an associated  .as controller class. I am trying to keep actionscript out of my .mxml files.


          <wrapperComponent /> (in a TitleWindow via PopUpManager)

               ...lots more stuff here...



      mxml file:

              <tree  itemClick="dispatchCustomEvent()"/>
              <locationsManager />



      mxml file:
                  <datagrid /> <--Need to capture event here
                  <button /><--Need to capture event here
                  <button />


      If I am understanding what I have been reading correctly, an event dispatched from my Tree in the wrapper component would propagate upward to the Application, and never be detected by the components lower in the hierarchy, where my locationsManager component resides.


      I have tried attaching a listener to Application.application.systemManager, in both the wrapper and locationsManager .mxml files, the TitleWindow that they are presented in, and a few other places. I have also attempted to dispatch the event from multiple different locations (one at a time for testing). This seems to be one nut I can't crack...need the Complete Idiot's Guide to Flex events, I guess.


      My understanding is that the ideal is to have no direct linkage between components, and that events should be used to transmit all data between components, so that is what I am trying to accomplish. I can make it work by tunneling into the locationsManager component's controller from the wrapper controller and dispatching the event within it, but that doesn't seem right.


      Thanks for your patience and assistance.


        • 1. Re: Send a custom event downward through hierarchy?
          Andrew Rosewarn



          I think usually you would look to listen for events and react to them as they travel up  the hierarchy.  To alter components lower you generally access with API's.


          If your component is burried in the heirachy and you want to do something to it after an event you could try keeping a reference to that component higher up in the Application.  That way when the event travels up from your dispatching comp, you could access all the properties etc through the refernce you have.


          Just my quick thought



          1 person found this helpful
          • 2. Re: Send a custom event downward through hierarchy?
            PeakDigital Level 1

            Thanks for the input.


            I'm still new to Flex, learning its terminology.  Is it considered to be an "API" when the lower component exposes a select group of properties via getter/setter methods?




            • 3. Re: Send a custom event downward through hierarchy?
              Andrew Rosewarn Level 3

              Yea I would have said thats the correct termonology.


              You are creating a custom component in you example, and if that say had a vbox in it, you wouldn't want to addChidren directly to the vbox, as there may need to be other processes taking place whe you do so.  Therefore you'd create a public method call say addVboxItems and use that.  That way you properly control what happens in the comp when you add items.


              I'd say that consititues an API.



              • 4. Re: Send a custom event downward through hierarchy?

                Another method, used often in the Model View Controller (MVC) strategy, is to have a central data model which is simply a class that holds the state of the application.  Singleton classes are great for this.

                So you would have a class called Model.as for instance, that would have properties including currentLocation, etc to hold whatever data you want to pass to the datagrid.  You could then bind to that model from anywhere in your application and see data changes.

                Another method would be the Message Bus pattern (I think that's what it's called) where you create a class that extends EventDispatcher and everything uses it to dispatch events (ie. messageBus.dispatchEvent(new myCustomEvent());) and everything listens for events from it (ie. messageBus.addEventListener("myCustomEvent", myEventHandler);).  Again, with this method, you can capture events anywhere in your application.

                1 person found this helpful
                • 5. Re: Send a custom event downward through hierarchy?

                  I concur with TheFlexGuy.  I have only been doing OO type programming for a little over a year.  I am using an MVC, having been taught (and written by) one of our guys here.  I love it.  Removes much of the messing with passing things around.  I know this does not address you issue, but I wanted to add my support for MVC's.  I use it on everything that is more complicated than few pages of code.



                  • 6. Re: Send a custom event downward through hierarchy?
                    PeakDigital Level 1

                    Thank you for the additional input. I'll look at working those suggestions into my project.