IndexedDB 之事務
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處理方法。