Home Alloymodel 数据绑定 Databinding 2 增删改查 Crud
Post
Cancel

Alloymodel 数据绑定 Databinding 2 增删改查 Crud

注意: 看明白这篇文章的基础,是需要知道Titanium中基本的数据库操作. 见:  http://docs.appcelerator.com/titanium/latest/#!/guide/Working_with_a_SQLite_Database-section-29004901_WorkingwithaSQLiteDatabase-CreatingandInstallingDatabases

refer to:  http://docs.appcelerator.com/titanium/latest/#!/guide/Alloy_Sync_Adapters_and_Migrations

直接看如何使用(最头疼的地方) 

先配置model:    

// app/modes/book.js   ,  有两个列: title, id.   数据库中有18条记录
//migration, db_name, db_file 也是可以配置的
exports.definition = { 
  config: {
    'columns': {
      'title' : 'TEXT',
      'id' : 'INTEGER PRIMARY KEY AUTOINCREMENT'
    },  
    'adapter': {
      'type': 'sql',
      'collection_name': 'books',
      'idAttribute': 'id',
      //'migration': '20150404180000'
    }   
  },  
  extendModel: function(Model){
    _.extend(Model.prototype, {});
    return Model;
  },  
  extendCollection: function(Collection){
    _.extend(Collection.prototype, {});
    return Collection;
  }
}

再配置 view: (很简单,务必建立一个 <Model /> 标签,否则 Alloy.Models 是空,  无法使用Alloy.createModel('book') ) 

<Alloy>
  <Model src='book' instance='true'/>
  <Window class="container">
...
</Alloy>

一旦配置好了之后, 这样用:   ( 目前还不知道如何在controller中调用 sqlite中的数据) 

// 假设数据库的表: books中有18个结果
my_book = Alloy.createModel('book')
my_book   // 这时 这个变量啥内容也没有. 
my_book.fetch()  
my_book  // fetch()之后, 返回18个结果 

如果不用Model, 用Collection, 也可以查询:
books = Alloy.createCollection('book') 
books // 啥也没有
books.fetch()
books // 结果就全出来了. 

上面的看明白后,下面的基本不用看了 .  容易蒙, 而且没有成体系. 慎重! 

Sync Adapter 

就是存储的策略. 是通过web service 保存到远程的服务器,还是 保存到本地的数据库.(android, ios上的sqlite)

默认的情况是,Alloy 在创建model的时候,需要设置 Model.urlRoot 属性,或者 Collection.url. 然后Backbone会通过RESTful 的方式向远程服务器发送请求.

id 是主键. 跟MYSQL 中的id一样.

cid  是Backbone分配给某个model的临时id (client id) ,仅仅在 app运行时 有用.

下面的表格列出了 增删改查对应的 内容:

Backbone Method

Sync CRUD Method

Equivalent HTTP Method

Equivalent SQL Method

Collection.fetch

read

GET

SELECT

Collection.create (id == null)
or
Collection.create (id != null)

create
or
update

POST
or
PUT

INSERT
or
UPDATE

Model.fetch

read

GET

SELECT

Model.save (id == null)
or
Model.save (id != null)

create
or
update

POST
or
PUT

INSERT
or
UPDATE

Model.destroy

delete

DELETE

DELETE

所以,下面代码可以清晰的看出, 某个方法(fetch, save) 对应的RESTful request: 

// Since the urlRoot attribute is defined, all HTTP commands are to /library
// 思维注: 没有测试下一行代码,但是我估计 /library 应该不是完整的远程url, 是不是应该写成: 
// http://api.ooxx.com/interface/library ? 
var Book = Backbone.Model.extend({urlRoot:'/library'})  
var book = new Book();
 
// Performs a POST on /library with the arguments as a payload and the server returns the id as 1
book.save({title:'Bossypants',author:'Tina Fey',checkout:false}) 
 
// Performs a GET on /library/1
book.fetch({id:1}); 
 
// Performs a PUT on /library/1 with the entire modified object as a payload.
book.save({checkout:true});
 
// Performs a DELETE on /library/1
book.destroy();

如何设置 Ti Model中的config ? (看里面的sync方式)?

1. sql:  保存在手机上的sqlite 数据库

2. properties: 保存在Titanium的环境中.

对应的sync 文件会被复制到: Resources/alloy/sync 中.

SQLite Adapter

几个例子:

var library = Alloy.createCollection('book');
// The table name is the same as the collection_name value from the 'config.adapter' object. This may be different from the model name.
var table = library.config.adapter.collection_name;
// use a simple query
library.fetch({query:'SELECT * from ' + table + ' where author="' + searchAuthor + '"'});
// or a prepared statement
library.fetch({query: { statement: 'SELECT * from ' + table + ' where author = ?', params: [searchAuthor] }});

下面两行代码是等价的

myModel.fetch({id: 123});
// is equivalent to
myModel.fetch({query: 'select * from ... where id = ' + 123 });

database migration ( 不建议使用 , 建议直接建立新的sqlite 数据库( 如: my_db_v1, my_db_v2) 

migration 跟rails的一样, 仅仅在sql方式下生效,命名方式也跟rails的一样, 定义好两个方法: up, down

例子:

var preload_data = [
  {title: 'english book'},
  {title: 'math book'},
  {title: 'chinese book'}
]

migration.up = function(migrator){
  migrator.createTable( {
    'columns': {
      'title' : 'string'
    }
  })
  for (var i = 0; i< preload_data.length; i++){
    migrator.insertRow(preload_data[i]);
  }
}

migration.down = function(migrator){
  migrator.dropTable();
}

migrator 作为特殊的migration object, 有下列方法:

Key

Description

db

Handle to a Ti.Database instance to interact with the SQLite database. Use this handle to execute SQL calls using db.execute.

DO NOT CLOSE THIS HANDLE OR OPEN A SECOND INSTANCE OF THE DATABASE. This will cause fatal application errors.

dbname

Name of the database.

table

Name of the table. Same as value of the config.adapter.collection_name key of the Alloy Model file.

idAttribute

Name of the columns attribute to use as the primary key of the database.

createTable

Function to create a table in the database with the specified schema. Required parameter is the columns object from the configuration object of the Alloy Model file.

dropTable

Function to drop the current table from the database.

insertRow

Function to insert data into the table. Required parameter is a data object to insert in the table. Useful for preloading data.

deleteRow

Function to delete data from the table. Required parameter is a data object to remove from the table. Alloy uses an SQLite query based on the specified object to find the data to delete.

migration会被自动执行到最新.   会在 app 启动时自动执行.  

这里的官方文档没有说清楚, 尚未弄明白它是如何使用的. 所以不建议使用migration 

This post is licensed under CC BY 4.0 by the author.