This content has been marked as final. Show 5 replies
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:
Be sure to include where appropriate:
[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?
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.
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?
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