IndexedDB 之事務(wù)
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:
翻譯:
對(duì)象商店創(chuàng)建完畢之后,所有的進(jìn)一步操作都要通過事務(wù)。事務(wù)通過數(shù)據(jù)庫對(duì)象上的方法 transaction()創(chuàng)建。任何時(shí)候你想要讀或者改數(shù)據(jù),都需要一個(gè)事務(wù)把所有的變更整合到一起。創(chuàng)建事務(wù)最簡(jiǎn)單的形式也要如下:
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:
翻譯:
不傳參,你擁有一個(gè)對(duì)數(shù)據(jù)庫所有對(duì)象的只讀權(quán)限。更好的,你能指定一個(gè)或多個(gè)你想訪問的存儲(chǔ)對(duì)象的名字:
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:
翻譯:
這樣確保在事務(wù)過程中只有users的存儲(chǔ)對(duì)象被加載并且可用。假如你想訪問不止一個(gè)的對(duì)象存儲(chǔ),第一個(gè)參數(shù),你也能傳穿字符串的數(shù)組。
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:
翻譯:
就像前面提到的,每一個(gè)這樣的事務(wù)都只能以只讀的形式訪問數(shù)據(jù)。否則,你必須傳遞第二個(gè)參數(shù)以標(biāo)明你的訪問模式。通過IDBTransaction有如下常量可用:READ_ONLY (0), READ_WRITE (1), 和VERSION_CHANGE (2);IE10+和FF4+實(shí)現(xiàn)了IDBTransaction,Chrome支持webkitIDBTransaction,因此為了規(guī)范接口,下面的代碼是有必要的:
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
With that setup, you can specify the second argument to transaction():
翻譯:
通過上面的設(shè)置,你能指定transaction()的第二個(gè)參數(shù):
var transaction = db.transaction(“users”, IDBTransaction.READ_WRITE);
This transaction is capable of both reading and writing into the users object store.
翻譯:
這個(gè)事務(wù)不僅能讀而且能寫入users對(duì)象存儲(chǔ)。
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:
翻譯:
假如你有一個(gè)對(duì)事務(wù)的引用, 你能使用方法objectStore()傳入對(duì)象存儲(chǔ)名字訪問指定的對(duì)象。然后,你能像之前那樣使用add()和put(),也可以使用get()取值。delete()移除一個(gè)對(duì)象,clear()清除所有對(duì)象。方法get()和delete()每一個(gè)方法都接收一個(gè)對(duì)象主鍵作為他們的參數(shù),并且所有這5個(gè)方法都創(chuàng)建了一個(gè)新的請(qǐng)求。例如:
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:
翻譯:
因?yàn)槿魏螖?shù)量的請(qǐng)求都能在一個(gè)單一事務(wù)中完成,所有事物本身也有事件柄:onerror和oncomplete。他們被用來提供事物級(jí)別的狀態(tài)信息:
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的事件對(duì)象(event)不會(huì)給你任何數(shù)據(jù)的訪問權(quán)限(例如get()請(qǐng)求返回的數(shù)據(jù)),因此對(duì)于這類請(qǐng)求你需要一個(gè)onsuccess處理方法。