IndexedDB 之事务

欢欢欢欢 发表于 2018-2-22 18:40

Transactions

Past the creation step of an object store, all further operations are done through transactions. A transaction is created using the transaction() method on the database object. Any time you want to read or change data, a transaction is used to group all changes together. In its simplest form, you create a new transaction as follows:

翻译:

对象商店创建完毕之后,所有的进一步操作都要通过事务。事务通过数据库对象上的方法 transaction()创建。任何时候你想要读或者改数据,都需要一个事务把所有的变更整合到一起。创建事务最简单的形式也要如下:

var transaction = db.transaction();

With no arguments specified, you have read-only access to all object stores in the database. To be more optimal, you can specify one or more object store names that you want to access:

翻译:

不传参,你拥有一个对数据库所有对象的只读权限。更好的,你能指定一个或多个你想访问的存储对象的名字:

var transaction = db.transaction(“users”);

This ensures that only information about the users object store is loaded and available during the transaction. If you want access to more than one object store, the first argument can also be an array of strings:

翻译:

这样确保在事务过程中只有users的存储对象被加载并且可用。假如你想访问不止一个的对象存储,第一个参数,你也能传穿字符串的数组。

var transaction = db.transaction([“users”, “anotherStore”]);

As mentioned previously, each of these transactions accesses data in a read-only manner. To change that, you must pass in a second argument indicating the access mode. These constants are accessible on IDBTransaction as READ_ONLY (0), READ_WRITE (1), and VERSION_CHANGE (2). While Internet Explorer 10+ and Firefox 4+ implement IDBTransaction, Chrome supports it via webkitIDBTransaction, so the following code is necessary to normalize the interface:

翻译:

就像前面提到的,每一个这样的事务都只能以只读的形式访问数据。否则,你必须传递第二个参数以标明你的访问模式。通过IDBTransaction有如下常量可用:READ_ONLY (0), READ_WRITE (1), 和VERSION_CHANGE (2);IE10+和FF4+实现了IDBTransaction,Chrome支持webkitIDBTransaction,因此为了规范接口,下面的代码是有必要的:

var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;

With that setup, you can specify the second argument to transaction():

翻译:

通过上面的设置,你能指定transaction()的第二个参数:

var transaction = db.transaction(“users”, IDBTransaction.READ_WRITE);

This transaction is capable of both reading and writing into the users object store.

翻译:

这个事务不仅能读而且能写入users对象存储。

Once you have a reference to the transaction, you can access a particular object store using the objectStore() method and passing in the store name you want to work with. You can then use add() and put() as before, as well as get() to retrieve values, delete() to remove an object, and clear() to remove all objects. The get() and delete() methods each accept an object key as their argument, and all five of these methods create a new request object. For example:

翻译:

假如你有一个对事务的引用, 你能使用方法objectStore()传入对象存储名字访问指定的对象。然后,你能像之前那样使用add()和put(),也可以使用get()取值。delete()移除一个对象,clear()清除所有对象。方法get()和delete()每一个方法都接收一个对象主键作为他们的参数,并且所有这5个方法都创建了一个新的请求。例如:

var request = db.transaction(“users”).objectStore(“users”).get(“007”);

request.onerror = function(event){ alert(“Did not get the object!”); };

request.onsuccess = function(event){

var result = event.target.result; alert(result.firstName); //”James”

};

Because any number of requests can be completed as part of a single transaction, the transaction object itself also has event handlers: onerror and oncomplete. These are used to provide transaction-level state information:

翻译:

因为任何数量的请求都能在一个单一事务中完成,所有事物本身也有事件柄:onerror和oncomplete。他们被用来提供事物级别的状态信息:

transaction.onerror = function(event){

//entire transaction was cancelled

};

transaction.oncomplete = function(event){

//entire transaction completed successfully

};

Keep in mind that the event object for oncomplete doesn’t give you access to any data returned by get() requests, so you still need an onsuccess event handler for those types of requests.

翻译:

记住,oncomplete的事件对象(event)不会给你任何数据的访问权限(例如get()请求返回的数据),因此对于这类请求你需要一个onsuccess处理方法。