基于 Indexed Database 来构建 Web 应用的本地存储(2)
 
- UID
- 1066743
|

基于 Indexed Database 来构建 Web 应用的本地存储(2)
Indexed Database API同步版本的 API 只在工作线程(Worker Threads)上有用,由于并不是所有的浏览器都支持工作线程,这里讨论的 API 都是异步的 API(Asynchronous APIs)。
异步 API 方法的返回并不需要阻塞调用者线程,所有的异步操作都返回一个 IDBRequest 实例,当操作结果可用时,可以使用这个 IBDRequest 实例来访问操作结果。
1.事件接口(Event Interface)
异步操作诸如创建数据库对象,或操作数据库对象时,就会触发事件。向数据库对象发送请求时,用户代理将其相关信息载入到内存中,当这些请求对象可以被处理时,它通过触发事件来提醒应用程序。事件如下所示:
IDBEvent 接口继承自 中定义的 Event。
清单 1. IDBEvent1
2
3
4
| [NoInterfaceObject]
interface IDBEvent : {
readonly attribute any source;
};
|
source 返回触发这个事件的异步请求对象
清单 2. IDBSuccessEvent1
2
3
4
| [NoInterfaceObject]
interface IDBSuccessEvent : IDBEvent {
readonly attribute any result;
};
|
result 返回在 source 上异步请求成功完成后的结果
清单 3. IDBTransactionEvent1
2
3
4
| [NoInterfaceObject]
interface IDBTransactionEvent : IDBSuccessEvent {
readonly attribute IDBTransaction transaction;
};
|
transaction 为 IDBTransaction 类型,返回用于请求 source 的事务
清单 4. IDBErrorEvent1
2
3
4
5
| [NoInterfaceObject]
interface IDBErrorEvent : IDBEvent {
readonly attribute unsigned short code;
readonly attribute DOMString message;
};
|
code 返回在 source 上执行异步请求时的最合适的错误代码,这个错误代码的有效值在异常 IDBDatabaseException 中定义;message 返回错误原因的描述。
清单 5. IDBVersionChangeEvent1
2
3
4
| [NoInterfaceObject]
interface IDBVersionChangeEvent : IDBEvent {
readonly attribute DOMString version;
};
|
version 返回 VERSION_CHANGE 事务发生时的数据库新版本号
2.IDBRequest 接口
IDBRequest 接口通过使用事件处理函数的属性来访问异步请求的数据库和数据库对象。例如下面的例子,我们打开一个数据库,根据不同的情形注册不同的事件处理函数:
清单 6. IDBRequest 例子1
2
3
| var request = indexedDB.open('AddressBook', 'Address Book');
request.onsuccess = function(evt) {...};
request.onerror = function(evt) {...};
|
清单 7. IDBRequest1
2
3
4
5
6
7
8
9
| [[NoInterfaceObject]
interface IDBRequest : {
void abort ();
const unsigned short LOADING = 1;
const unsigned short DONE = 2;
readonly attribute unsigned short readyState;
attribute Function onsuccess; //成功事件的处理函数
attribute Function onerror; // 错误事件的处理函数
};
|
readyState 为 unsigned short 类型 ; 每个请求都以 LOADING 状态开始,当请求结束,失败或取消时,状态变为 DONE
abort:用于中断正在执行的事务请求,此方法在 readyState 为 DONE 时无效。
3.打开数据库
对象 Window 和 Worker 实现 IDBEnvironment 接口。
清单 8. IDBEnvironment1
2
3
4
5
6
| Window implements IDBEnvironment;;
Worker implements IDBEnvironment;
[NoInterfaceObject]
interface IDBEnvironment {
readonly attribute IDBFactory indexedDB;
};
|
indexedDB 这个属性提供给应用程序一种访问 Indexed 数据库的机制;每一个异步请求的方法都返回一个 IDBRequest 对象,它通过事件与请求应用程序交互
清单 9. IDBFactory1
2
3
4
5
6
7
| [NoInterfaceObject]
interface IDBFactory {
readonly attribute DOMString databases;
IDBRequest open (in DOMString name, in DOMString description)
raises (IDBDatabaseException);
};
};
|
database 这个值是全局范围内可用的数据库名字
open 如果数据库连接打开时发生了错误,那么这个方法的返回对象将触发一个错误事件,错误事件的 code 设置为 UNKNOW_ERR,message 设置为合适的信息
4.数据库
数据库对象(database object)用来管理数据库中的对象(objects of that database)。这也是获得这个数据库事务的唯一方法。
清单 10. IDBDatabase1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| [NoInterfaceObject]
interface IDBDatabase {
readonly attribute DOMString name;
readonly attribute DOMString description;
readonly attribute DOMString version;
readonly attribute DOMStringList objectStores;
IDBObjectStore createObjectStore (in DOMString name,
[TreatNullAs=EmptyString] in optional DOMString keyPath,
in optional boolean autoIncrement) raises (IDBDatabaseException);
IDBObjectStore objectStore (in DOMString name,
in optional unsigned short mode,
in optional unsigned long timeout) raises (IDBDatabaseException);
void removeObjectStore (in DOMString name) raises (IDBDatabaseException);
IDBRequest setVersion ([TreatNullAs=EmptyString] in DOMString version);
IDBTransaction transaction (in optional DOMStringList storeNames,
in optional unsigned short mode,
in optional unsigned long timeout) raises (IDBDatabaseException);
void close ();
};
|
createObjectStore 这个方法在连接的数据库中以给定的名字创建一个新的存储对象,并且返回它;返回类型:IDBObjectStore
objectStore 这个方法在连接的数据库中隐式地创建一个事务,并以给定的名字打开存储对象;返回类型:IDBObjectStore
removeObjectStore 这个方法在连接的数据库中以给定的名字销毁一个存储对象
setVersion 设置连接的数据库的版本号;返回类型:IDBRequest
transaction 这个方法返回一个 IDBTransaction 对象,并执行创建事务的操作。
5.对象存储(object store)
对象存储实现以下接口:
清单 11. IDBObjectStore1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [NoInterfaceObject]
interface IDBObjectStore {
readonly attribute name;
readonly attribute keyPath;
readonly attribute indexNames;
IDBRequest put (in value, in optional key) raises (IDBDatabaseException);
IDBRequest add (in value, in optional key) raises (IDBDatabaseException);
IDBRequest remove (in key) raises (IDBDatabaseException);
IDBRequest get (in key) raises (IDBDatabaseException);
IDBRequest openCursor (in optional IDBKeyRange range,
in optional direction) raises (IDBDatabaseException);
IDBIndex createIndex (in name, in keyPath,
in optional unique) raises (IDBDatabaseException);
IDBIndex index (in name) raises (IDBDatabaseException);
removeIndex (in indexName) raises (IDBDatabaseException);
};
|
add 这个方法将给定的值添加到对象存储中。如果记录能够被成功地加入到对象存储中,请求对象将触发一个成功事件。如果对象存储中已经有这个记录,这个方法的请求对象将触发一个错误事件,它的 code 设置为 CONSTRAINT_ERR;返回类型:IDBRequest
createIndex 这个方法在连接的数据库中以给定的名字和参数创建一个新的索引
get 这个方法在连接的数据库中以给定的名字取得相应的记录
index 这个方法在连接的数据库中以给定的名字打开索引
remove 这个方法在对象存储中以给定的名字删除一个记录
removeIndex 这个方法在连接的数据库中以给定的名字删除一个索引 |
|
|
|
|
|