5 Replies Latest reply on Nov 26, 2008 1:27 PM by injpix

    Global variables

    jfb00 Level 3
      Hi All,
      My app have a login page, after validate my login I want to stored the userid in a global variable so I can use it alone all the app in flex.
      Can I do global variables?
      How can I do this?
      In the pass I was able to do using session variables.
      thanks

      JFB
        • 1. Re: Global variables
          *gsb* Level 1

          Global variables as in AS2 do not exist in AS3.
          Global variables are usually a bad idea from a design point of view.

          However the easiest way to do what you want is to define your username or authorization variable on the main application object. Then anywhere in your code you can access it via:

          Application.application.yourAuthorizationVariable

          Be sure to include where appropriate:
          import mx.core.Application


          • 2. Re: Global variables
            injpix Level 3
            [I agree mostly with what gsb has posted. However I personally don’t like to have any object accessing a variable somewhere in the application. ]

            I don’t see why you would need to have a global variable in a Flex application. If you need to simply store the user id, you should do this in your model (that’s if you have one). If there is no model in your application then I would create a singleton class perhaps called “SessionModel”. And in this class you can have a getter/setter to store the user’s id. Then this can be accessed by any object in your application. However, as always you shouldn’t exploit this design pattern (Singleton) freely.

            Descendants of a parent object shouldn’t be tapping into a singleton. They should be accessing their parent object for that info. At least that is a simple rule of mine and others.

            Or are you talking about shared objects? Which retains data on the user’s machine when the user returns to the application after it has been terminated?
            • 3. Re: Global variables
              rtalton Level 4
              Use a shared object which stores the login info.
              Clear it out whenever a user opens the application, otherwise it will always show them as logged in when you check it.
              • 4. Re: Global variables
                jfb00 Level 3
                Thanks everyone reply's.
                Looks like i need a share object so the user can reuse the data later.
                Anybody have an example of this?
                What is the disadvantage to have main application object?
                Rgds

                JFB


                • 5. Re: Global variables
                  injpix Level 3
                  To simply put it, a disadvantage of assigning a variable in the application object is that it will be terminated when the application terminates. The advantage of having a shared object, is that a variable can be retrieved so that the user doesn’t have to reenter their id (Assuming that the user hasn’t cleared their browser cache). If you do choose to use shared objects, keep in mind, then anyone using that browser can access the account of the user’s shared object.

                  //Shared Objects in Help docs
                  http://livedocs.adobe.com/flex/3/html/help.html?content=lsos_1.html