I'm new to web app development and I couldn't find a solution to the following problem. Basically I'm trying to get sorted the array of newest objects from IndexedDB database. Each object contains a timestamp value. I'v created an index on timestamp and I'm able to get the object with the highest value.
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);
}
Function openDB opens the database and passes the db object to the _getMails function as a parametr. Function getMails currently passes only the object with a highest timestamp value. I could iterate over the databse x(numberOfMessages) times and allways select the object with a highest timestamp while excluding the objects that are already in the array I'm trying to get. But I'm not sure if it's the most convenient way to do so. Thank You for your responses. Jan
You just need to call
cursor.continue()
inonsuccess
function. It will be called again with next cursor result.