【发布时间】:2014-11-26 05:30:15
【问题描述】:
我在 IndexedDB 表中有一些非常简单地包含这些数据的数据:
var Customers = [
{ ssn: "123-45-6666", name: "Andrew", age: 22, email: "andrew@hotmail.com" },
{ ssn: "555-66-7777", name: "Gail", age: 25, email: "gail@email.me" }
];
然后我有这个函数从 IndexedDB 取回数据:
function RetrieveTableRows(Table) {
var returnData = [];
var db = window.indexedDB.db;
var trans = db.transaction([Table], "readwrite");
var store = trans.objectStore(Table);
var keyRange = IDBKeyRange.lowerBound(0);
var cursorRequest = store.openCursor(keyRange);
cursorRequest.onerror = window.indexedDB.onerror;
cursorRequest.onsuccess = function(e) {
var result = e.target.result;
if(!!result == false) {
return;
}
returnData.push(result.value);
result.continue();
};
return returnData;
}
我意识到它不起作用,因为 onsuccess 函数是异步的,但是我无法找到解决方案。
简单地说,我希望能够写:
var myCustomers = RetrieveTableRows('customers');
然后能够使用变量 myCustomers - 这可能吗?
我尝试过使用JQuery.deferred(); 方法,但这似乎不起作用,我知道我可以做这样的事情:
transaction.oncomplete = function() {
ReturnTableRows(returnData);
};
}
function ReturnTableRows(data) {
//do something with the array of data
}
但我不知道如何将其传递回 myCustomers 变量。
【问题讨论】:
标签: javascript jquery arrays asynchronous indexeddb