In the 1st part Using Appcelerator to create your mobile applications - Part 1, we briefly discussed the benefits of creating mobile applications with this technology and explained with an example how to start a project in Alloy MVC. This sample application, which will be called MiBiblioteca, works for Android and IOS thanks to Titanium SDK.
In this second part (as promised in Part 1) I will focus on the sights and styles, but also will modify some code to add more books to our mobile library.
To do this we will create an array of objects in the file Alloy.js. In Appcelerator, This file will run first, before the index.js, so it's a good place to keep our arrangement globally and thus use it in any controller.
Books define the object globally as follows:
The next step is to modify the model to save the new attribute, it is as simple as it seems, only you need to add a new column in the file **books.js**.
Since we have the updated model and arrangement of uninitialized books, it's time to change our initial screen, add all the books to the library with the following cycle:
It is simple, a bucle *for* which will array all the objects declared in **Alloy.js** and will add them to your book collection.
Our list of books have a title and an image, so we must change the code **index.xml** as follows:
What I did was remove the attributes of *TableViewRow* and use a *Label* for the title and *ImageView* for the image.
Now, ltes give it some styles!
In the above code I used three classes (book list, title and image), these are declared in the file **styles / index.tss** and its notation is similar to as is done in **CSS**
*Ti.UI.FILL* means that this *View* must fill all available space, in this case each line of books occupy the entire width of the device screen and have a height of 50.
Before proceeding with the details view, you must first change the event click on the books, that is, take the position of the book on the list and get the book under the object at that position:
With this, we end up in the file **index.js**. Now, we will edit the look in **bookdetails.xml** You must add an object type *ImageView* to display the image and add the respective classes to style as well:
**NOTE**: To declare global class files, **app.tss** is used.
Now I have to add the image to the argument *ImageView* object. We do this in the controller **bookdetails.js**
Y el resultado para Android es...
Create lists and give Alloy styles with Appcelerator is a relatively simple task, but we must bear in mind that the more complex the application, the more complex will be the views. In this regard it is essential the use of widgets pre modules and designed by the developer community. A site that I would recommend is http://gitt.io, where you will find a whole lot of Alloy widgets and Titanium Modules.
An advantage of **Alloy MVC** is the ability to reuse code by separating functionality. Thus, we can have specific views for different devices (Android, IOS), while keeping the same driver code. **Alloy drivers** usually have a one-on-one with their views, which allows access to all components and edit them at any time.