13 Replies Latest reply on Sep 23, 2010 7:18 AM by ArrowUpRight

    Events & Static Classes

    ArrowUpRight

      Can only an instance of a class dispatch an event, or the static class can too?

       

      We have MyManager static class:

       

      package managers

      {

           import mx.rpc.CallResponder;

           import services.MyDataService;

           import valueObjects.MyValueObject;

       

           public static class MyManager

           {

                public static var updateDataResult:CallResponder = new CallResponder();

                private static var myDataService:MyDataService = new MyDataService ();

                private static var myValueObject:MyValueObject = new MyValueObject();

       

                public static function updateMyDataService():void {

                     updateDataResult.token = myDataService.update(myValueObject);

                }

           }

      }

       

      And the other class wich calls:

       

      import managers.MyManager;

      MyManager.updateMyDataService();

      MyManager.updateDataResult.addEventListener(ResultEvent.RESULT,resultReceived);

       

      and this Event Listener does not work.

        • 1. Re: Events & Static Classes
          BhaskerChari Level 4

          Hi Banditizm,

           

          I dont think there is any such called Static class in Flex. I think we can have static members and static methods of a class. But we cannot declare something like...

           

          public static class IdentificationManager

          {

           

          }

           

          and yes you can dispatchEvent and listen from a class which has static members and static methods.

           

           

          Thanks,

          Bhasker

          • 2. Re: Events & Static Classes
            ArrowUpRight Level 2

            Yes, you are right, there is no "static" keyword in the class declaration, and i mean Global Classes Without Instantiating, you know, when i said Static Classes, but the construction i show above does not work. It likes, the static callResponder can not dispatch any event

            • 3. Re: Events & Static Classes
              BhaskerChari Level 4

              Hi Banditizm,

               

              Yes you can even dispatch the events from your static callResponders but I think the difficulty you might be facing is you cannot capture or listen to those events dispatched.

               

              You can do one thing you can dispatch these events w.r.t systemManager and also listen to these events w.r.t. it.

               

              I mean from your call reponder dispatch the event as below..

               

              systemManager.dispatchEvent(new Event("dataLoadComplete"));

               

              //And you can listen it in your mxml or .as file as below...

              systemManager.addEventListener("dataLoadComplete", onDataLoadComplete);

               

               

              Thanks,

              Bhasker

              • 4. Re: Events & Static Classes
                ArrowUpRight Level 2

                Thank you Bhasker!

                Now i realise that event dispatched but i can't listen to it.

                But, for future, what is w. r. t. deсoding?

                And, you mean, the SystemManager with capital S.

                • 5. Re: Events & Static Classes
                  BhaskerChari Level 4

                  Hi Banditizm,

                   

                  w. r. t ---> with respect to..

                   

                  I have used short hand notation for it..

                   

                  Have you made a test for listening the event..dispatched..and also try to remove the eventListener w.r.t systemManager only when it is no longer needed..

                   

                  Otherwise it still retains memory.

                   

                  Note: systemManager is with small s only..not capitalized..

                   

                  Thanks,

                  Bhasker

                  • 6. Re: Events & Static Classes
                    ArrowUpRight Level 2

                    oops, only the instance of SystemManager can dispatchEvent. So, i can't have only one instance of something to be accessed from any file just by importing it.

                    • 7. Re: Events & Static Classes
                      BhaskerChari Level 4

                      You need not create any instance of systemManager . Actuallt the systemManager itself refers to the top level application.

                       

                      You can directly use systemManager.dispatchEvent and systemManager.addEventListener with out creating any reference to it anywhere and you can use the same any where in your application.

                       

                      Hope you now understand it.

                       

                       

                      Thanks,

                      Bhasker

                      • 8. Re: Events & Static Classes
                        ArrowUpRight Level 2

                        OK, the correct question is, why can not i acces systemManager(current SystemManager used by this component) from my custom manager class wich extends nothing? Otherwise, i can acces it from the mxml component.

                        • 9. Re: Events & Static Classes
                          ArrowUpRight Level 2

                          Note: i'm trying to connect to systenManager from static function.

                          • 10. Re: Events & Static Classes
                            BhaskerChari Level 4

                            From where ever you use the systemManager in your application it refers to only one reference that is a reference for the whole application.

                             

                            Its not like it will be seperate for different components, it refers to same and only single topLevel SystemManger class form where ever you try to access.

                             

                            You can also access it from your custom manager class also. If your class is a .as file then you can try

                             

                            Application.application.systemManager.dispatchEvent(new Event("someEvent"));

                             

                            Thanks,

                            Bhasker

                            1 person found this helpful
                            • 11. Re: Events & Static Classes
                              ArrowUpRight Level 2

                              God bless you Bhasker.

                              Now when i use this construction

                               

                              Application.application.systemManager.dispatchEvent(new Event('whereAreYouEvent'));

                               

                              codecompletion helps me, but error thrown:

                               

                              1119: Access of possibly undefined property application through a reference with static type Class. 

                               

                              IdentificationManager.as    /PR5/src/managers    line 48    Flex Problem

                               

                              It looks like madness i know but why does not this work to me?

                              • 12. Re: Events & Static Classes
                                BhaskerChari Level 4

                                Hi,

                                 

                                Are you using FlashBuilder(Flex4) or Flex3..??

                                 

                                Have you imported the below namespace..

                                 

                                import mx.core.Application;

                                 

                                If you are using Flex4 I think you need to use FlexGlobals.topLevelApplication instead of Application.application also dont forget to import the correct namespace..

                                 

                                Thanks,

                                Bhasker

                                1 person found this helpful
                                • 13. Re: Events & Static Classes
                                  ArrowUpRight Level 2

                                  Wooh! Yes, i use Flex4, and now when i respond to FlexGlobala.topLevelApp both in .as and .mxml files everything woks!

                                  Thank you a lot once more, such a beautiful learning day!