How to organize JS files in a Appcelerator Titanium project

Titanium itself is essentially MVC given that your app.js file is the main controller and each View you create is the view and you pass (or set) model data against the view.

In Titanium, you can decompose your application using a couple of nice built-in mechanisms:

  1. Titanium.include – Titanium.include allows you to include one or more JS files in place much like the C #include compiler directive. You can put common functions and JS classes in this file and then include them where ever you want them imported and available.

  2. Titanium.UI.createWindow – you can create a new View as as a property of the new Window pass in a URL to another JS context which will create a new JS sub-context and allow you to maintain its own variable space (but still give you access back to your parent).

Also, in Titanium, you can create folders that allow you to logically organize your application in a way that is suitable to you and your application.

Edit: Today, the Titanium.Include method is deprecated.
As mentionned in the documentation, we should create a CommonJS module and use the require() statement.

More information about this statement : Require

More information about modules : Modules

Leave a Comment