12 Replies Latest reply on Sep 30, 2008 5:44 AM by Peter Lorent

    Event Listening across two components?

    EvolvedDSM Level 2
      Let's say I have two components. One of them has a button. The other will post an Alert once it detects that the button has been pressed.

      How can the Alert component detect the button press of the other component? I figured an eventListener would been needed, but I am not sure how to write it.
        • 1. Re: Event Listening across two components?
          ericbelair Level 1
          It sounds like you are making it too complicated. You should simply display the Alert with the Button click event.
          • 2. Re: Event Listening across two components?
            EvolvedDSM Level 2
            While I appreciate the reply, you've dodged my original intent. =\

            What I'm facing isn't as cut and dry as the [button & alert] example I gave above, but my actual issue is too complex to go into any amount of explaining or detail, so the example was as simple as I could make it.

            I need a way that component #1 can detect events of component #2. Simple as that.

            Component #2 does "something" and Component #1 reacts to it. Essentially though, I need a way to refresh an arrayCollection in component #1 when an event occurs in component #2.

            Hope this is more clear.

            • 3. Re: Event Listening across two components?
              g-s-b
              There may be several ways to do this, like setting up an observer pattern or more simply using the Binding utilities. However, I would suggest that you look at the "Mate" framework for Flex project development. It better enables MXML tag based event management. Here is a link to the Mate site:
              http://mate.asfusion.com/

              • 4. Re: Event Listening across two components?
                EvolvedDSM Level 2
                Ehh, I'm too deep into this project to start with another framework.

                Maybe someone can just tell me how I can refresh an arraycollection from component #1 via an event in component #2...
                • 5. Re: Event Listening across two components?
                  istrasci Level 1
                  First of all, is one component a child of the other, like:
                  <ns1:component1>
                  <ns1:component2 />
                  </ns1:component1>
                  ???

                  Or are they both "descendants" of a common ancestor (parent) component, like:
                  <mx:VBox>
                  <ns1:component1 />
                  <ns1:component2 />
                  </mx:VBox>
                  ???

                  That makes a HUGE difference!!! I'm assuming it's the second way, it which case you'd need to use events...
                  • 6. Re: Event Listening across two components?
                    ericbelair Level 1
                    Sorry that I didn't understand you're original intent....

                    If you're not using an MVC framework (ie Cairngorm, EasyMVC), I would dispatch a custom event from the button click, and have a listener catch it and trigger the Array Collection update. This could get tricky, if component #2 is not a child or sibling of component #1.
                    • 7. Event Listening across two components?
                      EvolvedDSM Level 2
                      As guessed, they are both children of my main application. And yes, I would need to use events, as my title indicates.

                      Arg. Ok, let's say I have a function in component #2. Is there no way within that function that I can call a refresh to an arraycollection in comp #1?

                      private function myfunction():void{
                      comp1.ac.refresh();
                      {

                      Something as simple as that would work just fine. Though, comp1.ac.refresh() simply doesn't work of course.

                      EDIT: I'll give Eric's suggestion a shot in a moment.
                      • 8. Re: Event Listening across two components?
                        ericbelair Level 1
                        That won't work unless comp1 is a child of comp2. comp2 can't "see" comp1 if it is in another part of the application. The only other suggestion that I have is to make the Array Collection a public variable in the Application file. Then it can be accessed throughout the the application via "parentApplication."
                        • 9. Event Listening across two components?
                          istrasci Level 1
                          Something like that might work, but from a programming standpoint, your components should be designed in such a way that they know absolutely nothing about the outside world (parent or other components). For all it knows, it's the only component in existence. But, using events, he could send out a message (event), that if someone else (another component) happened to hear it, they could do something in response to it. Think of the movie "Contact" (if you've ever seen it).

                          Anyway, your best bet is to make a custom event that is able to encapsulate an ArrayCollection. Then when the button is clicked, you wrap up the AC in the event and send it out. Your other component will be listening for events that are of that type (your custom event). Then he can unwrap the AC in it and use it. If you do not know how to create custom events and/or send data in events, look up a tutorial on it. It's not that difficult to implement if you can follow the concept I just talked about. However, I've also personally tried to make a custom event to pass an ArrayCollection, and for some reason it never worked (even though I followed some tutorials down to the letter).
                          • 10. Re: Event Listening across two components?
                            EvolvedDSM Level 2
                            I'll continue to work on this with the help in this thread, but I've gotta close up shop for the day. thanks for the suggestions
                            • 11. Re: Event Listening across two components?
                              Level 7

                              "EvolvedDSM" <webforumsuser@macromedia.com> wrote in message
                              news:gbr9h1$60s$1@forums.macromedia.com...
                              > While I appreciate the reply, you've dodged my original intent. =\
                              >
                              > What I'm facing isn't as cut and dry as the [button & alert] example I
                              > gave
                              > above, but my actual issue is too complex to go into any amount of
                              > explaining
                              > or detail, so the example was as simple as I could make it.
                              >
                              > I need a way that component #1 can detect events of component #2. Simple
                              > as
                              > that.
                              >
                              > Component #2 does "something" and Component #1 reacts to it. Essentially
                              > though, I need a way to refresh an arrayCollection in component #1 when an
                              > event occurs in component #2.
                              >
                              > Hope this is more clear.

                              This violates best practices (loose coupling) in Flex. Please see the FAQ
                              for resources about how you can use better practices in your code.


                              • 12. Re: Event Listening across two components?
                                Peter Lorent Level 2
                                Maybe this can help. Attached is a very simple MVC like setup. The main application functions as a (document) controller and decides what to do when components broadcast events. The ApplicationManager is a model class that loads data and broadcasts events that the data is loaded to subscribed listeners. The Dashboard is a component (a View) that uses the ApplicationManager to load the data and then sends out an event to let the main application know it has received an update. The main application then updates the ClientScreen component (a View). I've commented the code.