4 Replies Latest reply on Nov 11, 2010 10:19 AM by Devtron

    Global Variables?

    ShedoSurashu Level 1

      Hi! Sorry if I seem to be really new to Flex (actually, I really am) but I just wanted to know, how do I use a global variable in Flex?



      I retrieve some data from a PHP file on a server. I want those data to be preserved althrough out my application as long as it's running, irregardless of the state it's in...


      How would I go about achieving such?

        • 1. Re: Global Variables?
          JeffryHouser Level 4

          Global variables break encapsulation, thereby making your code less easy to reuse and test.  I wouldn't use them without some thought of the architecture and consequences.


          All that said:


          1) You can create a variable in your main application file and access it using FlexGlobals.topLevelApplication.yourVariable .  (Note: In Flex 3, use Application.application )

          2) You could create A Singleton class and access it anywhere you need it.  The Cairngorm ModelLocator is one example.  If you Google, I'm sure there are a bunch of different ways to create a Singleton in Flex.

          • 2. Re: Global Variables?
            ShedoSurashu Level 1

            Hmm I'm afraid I failed to get half of what is stated on your reply.

            • 3. Re: Global Variables?
              Abhinav Sharma Level 2



              Global Variable can be accessed from anywhere in the application.

              Make Global Variables using Static keyword.


              Simplest way:

              Make a public class GlobalVars.as which contains global variables.


              GlobalVars.as     -

                  public class GlobalVars

              //     constructor

                       public function GlobalVars()



              //     declare global variables like this         


                      public static var globalVar1:String = "Some Data";
                      public static var globalVar2:Number = 23;




              globalVar1 and globalVar2 are global variables.


              To access them in another class in same application you just have to do the below stuff:


              lets another class is MyClass.as


                   public class MyClass

              //     constructor

                       public function MyClass()


              //       access Global Vars


                        GlobalVars.globalVar1 = "Store New String"; // globalVar1 = "Store New String"

                        private var getVal:Number = GlobalVars.globalVar1; // getVal = 23




              Hope this would helped you.




              • 4. Re: Global Variables?
                Devtron Level 3

                You need to look into FLEX GLOBALS. It's the FLEX version of Global variables. FLEXGlobals reside in the application layer, meaning they sit at the top of the totem pole and are exposed to the entire application. This is kinda new to Flex 4, and its a different approach. If you're using FLEX 3, I believe its a bit different?


                Here is the poopy Adobe documentation:

                http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/core/FlexGlobals.htm l?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2


                Here is a link to google search:

                http://www.google.com/search?source=ig&hl=en&rlz=&=&q=FLEX+Globals&btnG=Google+Search&aq=f &oq=#hl=en&expIds=17259,17291,17311,25532,25752,25854,25907,26339,26637,26663,26788,27404, 27510,27547&sugexp=ldymls&xhr=t&q=FLEX+FlexGlobals&cp=9&qe=RkxFWCBGbGV4R2xvYmFscw&qesig=NC kMdkHkzZBWs1W2VGz6hQ&pkc=AFgZ2tls8XOyn5apTIf4TRNs-kAzooVu3OMBJQsjAvsazYs6t0Hj0Axm6Jcx91Y6- kHsYloBh2QtywMhabFtZTkP6hrLBVtfnA&pf=p&sclient=psy&safe=off&source=hp&aq=0&aqi=&aql=&oq=FL EX+FlexGlobals&gs_rfai=&pbx=1&fp=53894057c0dc71cc


                Page 129 of the Flashbuilder 4 and Flex 4 Bible states:

                "New Feature
                The MX version of the Application component had a static property named application that referenced
                the application itself. The commonly used expression was Application.application. In Flex 4,
                this functionality has been moved to a static property named topLevelApplication, in a new class named
                FlexGlobals. So, the new version of this expression is FlexGlobals.topLevelApplication. As
                with the Flex 3 version, this expression is defined in the API to return an Object, but the native type matches
                the application’s actual name. So, to create a variable that references an application named HelloWorld, use
                this syntax:

                private var myApp:HelloWorld =
                FlexGlobals.topLevelApplication as HelloWorld;"


                Page 25 of the FLEX Cookbook states:

                "A Flex component can be nested within many different parent components, and also
                within multiple applications. In previous versions of Flex, developers would often access
                the parent application by using Application.application to return a reference to
                the main application. In Flex 4, you should use either the parentApplication property
                that all UIComponents define to access the application that a particular component
                is loaded into, or the FlexGlobals.topLevelApplication to access the top-most
                Figure 1-10 shows how a Button could access the various parent components within
                which it is nested.



                Figure 1-10. Accessing parent documents and applications from a component"