Alloy Quick Start

Alloy Quick Start

Overview

This guide provides basic instructions on installing the Alloy command-line interface (CLI) and creating a quick project.

Command-Line Interface Installation

The Alloy command-line interface and plugin should be automatically installed by the studio updater. See Quick Start for instructions. If there were installation problems or if you wish to install the Alloy CLI independent of the Studio installation, refer to the manual installation instructions below.

Manual Installation

The following steps install the latest released version of Alloy.

Titanium SDK 3.0.x or later, and the Titanium CLI need to be installed and configured before installing the Alloy CLI. See Setting up the Titanium CLI for instructions.

  1. Download and install Node.js from http://nodejs.org/#download, which includes the npm package manager needed to install Alloy.

  2. From a console window, run the following command to install Alloy:

sudo npm install -g alloy

By default, these installation directions will install the latest Alloy release. To install a specific released version, use the same directions, except after 'alloy', add the at symbol ('@') with the version number. For instance, executing sudo npm install -g alloy@1.0.0 will install version 1.0.0.

Bleeding Edge Installation

To install the latest development version of Alloy, after installing Node.js , clone the Alloy GitHub repository and perform an npm install from the Alloy project's root directory:

git clone https://github.com/appcelerator/alloy.git
cd alloy
[sudo] npm install -g .

Create a Project

Using Studio

To create a new Alloy project, start Titanium Studio, then

  1. From the menu, select File > New > Mobile App Project. The New Mobile App Project wizard appears.

  2. Select Alloy in the Available Templates box, choose a template, then click the Next button.

  3. Complete all of the fields, then click the Finish button.

A new skeleton Alloy project will be generated. If you are familiar with classic Titanium development, the Resources folder is hidden from the App and Project Explorer. All work for an Alloy project is done in the app directory.

Using the CLI

To create a new Alloy project, use the Titanium CLI to create a new Titanium project, then use the Alloy CLI to generate the Alloy framework components:

titanium create --name=appname --id=com.domain.appname --platforms=android,ipad,iphone,mobileweb
cd appname
alloy new

A new skeleton Alloy project will be generated in the appname directory.

Simple Example

The following example converts the image_view_file.js file from the Titanium KitchenSink sample application to an Alloy project.

To see the example in the KitchenSink application, click on the Base UI tab, then navigate to Views > Images Views > Image File.

View

The view file declares the structure of the GUI. For example, the file below defines a window with an image and label.

Replace the contents of app/views/index.xml with the following:

<Alloy>
	<Window>
		<ImageView id="imageView" onClick="clickImage"/>
		<Label id="l">Click Image of Apple Logo</Label>
	</Window>
</Alloy>

Style

The style file formats and styles the components in the view file. For example, the style below defines the background color of the window; position, dimensions and color of the label; and position, dimensions and location of the image.

Replace the contents of app/styles/index.tss with the following:

"Window": {
	backgroundColor:"white"
},
"#l":{
	bottom:20,
	width: Ti.UI.SIZE,
	height: Ti.UI.SIZE,
	color:'#999'
},
"#imageView":{
	image:"/images/apple_logo.jpg",
	width:24,
	height:24,
	top:100
}

Controller

The controller file contains the presentation logic, which responds to input from the user. For example, the controller code below creates an alert dialog when the user clicks on the image and opens the window when the application starts.

Replace the contents of app/controllers/index.js with the following:

function clickImage(e) {
	Titanium.UI.createAlertDialog({title:'Image View', message:'You clicked me!'}).show();
}

$.index.open();

Asset

Create a folder called app/assets/images and copy the apple_logo.jpg file from the KitchenSink project. This file will be copied to Resources/images/apple_logo.jpg by Alloy during the build process.

Compile and Run

The Alloy CLI converts the view, style and controller in to a Titanium project, which can be built and ran by Studio or the Titanium CLI.

Using Studio

In the Project Explorer view, select your project, then from the global tool bar, select Run from the Launch Mode drop-down and select the device to launch the application on in the Target drop-down. Alloy will generate the Titanium files from the Alloy project files, which will then be compiled by Studio and launched on the device simulator.

For the Mobile Web platform, compile the application using the CLI, then run it with Studio. Run the following command from a console to compile the application:

alloy compile --config platform=mobileweb

Using the CLI

From a console window, go to the root directory of the project, then

titanium build [-p platform]

The Titanium CLI contains hooks to the Alloy CLI, so you do not need to run any Alloy commands.

Refer to the Titanium Command-Line Interface Reference for more information about using the titanium build command.

For the Mobile Web platform, run the application from Studio after compiling it.

More Examples

For more examples of Alloy, see Alloy Samples.

Next Steps

Review Alloy Concepts to learn more about Alloy and how to structure your project. From there, visit the links on Alloy Views, Alloy Controllers, and Alloy Models to learn how to write views, controllers and models, respectively.