8 Replies Latest reply on Dec 10, 2009 2:31 AM by nikos101

    How do add listeners to AS class

    nikos101 Level 2

      I have a singleton AS class that handles data. How do add listeners to it when My mxml files dispatch events

       

      I tried this:

       

          private function addActionListeners():void
                  {
                      systemManager.addEventListener(LogoutEvent.LOGOUT, clearData);
                  }

       

      but an AS class doesn't understand systemManager

        • 1. Re: How do add listeners to AS class
          Gregory Lafrance Level 6

          Make your class extend UIComponent, then you'll have access to systemManager.

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance
          www.ChikaraDev.com

          Flex Development and Support Services

          1 person found this helpful
          • 2. Re: How do add listeners to AS class
            nikos101 Level 2

            awesome, the systemManager does appear to be null though?

             

            private function addActionListeners():void
                        {
                            systemManager.addEventListener(LogoutEvent.LOGOUT, clearData);
                        }

            • 3. Re: How do add listeners to AS class
              nikos101 Level 2

              I call this method

               

              public function VO(caller:Function=null)
                      {
                          if (caller != VO.getInstance)
                          {
                              throw new Error("Singleton is a singleton class, use getInstance() instead");
                          }
                          if (VO.instance != null)
                          {
                              throw new Error("Only one Singleton instance should be instantiated");
                          }
                          //put instantiation code here
                          addActionListeners();

               

               

                      }

              • 4. Re: How do add listeners to AS class
                nikos101 Level 2

                All my components use the VO actionscript class that extends UIComponent reference it like this:

                 

                VO.getInstance()

                 

                maybe I need to do something like this

                 

                private var vo = VO.getInstance

                 

                in order to geth the VO class to have a non null systemManager?

                 

                 

                 

                 



                package models
                {
                    import customevents.LogoutEvent;
                   
                    import mx.collections.ArrayCollection;
                    import mx.core.UIComponent;

                 

                    public class VO extends UIComponent
                    {
                       
                            [Bindable]
                        public var usersResult:ArrayCollection;
                          

                       
                       
                            private function addActionListeners():void
                            {
                                systemManager.addEventListener(LogoutEvent.LOGOUT, clearData);
                            }

                 

                            private function clearData():void
                            {
                               
                                clearDataValues();
                            }
                       
                   

                 

                        public function VO(caller:Function=null)
                        {
                            if (caller != VO.getInstance)
                            {
                                throw new Error("Singleton is a singleton class, use getInstance() instead");
                            }
                            if (VO.instance != null)
                            {
                                throw new Error("Only one Singleton instance should be instantiated");
                            }
                            addActionListeners();
                           

                 

                        }       

                 

                        public static function getInstance():VO
                        {
                            if (instance == null)
                            {
                                instance=new VO(arguments.callee);
                            }
                            return instance;
                        }

                 


                   
                    }
                }

                • 5. Re: How do add listeners to AS class
                  paul.williams Level 4

                  I would expect the systemManager property to be set when the component is added to the display list.

                  • 6. Re: How do add listeners to AS class
                    nikos101 Level 2

                    Yes but this never gets added to the display list, it is purely a data

                    class.

                     

                    best wishes

                     

                    Nikos

                    • 7. Re: How do add listeners to AS class
                      paul.williams Level 4

                      Then extending UIComponent isn't the right way to go. If you want to use SystemManager as an event conduit then you'll have to find someway of injecting it into your singleton. Maybe when your application starts up.

                      • 8. Re: How do add listeners to AS class
                        nikos101 Level 2

                        All I need the data class to get is events from the components, maybe I

                        don't need to use SM

                         

                        best wishes

                         

                        Nikos