HTML and CSS Reference
In-Depth Information
Next up, you define your global members as members of the newly created Globals object. The
difference is that now Globals is a brand new global object, and all the members you want to use
as global in your application are actually defined as children of Globals and are not polluting the
JavaScript namespace. Put another way, all your application's global variables are grouped under a
single global object that is visible to the JavaScript interpreter.
Keep application state at hand
All applications need to maintain their own state, that way the state can be persisted to disk when the
application is closed or, when used on a mobile device, the application is sent to the background.
A good approach is to have some code ready to load the application's state upon application
startup. Data can be loaded from a storage location (if available), downloaded from some remote
location, or simply initialized to default values. When the application exits, the current state should be
persisted to storage to be ready for next use.
For this pattern to work smoothly, it is necessary that you start by defining a JavaScript object
whose properties represent the state of the application. Here's an example:
var MyAppState = function () {
var that = {};
that.init = function () {
// Take care of initialization and default values
};
that.load = function () {
// Take care of loading from storage
};
that.save = function () {
// Take care of storage
};
// Other properties here
...
that.init();
return that;
}
To attach an instance of this object to the Globals container, you write code as follows:
Globals.Current = new MyAppState();
From now on, you can read and write the state of your application via highly readable statements
such as Globals.Current.Xxx, where Xxx is the name of a property or member defined on MyAppState .
Search WWH ::




Custom Search