濑亚美莉在线|伊人三级|久久久久青草大香线综合精品|四虎黄色|佳佳黑丝高跟极致调教

IndexedDB之光標查詢

歡歡歡歡 發表于 2018-2-24 12:00

Querying with Cursors

Transactions can be used directly to retrieve a single item with a known key. When you want to retrieve multiple items, you need to create a cursor within the transaction. A cursor is a pointer into a result set. Unlike traditional database queries, a cursor doesn’t gather all of the result set up front. Instead, a cursor points to the fi rst result and doesn’t try to find the next until instructed to do so. 

翻譯:

事務能直接取出已知主鍵的單一條目。當你想要取出多個條目是,你需要在事務里面創建一個光標。一個光標就是結果集中的一個指針。

Cursors are created using the openCursor() method on an object store. As with other operations with IndexedDB, the return value of openCursor() is a request, so you must assign onsuccess and onerror event handlers. For example:

翻譯:

使用對象存儲上的openCursor()來創建光標。和IndexDB上的其他操作一樣,方法openCursor()的返回值是一個請求,所以你必須實現onsuccess和onerror時間。如下:

var store = db.transaction(“users”).objectStore(“users”), request = store.openCursor();

request.onsuccess = function(event){

//handle success };

request.onfailure = function(event){

//handle failure };

When the onsuccess event handler is called, the next item in the object store is accessible via event.target.result, which holds an instance of IDBCursor when there is a next item or null when there are no further items. The IDBCursor instance has several properties:

翻譯:

當成功事件被調用的時候,通過event.target.result你可以訪問對象存儲中的條目。如果還有下一條,event.target.result返回IDBCursor的實例;如果沒有下一條,則返回null;IDBCursor實例有一些屬性:

direction — A numeric value indicating the direction the cursor should travel in. The default is IDBCursor.NEXT (0) for next. Other values include IDBCursor.NEXT_ NO_DUPLICATE (1) for next without duplicates, IDBCursor.PREV (2) for previous, and IDBCursor.PREV_NO_DUPLICATE (3) for previous without duplicates.

key — The key for the object.

value — The actual object.

primaryKey — The key being used by the cursor. Could be the object key or an index key (discussed later).

翻譯:

direction — 一個數字,指示光標行進的方向。默認值是IDBCursor.NEXT (0),向前進。其他的值包含IDBCursor.NEXT_ NO_DUPLICATE (1) 向前進排除重復值。IDBCursor.PREV (2),向后退;IDBCursor.PREV_NO_DUPLICATE (3)向后退排除重復的。

key — 對象的主鍵。

value — 對象本身。’

primaryKey — 光標使用的主鍵。可以是對象的主鍵或者一個索引鍵(稍后討論)。

You can retrieve information about a single result using the following:

翻譯:

使用如下代碼,你能獲得一個單一結果的信息:

request.onsuccess = function(event){

var cursor = event.target.result;

if (cursor){ //always check

console.log(“Key: “ + cursor.key + “, Value: “ + JSON.stringify(cursor.value));

} };

Keep in mind that cursor.value in this example is an object, which is why it is JSON encoded before being displayed.

翻譯:

注意此示例中cursor.value是一個對象,所以顯示前需要JSON編碼。

A cursor can be used to update an individual record. The update() method updates the current cursor value with the specified object. As with other such operations, the call to update() creates a new request, so you need to assign onsuccess and onerror if you want to know the result:

翻譯:

一個光標能被用來更新某條記錄。 方法update()更新指定對象的光標值。和其他操作一樣,方法update()的調用創建了一個新的請求,因此如果你想知道結果,你需要給onsuccess和onerror事件柄賦值:

request.onsuccess = function(event){

var cursor = event.target.result, value, updateRequest;

if (cursor){ //always check

if (cursor.key == “foo”){

value = cursor.value; //get current value

value.password = “magic!”; //update the password

updateRequest = cursor.update(value); //request the update be saved

updateRequest.onsuccess = function(){ //handle success; };

updateRequest.onfailure = function(){ //handle failure }; } } };

You can also delete the item at that position by calling delete(). As with update(), this also creates a request:

翻譯:

通過調用方法delete(),你也能刪除那個位置的條目。和update()一樣,這樣也會創建一個請求:

request.onsuccess = function(event){

var cursor = event.target.result, value, deleteRequest;

if (cursor){ //always check

if (cursor.key == “foo”){

deleteRequest = cursor.delete(); //request the value be deleted

deleteRequest.onsuccess = function(){ //handle success; };

deleteRequest.onfailure = function(){ //handle failure }; } } };

Both update() and delete() will throw errors if the transaction doesn’t have permission to modify the object store.

翻譯:

假如事務沒有權限修改對象,udpate()和delete()都將拋出異常。

Each cursor makes only one request by default. To make another request, you must call one of the following methods:

每個光標默認只產生一次請求。為了產生其他的請求,你必須調用下列方法之一:

continue(key) — Moves to the next item in the result set. The argument key is optional. When not specified, the cursor just moves to the next item; when provided, the cursor will move to the specified key.

advance(count) — Moves the cursor ahead by count number of items.

翻譯: 

continue(key) — 移動到結果集的下個條目。參數可選。如果不指定,光標就移往下個條目;如果提供,光標則移往指定主鍵。

advance(count) — 往前移動光標跳過指定數量的條目。

Each of these methods causes the cursor to reuse the same request, so the same onsuccess and onfailure event handlers are reused until no longer needed. For example, the following iterates over all items in an object store:

上述方法都使得光標重用相同的請求。因此,相同的onsuccess和onfailure事件被重用直到不再需要。例如,下面代碼遍歷了對象存儲中的所有條目。

request.onsuccess = function(event){

var cursor = event.target.result;

if (cursor){ //always check

console.log(“Key: “ + cursor.key + “, Value: “ + JSON.stringify(cursor.value));

cursor.continue(); //go to the next one

} else {

console.log(“Done!”); } };

The call to continue() triggers another request and onsuccess is called again. When there are no more items to iterate over, onsuccess is called one last time with event.target.result equal to null.

翻譯: 

continue()的調用觸發了另一個請求,然后onsuccess被再次調用。當遍歷結束之后,onsuccess被最后一次調用,此時,event.target.result為空。