HTML5如何判断IndexedDB游标何时结束 [英] HTML5 How to tell when IndexedDB cursor is at end
问题描述
我通过indexedDB数据存储迭代,将数据添加到JavaScript数组。如何知道光标何时结束,所以我可以对数组进行排序并对其执行操作?
I am iterating thru an indexedDB data store, adding data to a JavaScript array. How can I tell when the cursor is at the end, so I can sort the array and act on it?
当从光标检索到一行时,调用onsuccess - 当整个光标被导航时,是否有另一个回调?
onsuccess is called when a row has been retrieved from the cursor - is there another callback when the entire cursor has been navigated?
推荐答案
结果( event.target .result
)是一个游标对象或null。
The result (event.target.result
) of a successful cursor request is either a cursor object or null.
如果设置了 event.target.result
,则它是游标,您可以访问 event.target.result.value
。然后,您可以调用 event.target.result.continue()
转到下一个对象(如果有)。
If event.target.result
is set, it's the cursor, and you can access event.target.result.value
. You can then call event.target.result.continue()
to go on to the next object, if any.
如果未设置 event.target.result
,则没有更多对象。
If event.target.result
is not set, then there are no more objects.
为了说明,我的一个项目的一些代码:
For illustration, some code from a project of mine:
var collectObjects = function (request, cb) {
var objects = []
request.onsuccess = function (event) {
if (!event.target.result) return cb(null, objects)
cursor = event.target.result
objects.push(cursor.value)
cursor.continue()
}
request.onerror = function (event) {
cb(event.target.error)
}
这篇关于HTML5如何判断IndexedDB游标何时结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!