0 javascript indexeddb google-chrome-app
在我打电话之前,是否indexeddb CursorWithValue存储下一条或上一条记录cursor.continue()?我可以查看该IDBCursorWithValue对象,然后存储指向下一条记录的指针吗?
是否可以通过部分键获取第一条记录,然后仅当用户单击下一条记录时获取下一条记录,而不缓冲数组中的记录集合?
我知道我可以用来cursor.continue()获取所有匹配的记录并将其存储在数组中。我还了解,如果我只获取第一个匹配的记录,并终止onsuccess对数据库的调用被终止的函数,那么我相当确定我将失去链接到下一个记录的能力。
通过以下工作,我可以获得部分密钥的一个或所有匹配记录。有了它,\uffff我基本上得到了匹配的阿尔法和所有更大的记录。
storeGet = indexTITLE.openCursor(IDBKeyRange.bound(x.value, x.value, '\uffff'), 'next');
Run Code Online (Sandbox Code Playgroud)
这对我来说是全新的,也许我对这一切的看法都是错误的。任何建议表示赞赏。我一直在阅读这里和 github 上的每一个线程,希望其他人已经在用 indexeddb 做这件事了。
让我尝试重述一下问题:
您已经在一个范围内迭代了光标的一部分。现在您想要停止并等待用户输入,然后再继续。但交易将关闭,因此您不能继续点击。你会做什么呢?
首先:好问题!这很棘手。您有几种不同的选择。
在最简单的情况下,您有一个唯一的索引(或对象存储),因此不存在重复的键。
var currentKey = undefined;
// assumes you open a transaction and pass in the index to query
function getNextRecord(index, callback) {
var range;
if (currentKey === undefined) {
range = null; // unbounded
} else {
range = IDBKeyRange.lowerBound(currentKey, true); // exclusive
}
var request = index.openCursor(range);
request.onsuccess = function(e) {
var cursor = request.result;
if (!cursor) {
// no records found/hit end of range
callback();
return;
}
// yay, found a record. remember the key for next time
currentKey = cursor.key;
callback(cursor.value);
};
}
Run Code Online (Sandbox Code Playgroud)
如果您有一个非唯一索引,那就更棘手了,因为您需要存储索引键和主键,并且无法在该位置打开游标。(请参阅功能请求:https://github.com/w3c/IndexedDB/issues/14)因此您需要将光标前进到之前看到的 key/primaryKey 位置:
var currentKey = undefined, primaryKey = undefined;
// assumes you open a transaction and pass in the index to query
function getNextRecord(index, callback) {
var range;
if (currentKey === undefined) {
range = null; // unbounded
} else {
range = IDBKeyRange.lowerBound(currentKey, true); // exclusive
}
var request = index.openCursor(range);
request.onsuccess = function(e) {
var cursor = request.result;
if (!cursor) {
// no records found/hit end of range
callback();
return;
}
if (indexedDB.cmp(cursor.key, currentKey) === 0 &&
indexedDB.cmp(cursor.primaryKey, primaryKey) <= 0) {
// walk over duplicates until we are past where we were last time
cursor.continue();
return;
}
// yay, found a record. remember the keys for next time
currentKey = cursor.key;
primaryKey = cursor.primaryKey;
callback(cursor.value);
};
}
Run Code Online (Sandbox Code Playgroud)
我假设没有上限,例如我们希望索引中的所有记录。range您可以根据需要替换的初始化。