The Titanium API is made up of modules--collections of related methods, properties, and constants. There are several types of modules:
- The Titanium namespace is made up of built-in modules--modules such as
Titanium.Geolocation. These modules are always available to Titanium applications.
- Packaged modules are optional extensions to the Titanium API that can be imported into your application using the
requiremethod. Packaged modules are imported using an identifier (such as
- CommonJS modules can be used in your application to structure and organize code. Unlike packaged modules, CommonJS modules are not managed by Studio or the
titaniumcommand-line interface. To import a CommonJS module, you import it using the absolute or relative path to the module inside your project's
This section deals with extending the Titanium API by building packaged modules, which can be sold through the Appcelerator Marketplace or distributed through other means.
Packaged Titanium modules
Starting with Release 3.3.0, the Titanium CLI generates one module project for multiple platforms. The module project contains platform-specific resource folders, called
mobileweb, as well as common resources for assets, documentation and examples.
To write a native module for Android or iOS, you need to be familiar with several concepts:
- Native Module Objects
- Views and View Proxies
- Events and Callbacks
TiProxy on iOS,
In many cases, a proxy object is a wrapper, or proxy, for a native object such as a UI control.For example, the
The following diagram represents what happens when this code is executed.
In this example, setting the
title property on
win causes the title to be set on the native window proxy, but the
Native module objects
A module object is the native representation of a Titanium module. It extends a native module type (
TiModule on iOS,
Your packaged module can only contain a single native module class.
Every proxy object has a parent module. The parent module usually exposes methods for creating its child proxy objects, like the
Ti.UI module provides a
Ti.UI.createButton method to create a
Views and view proxies
For UI components, there are some additional classes you'll need to use. A view proxy is a special proxy type that extends
TiViewProxy, which has additional properties and methods specific to the Titanium UI system. Each view proxy has a corresponding Titanium view type, which extends the
There are several special features about view and view proxies:
- When you set a property on the view proxy, the underlying native view may not exist. Therefore, the view proxy is responsible for maintaining the properties.
- When a view is created, it is initialized with the current set of properties stored by the view proxy.
- When a view proxy's properties are updated, the view is updated as well.
- Primitive values, such as numbers, strings and arrays can be converted to their equivalent values.
NSDictionary, for example).
- More complex types can be passed by creating a special proxy class to represent them.
Events and callbacks
- An event results in an event listener function being called on all registered listeners.
Because events can have multiple registered listeners, they are somewhat more flexible than callbacks.
- Use the UI thread when directly manipulating native UI elements.
- Avoid blocking the UI thread for any reason.
assets folder. The APIs exported by this module form the API of the packaged module.
For Android and iOS modules, the module must be formatted as a CommonJS module. For Mobile Web, the module must use the Asynchronous Module Definition (AMD) format.
animateView method, both of these methods are available on your module: