< BACK

Alloy and Titanium Studio, Part 2. Controllers, Views, Collections and Models and how to put it all together.

When you’re working with an MVC model you need to understand the relationships within, that’s a huge part of programming this way. In Part 1, I covered how to create Windows, and pass data between them, in this tutorial I’ll show you:

  1. Create model to save data
  2. How to create collections (of items), interact with them and show them on a table view.
  3. How to go from a table view showing a collection to a detail window showing the item (model) that was selected.
  4. In between, I’ll cover how controllers and views interact with the data (models and collections).
  5. I’ll use a “Things” collection, so we’re just gonna be describing things. I do this because it’s really easy, we’re just gonna name a thing and describe it, this makes it a really easy example if we compare it with a traditional Todo app where we have states and extra code that complicates everything.
  6. This will all be made using Android.

The first step is defining how we’ll be organizing the data, we’ll have:

The collection “thing” is composed by multiple “thing”, notice I’m using “thing” insted of using “things”. The reason I’m doing it is to keep Titanium from collapsing since basically once you name a controller, model, view or collection one way, you have to use the same name for each and everyone of the other components, so I must have a thing.js file for the controller, a thing.js file for the model, a thing.js file for the view, a thing.tss for the styles and also my collection must be named “thing”. It also helps to keep things simple, although it doesn’t help with how humans name stuff.

Let’s start easy. Let’s create the model first using the Titanium Studio wizard for creating models:

// File models/thing.js exports.definition = { config: { columns: { "name": "string", "description": "text" }, adapter: { type: "sql", collection_name: "thing" } }, extendModel: function(Model) { _.extend(Model.prototype, { // extended functions and properties go here });

  return Model;   },

extendCollection: function(Collection) {
_.extend(Collection.prototype, { // extended functions and properties go here });

  return Collection;   } } </code>

Now, let’s create a new Collection, and because we want it to be globally accessible we’ll do it in the alloy.js file. In case you don’t know, we can use the alloy.js to create methods or properties that will be available through the entire app.

// File alloy.js Alloy.Collections.thing = Alloy.createCollection("thing");

Now, let’s create our controller. The controller will do basically two things, it will add some initial data to the collection by creating models, and then it will react when we select a thing that we want to see in detail. We’ll add more functionality later on.

// File controllers/thing.js

// create a variable to reference our collection var things = Alloy.Collections.thing;

// our data object it’s really simple, it has only one thing var data = { “name”: “Pencil”, “description”: “You use a pencil to write things down” }

// This is our model // we pass data to our model “thing” var thing = Alloy.createModel(“thing”, data);

// Add the “thing” model to the “things” collection things.add(thing);

// Save our model to the SQL database thing.save(thing);

// Finally, fetch the collection items things.fetch(); </code>

Next, we’ll create a View.

// File: view/thing.xml


Share this: