5 Replies Latest reply on Nov 9, 2011 6:40 AM by Francisc

    Persistent var

    Francisc Level 3

      Hello,

       

      I have a variable that I want to access at anytime as it does not change.

      I am not using any framework.

       

      One option is:

      FlexGlobals.topLevelApplication.myVar

       

      But I don't like that.

       

      What I am doing now is this:

       

      public class Test

      {

          private static var _myVar:Object;

         

          public function Test(someObj:Object)

          {

              _myVar=someObj;

          }

          public static function get myVar():String

          {

              return _myVar;

          }

      }

       

      Then first set some value:

      var test:Test=new Test({test:'hello'});

       

      Then everywhere else:

      Test.myVar to get the value

       

      But I don't like this either.

       

      What's the proper way of doing this please?

        • 1. Re: Persistent var
          artguate

          If you wish to store a value that is not a pre-declared const, you have to instantiate it at some point in time somewhere, there is no way around it.

           

          You could also have a static setter function that actually stores the value in FlexGlobals.topLevelApplication  

           

          I would not call that "proper" but it's just another option.

          1 person found this helpful
          • 2. Re: Persistent var
            Amy Blankenship Level 4

            It sounds like what you're trying to create is "persistent global state," aka "bad practice." http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons

             

            Luckily, it seems your intuition is spot on that this will get you in a whole heap of something smelly if your project is larger in scale than 2 days' work.  What you need to do is create a proper Class that contains the information you need access to, then create an instance of that Class (not to be confused with Singleton).  Whenever something in your application needs that information, provide it by Dependency Injection.

             

            There are a number of Frameworks that specialize in making this Dependency Injection easier, such as Mate, Robotlegs, Parsley, and Swiz. My favorite is Robotlegs, but this video about Mate does a great job of explaining how the Frameworks work on a general level http://tv.adobe.com/watch/360flex-conference/mate-flex-framework-by-laura-arguello/

            1 person found this helpful
            • 3. Re: Persistent var
              Francisc Level 3

              Thank you both.

               

              I am getting data from a webserver. That data doesn't change during runtime.

              I want to be able to have access to it after I get it back without calling out to the server again.

              A framework is good, but there isn't enough time for that now.

               

              Is there a workaround that is a good practice for what I'm looking for?

              • 4. Re: Persistent var
                Amy Blankenship Level 4

                The data does change...It is empty before the call to the server and has data afterward.  This means that it is possible to write to it, and if you give global access to it and your project lives long enough and has enough developers, someone will.

                 

                Seriously, just create your one object that has your returned data once the data is returned, and pass that data around to the components that need it, just like you would with any other Model variable.

                 

                If you feel like you absolutely must make it a global static variable, I'd suggest making it where it is initialized in the constructor and all properties are read-only.

                • 5. Re: Persistent var
                  Francisc Level 3

                  Thank you.