Separation of logic and interface in titanium (javascript)

I am new to titanium and javascript appcelerators and am interested in coding iphone application. I recognized that there is a need for β€œmany” codes to create a user interface. which is still not a problem, but I tend to separate this code from my application logic wisely. What are the best practices?

[update] tweetanium is a great example of how to structure a titanium mobile app

+6
javascript user-interface titanium business-logic
source share
2 answers

OK, I just found some cool practice.

i includes con_file.js with application logic view_file.js with

Titanium.include('../controller/con_file.js'); 

I can now access the hole data structure.

+4
source share

I will have an attempt:

I try to use mvc-pattern to develop my application, since the implementation of all the material in one js file is pretty ugly. therefore, I decided to use one file for presentation and all materials representing the appearance, one file for processing the database (controller), especially SQL statements, and one file for the abstract data type (model).

short example:

view: viewConcerningObject.js

 Ti.include('object.js'); var win = Ti.UI.currentWindow; var myObject = new object(); var myObjectName = Ti.UI.createLabel({ text:myObject.getName(); }); win.add(myObjectName); 

model: object.js

 Ti.include('controllerConceringObject.js'); function object(){ this.name = 'myInitialName'; this.getName(){ return this.name; }; this.setName(newName){ this.name = newName; }; this.updateNameFromDb(){ this.name = getNameFromDatabase(); }; } 

controller: controllerConcerningObject.js

 function getNameFromDataBase(){ var db = Ti.Database('objects'); var sql = 'SELECT name FROM objects'; var recordset = db.execute(sql); var name = recordset.field(0); recordset.close(); db.close(); return name; }; 

therefore, the folder structure may be as follows:

myProject: folderView (viewConcerningObject.js), folderModel (theDatabase.db, object.js), folderController (controllerConcerningObject.js).

+4
source share

All Articles