7 Replies Latest reply on Sep 30, 2011 6:53 PM by targetplanet

    Listening for an event in main from a component

    whitefinger Level 1

      Hi

       

      Ok so i have a Flash Builder project 4.5 that i created in Flash Catalyst thats draws all its data from MySql and four months into the build, as this is my first web project im noticing that when the page loads up in the browser it loads everything at once! and as im hoping to reuse some of the same components for diffrent tasks i assume that if i put the data retrivail under events that happen within the web page this will hopeful control the data flow as well as showing the correct data depending on CurrentState!

       

      The problem that im having is that my buttons are in my Main Application and the data retrival process is in my components! so i was trying to create addEventListener(MouseEvent.CLICK events in a component that was listening for events in my Main application. i have search the internet but every one is talking about the process in the main or component but not both!

       

      Does anyone know what im talking about and could you please share some light please

        • 1. Re: Listening for an event in main from a component
          whitefinger Level 1

          So in my Main.mxml Application i have a Button with the id=SearchButton and a Click=button_clickHandler_6() with the function attached but i would like to listen for this event in my component ClientDataListSkin.mxml and then execute a function on hearing that event

          • 2. Re: Listening for an event in main from a component
            targetplanet

            I assume the ClientDataList component is a child of the main mxml.  Rather then having the compnent listen for an event, you need to call a function in that component from the button click handler

            button_clickHandler_6(e:Event){

                 ClientDataListSkinInstance.getData();

            }

            • 3. Re: Listening for an event in main from a component
              whitefinger Level 1

              Thanks for the response targetplanet and im sure you are right as i do use this appoach on occassion just must be thinking to deep to think i should of used the addeventlister approach.

               

              But could you tell me please the above way did work but the only way i could access the function is using the "public static function" but when using that the fuction cant see ID name of my CallResponder that is declared in <fx:Declarations> but if i use "protected function" the function can see the ID but calling from main wont see the function in the componant!

              • 4. Re: Listening for an event in main from a component
                targetplanet Level 1

                did you declare the ClientDataListSkin component in the mxml of your main mxml?  if so give it an id, and then using that id you should be able to access any public function in that component.

                 

                so in your ClientDataListSkin.mxml file say you have a function called getData

                 

                public function getData():void

                {

                //code to get data

                }

                 

                and you have given your ClientDataListSkin insitance an id

                <components:ClientDataListSkin id="clientList" ....

                 

                in your button listener you should be able to call that function like so

                clientList.getData();

                • 5. Re: Listening for an event in main from a component
                  whitefinger Level 1

                  O my god, something so simple but i didnt think of doing that many thanks targetplanet

                  • 6. Re: Listening for an event in main from a component
                    whitefinger Level 1

                    Do you ever get to a point in programming where you think that perhaps i have choosen the wrong hobby to get infatuated about

                     

                    That code worked really well targetplanet but it had a side effect that just makes me nutts lol

                      as it triggered a mouse event function in the component that was initialized in Main and just made my web site go nuts . the component that we initialized was a skin but its itemRenderer component that followed had the mouse event! perhaps i better start again with me learning perhaps i missed something

                    or perhaps i should just have one component to one job instead of trying to be clever and mutitask with less components!

                    • 7. Re: Listening for an event in main from a component
                      targetplanet Level 1

                      Yeah for a long time my tendency was to lean towards one component for multiple taskes.  It seemed like better programming for some reason, but what I found was the code would get really complex, and I would get lost in my own code.  Things would break and it would take me forever to find the issue.  So I started simplifying. Creating many simpler components that handle one or two tasks, and it made a huge difference in my app development.  Sure I had crazy numbers of componets, but when something goes wrong, it is usually easier to find.  Another huge improvement in my coding to start using a Framework.  I use puremvc, and I couldn't imagine trying to create something without it.

                      1 person found this helpful