separation of logic and UI in titanium (javascript)

无人久伴 提交于 2019-11-29 07:13:35

ok, i just found a cool practice.

i include the con_file.js with the application logic the view_file.js with

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

now i'm able to access the hole data structure.

i'll have a try:

i tend to use the mvc-pattern for developing my application since implementing all stuff in one single js-file is quite ugly. so i decided to use one file for the view and all the stuff concering the look-and-feel, one file for the database handling (the controller), especially the sql-statements, and one file for the abstract data type (the model).

a 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;
};

so the folder structure could be like this:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!