IndexedDB之設(shè)置光標(biāo)方向(Setting Cursor Direction)
Setting Cursor Direction
備注:這里的光標(biāo)方向常量已經(jīng)換成了字符串。分別是這樣對(duì)應(yīng)的:
IDBCursor.NEXT 對(duì)應(yīng)的是 ‘next’
IDBCursor.NEXT_NO_DUPLICATE對(duì)應(yīng)的是'nextunique'
IDBCursor.PREV對(duì)應(yīng)的是'prev'
IDBCursor.PREV_NO_DUPLICATE對(duì)應(yīng)的是'prevunique'
調(diào)用的時(shí)候如下:
var request = store.openCursor(null, 'prevunique');
There are actually two arguments to openCursor(). The first is an instance of IDBKeyRange and the second is a numeric value indicating the direction. These constants are specified as constants on IDBCursor as discussed in the querying section. Firefox 4+ and Chrome once again have different implementations, so the first step is to normalize the differences locally:
翻譯:
方法openCursor()實(shí)際上有兩個(gè)參數(shù)。第一個(gè)是IDBKeyRange實(shí)例,第二個(gè)是一個(gè)數(shù)字,指明前進(jìn)的方向。這些常量被指定為IDBCursor上的常量,之前一節(jié)【光標(biāo)查詢】討論過(guò)。再一次,F(xiàn)F4+和Chrome有不同的實(shí)現(xiàn)方式,因此第一步是兼容不同:
var IDBCursor = window.IDBCursor || window.webkitIDBCursor;
Normally cursors start at the first item in the object store and progress toward the last with each call to continue() or advance(). These cursors have the default direction value of IDBCursor .NEXT. If there are duplicates in the object store, you may want to have a cursor that skips over the duplicates. You can do so by passing IDBCursor.NEXT_NO_DUPLICATE into openCursor() as the second argument:
翻譯:
一般光標(biāo)開始于對(duì)象存儲(chǔ)里的第一個(gè)條目,并且分別通過(guò)調(diào)用方法continue()和advance()想最后一個(gè)挺近。這些光標(biāo)有默認(rèn)的方向值IDBCursor .NEXT。假如有重復(fù)的,你可能想要有一個(gè)跳過(guò)重復(fù)的光標(biāo)。你可以通過(guò)把IDBCursor.NEXT_NO_DUPLICATE傳入方法openCursor()作為第二個(gè)參數(shù)實(shí)現(xiàn)。
var store = db.transaction(“users”).objectStore(“users”), request = store.openCursor(null, IDBCursor.NEXT_NO_DUPLICATE);
Note that the first argument to openCursor() is null, which indicates that the default key range of all values should be used. This cursor will iterate through the items in the object store starting from the first item and moving toward the last while skipping any duplicates. You can also create a cursor that moves backward through the object store, starting at the last item and moving toward the first by passing in either IDBCursor.PREV or IDBCursor.PREV_NO_ DUPLICATE (the latter, of course, to avoid duplicates). For example:
翻譯:
注意,方法openCursor() 的第一個(gè)參數(shù)是null,這表明默認(rèn)的包含所有值的鍵值范圍被使用。這個(gè)光標(biāo)將會(huì)遍歷對(duì)象存儲(chǔ)里面的對(duì)象,從第一個(gè)條目開始,跳過(guò)所有重復(fù)的,移動(dòng)到最后一個(gè)。你也可以創(chuàng)建向后移動(dòng)通過(guò)對(duì)象存儲(chǔ)的光標(biāo),從最后一個(gè)開始,移動(dòng)到第一個(gè),第二個(gè)參數(shù)可以傳IDBCursor.PREV或者IDBCursor.PREV_NO_ DUPLICATE(同理,厚著避免重復(fù))。例如:
var store = db.transaction(“users”).objectStore(“users”), request = store.openCursor(null, IDBCursor.PREV);
When you open a cursor using IDBCursor.PREV or IDBCursor.PREV_NO_DUPLICATE, each call to continue() or advance() moves the cursor backward through the object store instead of forward.
翻譯:
當(dāng)你使用IDBCursor.PREV或者IDBCursor.PREV_NO_DUPLICATE打開一個(gè)光標(biāo)的時(shí)候,每一次對(duì)方法continue()或者advance() 的調(diào)用,都將光標(biāo)向后移動(dòng)穿越對(duì)象存儲(chǔ)而不是向前移動(dòng)。