Since I did not find a suitable MVC solution for the Titanium mobile project, I came up with the following approach. For small applications, this may be overly designed, but may help support growing applications.
Folder structure:
/Resources /model /view /controller /ui /iphone /android app.js app.jss
Separating views, models, and controllers requires a namespace, so we define it in app.js, which is our main controller:
var app = { view: {}, controller: {}, model: {}, ui: {} }
In folders, we put separate JavaScript files for each component. To do this, we could use the lightweight OOP library for JavaScript, such as MooTools or Prototype, or define simple JS functions as our objects. If you also want to inherit the parent classes, the library definitely makes sense.
Examples:
# Resources/controller/MyController.js app.controller.MyController = function() { return { getView: function() { return new app.view.MyView().getView(); } } } # Resources/view/MyView.js app.view.MyView = function() { return { getView: function() { return Ti.UI.createWindow({...}); } } } # Resources/view/MyModel.js app.model.MyModel = function() { return { some: "data", foo: "bar" } }
After that, we can include in the app.js file all the necessary model / view / controller classes with Ti.include () and reference the components with our namespace:
Ti.include("controller/MyController.js"); Ti.include("view/MyView.js"); var myController = new app.controller.MyController(); var myView = myController.getView(); myView.open();
The MVC approach now assumes that the controller “controls” the state of the view and transfers data from the model to the view. A view consists only of interface elements and styling properties. Any action performed in the user interface raises an event that tells the controller to take the required action.
But, of course, the exact definition of MVC may vary depending on your personal taste;)