Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
钛合金(javascript)中逻辑和UI的分离_Javascript_User Interface_Business Logic_Titanium - Fatal编程技术网

钛合金(javascript)中逻辑和UI的分离

钛合金(javascript)中逻辑和UI的分离,javascript,user-interface,business-logic,titanium,Javascript,User Interface,Business Logic,Titanium,我对appcelerators钛和javascript还不熟悉,我对编写iphone应用程序很感兴趣。我认识到创建UI需要“许多”代码。到目前为止这还没有问题,但我倾向于明智地将代码与应用程序逻辑分离。最佳实践是什么 [update]是一个很好的例子,说明了如何构建Tianium移动应用程序好的,我刚刚发现了一个很酷的做法 我包括con_file.js和view_file.js的应用程序逻辑 Titanium.include('../controller/con_file.js'); 现在我可

我对appcelerators钛和javascript还不熟悉,我对编写iphone应用程序很感兴趣。我认识到创建UI需要“许多”代码。到目前为止这还没有问题,但我倾向于明智地将代码与应用程序逻辑分离。最佳实践是什么


[update]是一个很好的例子,说明了如何构建Tianium移动应用程序

好的,我刚刚发现了一个很酷的做法

我包括con_file.js和view_file.js的应用程序逻辑

Titanium.include('../controller/con_file.js');
现在我可以访问孔数据结构了

我来试试:

我倾向于使用来开发我的应用程序,因为在一个js文件中实现所有东西是非常难看的。因此,我决定使用一个文件作为视图和所有与外观相关的内容,一个文件用于数据库处理(控制器),特别是sql语句,一个文件用于模型

举个简单的例子:

视图: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);
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;
};
模型: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();
   };

}
控制器:controllerConcerningObject.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);
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;
};
因此,文件夹结构可以如下所示:

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

您也可以尝试commonJS: