如何同步访问indexedDB? [英] How can I access indexedDB synchronously?
问题描述
indexedDB有规范,说明您可以同步访问索引数据库,但它还没有实现。
The indexedDB has a spec saying that you can access an indexed database synchronously, but it hasn't been implemented yet.
我只是想知道是否有办法让它手动同步,
I was just wondering if there is a way to make it synchronous manually,
我的JavaScript看起来像这样,
My JavaScript looks like this,
var trans = databaseAsync.transaction(["mapTile"], IDBTransaction.READ_WRITE);
var store = trans.objectStore("mapTile");
var keyRange = IDBKeyRange.bound(evt.data[0], evt.data[0]);
var cursorRequest = store.openCursor(keyRange);
// can you put some kind of wait in here?
cursorRequest.onsuccess = function(e)
{
var result = e.target.result;
if(!!result == false)
{
}
}
那么你可以在那里放一些东西让它等到onsuccess方法被调用后再继续吗?
So can you put something in there to make it wait until the onsuccess method has been called before continuing on?
我想要的原因这是上面的代码包含在这个方法中,
The reason why I want to do this is the code above is wrapped inside this method,
dojo.extend(esri.layers.ArcGISTiledMapServiceLayer, {
getTileUrl : function(level, row, col)
{
// blah
return url;
}
所以它是一个ESRI图块层(它会将图块加载到我的网页上的地图上),并且该方法需要立即返回特定图块的网址。它将是一个加载图像的URL,如果它没有缓存在数据库中,或者这个,
So it is an ESRI tile layer (which will load tiles onto a map on my web page), and that method needs to return the url straight away for a particular tile. It will either be a URL to load the image from if it isn't cached in the database already, or this,
data:image;base64,*BASE64DATA*
其中 BASE64DATA 是来自的数据以前缓存的数据库。
Where BASE64DATA is the data from the database if previously cached.
我是s之前使用localStorage,它同步工作,但是有5MB限制,所以我想我会尝试使用indexedDB。
I was previously using the localStorage for this, which works synchronously, but that has a 5MB limit so I thought I would experiment with indexedDB.
推荐答案
几个月前我问过的这个问题的接受答案。不幸的是,它依赖于仅在Firefox中支持的JavaScript 1.7(生成器)的功能。希望有一天这种技术会得到更广泛的支持。
An elegant way of doing this is provided in the accepted answer to this question I asked a couple months ago. Unfortunately, it relies on feature of JavaScript 1.7 (generators) that is only supported in Firefox. Hopefully some day this technique will be more widely supported.
这篇关于如何同步访问indexedDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!