I have the following method that gets items form the object store:
self.getSpecificFromDB = function(a, b, c, callback) {
self.trans = self.db.transaction(['do'], 'readonly');
self.store = self.trans.objectStore('do');
var items = [];
var cursorRequest = self.store.openCursor();
cursorRequest.onerror = function (event) {
console.log("Cursor error");
};
cursorRequest.onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
if (cursor.value.test == a) {
console.log("found it");
items.push(cursor.value);
}
cursor.continue();
}
};
callback(items);
};
I would like to use the array returned in items in the calling method of my viewmodel:
self.search = function() {
console.log("search called");
var resultarray = self.dbinstance.getSpecificFromDB(self.t(), self.d(), self.g(), function(elem) { console.log(elem);});
console.log(resultarray);
if (resultarray != undefined && resultarray.length > 0) {
self.searchitems.push({
general: resultarray[0].test,
two: resultarray[0].test2,
three: resultarray[0].test3
});
} else {
self.errormessagediv("Nothing was found");
}
};
The callback itself works, but the resultarray is always undefined, even when I use something like function (item) { return item; }
as a callback. How do I get the results to my viewmodel?