An MVC implementation for Appcelerator – Part 1: Introduction

Hi all,
let me introduce this post by welcoming Andrea Mostosi as a new addition to our team here at PrimeGap. Andrea is one of the most brilliant coder and passionate geeks I’ve ever met.

Starting with this introduction, he will present several tools and frameworks to enhance your coding with Appcelerator Titanium.

Welcome Andrea and here’s your post.


Since Titanium was introduced, in December 2008, no MVC implementation has been proposed from Appcelerator Inc.

People who work with common MVC frameworks (like Rails, Django, …) don’t like messy files. Many MVC implementations have been proposed, but no one has yet succeeded in becoming the standard.

On the official Wiki there is a “Javascript Frameworks” section with a link to Foundation, a MVC microframework with basic features. With a Google search you can find out a few other. I personally prefer Appcelerator on Rails.

Appcelerator on Rails tries to implement most of Rails features using similar filesystem design and limited-feature Ruby-based generator.

Model generator creates standard code for accessing data

var #{model} = Model.extend({
table_name: "#{table}",
_fields: {id: Number #{fields_sql}},

find: function(id) {
var model = new #{model}(this.db, this._find(id));
return model;

item_from: function(row) {
var model = new #{model}(this.db, this._item_from(row));
return model;

Controller generator writes both View and Controller code

var #{name}View = View.extend({
init: function(win, controller) {
this._super(win, controller);

layout: function() {
// do something
var #{name}Controller = Controller.extend({
init: function(win) {
this.view = new #{name}View(win, this);

(function() {
var win = Titanium.UI.currentWindow;
new #{name}Controller(win);

Controller gets its own View using a conventional (and static) name and View has a method layout() which draws the interface. Then there is a closure which instantiates the controller.

This is an efficient strategy, but with many big downside, we will see advantages and disadvantages in part 2.

There’s also a Migration generator who generate SQL code for table creation.

migrate("#{migration}", "CREATE TABLE IF NOT EXISTS #{model} (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT #{fields_sql})");

The migrate method executes SQL code and add a new row to the migration table.

Stay tuned for Part 2, where we will see Appcelerator on Rails in action.

UPDATED on July 22nd: added the Part 2 link. Enjoy!