【发布时间】:2015-06-25 10:07:47
【问题描述】:
我是网络应用程序开发的新手,我找不到以下问题的解决方案。基本上,我正在尝试对 IndexedDB 数据库中的最新对象数组进行排序。每个对象都包含一个时间戳值。我已经在时间戳上创建了一个索引,并且能够获取具有最高值的对象。
function getMails (numberOfMessages, _function) {
/* This function uses asynchronous methods, hence you have to pass the key and function that will receive
the messageitem as a parametr */
if (typeof optionalArg === 'undefined') {
optionalArg = 10;
}
function _getMails (db) {
var transaction = db.transaction(["MessageItem"], "readonly");
var store = transaction.objectStore("MessageItem");
var index = store.index("timestamp");
var cursor = index.openCursor(null, 'prev');
var maxTimestampObject = null;
cursor.onsuccess = function(e) {
if (e.target.result) {
maxTimestampObject = e.target.result.value;
_function(maxTimestampObject);
}
};
}
openDB(_getMails);
}
函数 openDB 打开数据库并将 db 对象作为参数传递给 _getMails 函数。函数 getMails 当前仅传递具有最高时间戳值的对象。我可以遍历数据库 x(numberOfMessages) 次并始终选择具有最高时间戳的对象,同时排除我试图获取的数组中已经存在的对象。但我不确定这是否是最方便的方式。谢谢你的回复。一月
【问题讨论】:
-
你的问题陈述不是很清楚,至少对我来说..你能把它和电话顺序一起安排吗..
标签: javascript indexeddb