IndexedDB之设置光标方向(Setting Cursor Direction)

欢欢欢欢 发表于 2018-2-28 15:58

Setting Cursor Direction

备注:这里的光标方向常量已经换成了字符串。分别是这样对应的:

IDBCursor.NEXT  对应的是 ‘next’

IDBCursor.NEXT_NO_DUPLICATE对应的是'nextunique'

IDBCursor.PREV对应的是'prev'

IDBCursor.PREV_NO_DUPLICATE对应的是'prevunique'

调用的时候如下:

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()实际上有两个参数。第一个是IDBKeyRange实例,第二个是一个数字,指明前进的方向。这些常量被指定为IDBCursor上的常量,之前一节【光标查询】讨论过。再一次,FF4+和Chrome有不同的实现方式,因此第一步是兼容不同:

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:

翻译:

一般光标开始于对象存储里的第一个条目,并且分别通过调用方法continue()和advance()想最后一个挺近。这些光标有默认的方向值IDBCursor .NEXT。假如有重复的,你可能想要有一个跳过重复的光标。你可以通过把IDBCursor.NEXT_NO_DUPLICATE传入方法openCursor()作为第二个参数实现。

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() 的第一个参数是null,这表明默认的包含所有值的键值范围被使用。这个光标将会遍历对象存储里面的对象,从第一个条目开始,跳过所有重复的,移动到最后一个。你也可以创建向后移动通过对象存储的光标,从最后一个开始,移动到第一个,第二个参数可以传IDBCursor.PREV或者IDBCursor.PREV_NO_ DUPLICATE(同理,厚着避免重复)。例如:

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.

翻译:

当你使用IDBCursor.PREV或者IDBCursor.PREV_NO_DUPLICATE打开一个光标的时候,每一次对方法continue()或者advance() 的调用,都将光标向后移动穿越对象存储而不是向前移动。