- Objective of the Guidelines
- The Guidelines Explained
- Applications shall take care not to pollute the global object
- Applications shall be modular and object-oriented using CommonJS modules
- Applications shall defer script loading until absolutely needed
- Applications shall take reasonable steps to manage memory
Objective of the Guidelines
The Guidelines in Brief
Titanium Mobile applications should:
- Be careful not to pollute the global object
- Be modular and object-oriented using CommonJS modules
- Take reasonable steps to manage memory usage and avoid memory leaks
Seriously? That's it?
Yes. There are many different ways to build an application - using an MVC or MVVM architecture, maybe a framework of your own choosing or one you've built yourself. The guidelines in this document are considered the bedrock requirements for a stable and performant Titanium Mobile application. Your own application structures, business logic, and conventions may vary, and we do not endeavor to impose any such conventions in this document.
The Guidelines Explained
Applications shall take care not to pollute the global object
this in app.js.
Any variables declared in app.js shall be globally available in the entire application. This can cause collisions if external scripts are included into this context via
Ti.include. For this and other reasons, use of
Ti.include is discouraged, and will be replaced in the future by the CommonJS module specification's
When separating code into external files, the
require function provided by the CommonJS module spec should be used to inject additional functionality into the global scope or the scope of a module. This technique will be explained later on in the document.
While there are a few reasonable use cases for this approach, such as an "app within an app" where every new window requires a "clean slate" with no dependencies on the global context, normally windows with URLs should not be used.
Applications shall be modular and object-oriented using CommonJS modules
exports object within a CommonJS module file. A sample usage of the CommonJS module spec in Titanium Mobile is below.
An instantiable object in a CommonJS module would look like this:
Applications shall defer script loading until absolutely needed
Applications shall take reasonable steps to manage memory
In very large applications, it occasionally becomes necessary to force Titanium to clean up resources. To do this, the developer has a few things to track:
- the windows can be closed. This should cause Titanium to free up the resources reserved for the view objects added to the window.
- proxy objects can be set to null. This ensures that these objects are garbage collected to release the associated native objects.