An MVC implementation for Appcelerator – Part 2: Improvements

Hi again, in this four parts tutorial I will present some ideas and projects to implement a MVC framework for Appcelerator Titanium, a multiplatform (iOS and Android) Javascript framework to build native mobile apps.

In part 1 we discovered the structure of Appcelerator on Rails. It’s a quite well designed framework, the basic ideas are good but there are several limitations. The first problem comes from the implementation of the controller.

var MainController = Controller.extend({
    init: function(win) {
        this._super(win);
        this.view = new MainView(win, this);
    }
});

(function() {
    var win = Titanium.UI.currentWindow;
    new MainController(win);
})();

The closure at the end of this class instantiates the controller only when you include the file. If you want to instantiate one more, you must include the class file again and this isn’t good from a coding perspective. A better implementation would split definition and instantiation.

Controller file can be more compact:

var MainController = Controller.extend({
    init: function(win) {
        this._super(win);
        this.view = new MainView(win, this);
    }
});

In this way, when you use the class include file once and it will instantiate the controller every time you need it.

Ti.include("/controllers/main.js");
var mainController = new MainController(mainWin);

This, as well as having potential advantages in terms of memory usage, it’s more rational too.

Another advantage is the ability to access global variables inside the controllers and views without weird tricks, something not previously allowed.

These were functional improvements: another improvement is to clean the included code. The Appcelerator on Rails’ library files are full of code that is no longer used. As well as increasing the size and memory usage it also generates a long series of warnings that complicates debugging. Cleaning and tidying these files is a necessary work.

Fabio Barbieri, the developer behind these improvements, has made ​​a preliminary sample of the code, cleaned and fixed, that can be downloaded here.

That’s all for part 2. In the next part I will present how to build a simple app starting from these considerations.

Links:
Part 1: Introduction
Part 3: A simple demo
Part 4: Model and DB