IDBRequest - Web API 接口参考 编辑

IndexedDB api中的IDBRequest接口提供了根据绑定事件处理函数访问结果集的方法。其中结果集来自对数据库和数据库对象发起的异步查询。对数据库的读写操作都是通过request的方式来实现。  

该request对象初始时不包括任何关于操作结果的信息,当request上的事件触发时,可以通过IDBRequest实例上的事件处理函数访问相关信息。

继承自: EventTarget

About this document

This document was last updated on August 17, 2012 and follows the W3C Specifications (Editor's Draft) drafted on July 24, 2012. It has not yet been verified.

基础概念

所有异步操作立即返回一个IDBRequest实例。每一个请求都有一个readyState属性,初始时为pending,当请求完成或失败的时候,readyState会变为done。当状态值变为done时,每一个请求都会返回result和error属性,并且会触发一个事件。当状态保持为pending时,任何尝试访问result或error属性的行为会触发一个InvalidStateError异常。

用直白的话来说就是:所有的异步方法返回一个request对象。如果request对象成功执行了,结果可以通过result属性访问到,并且该request对象上会触发success事件。如果操作中有错误发生,一个error事件会触发,并且会通过result属性抛出一个异常。

示例

下面的代码片段中,我们异步打开一个数据库并且发起一个请求。注册了几个事件处理函数来展示不同的情况。

var request = window.indexedDB.open('数据库名称');
request.onsuccess = function(event) {
        var db = this.result;
        var transaction = db.transaction([]);
// "readonly" is the default option;
// when data will be added to the database use "readwrite".
        var curRequest = transaction.objectStore('ObjectStore Name').openCursor();
        curRequest.onsuccess = ...;
    };
request.onerror = function(event) {
         ...;
    };
request.onupgradeneeded= function(event) {
         // changing objectStore data is done here, as opposed to a transaction enum:
         ...;
    };

Attributes

AttributeTypeDescription
resultreadonly any

Returns the result of the request.

If the the request failed and the result is not available, the InvalidStateError exception is thrown.

errorreadonly DOMError

The following error codes are returned under certain conditions:

  • AbortError — If you abort the transaction, then all requests still in progress receive this error.
  • ConstraintError — If you insert data that doesn't conform to a constraint. It's an exception type for creating stores and indexes. You get this error, for example, if you try to add a new key that already exists in the record.
  • QuotaExceededError — If you run out of disk quota and the user declined to grant you more space.
  • UnknownError — If the operation failed for reasons unrelated to the database itself. A failure due to disk IO errors is such an example.
  • NoError — If the request succeeds.
  • VersionError — If you try to open a database with a version lower than the one it already has.

In addition to the error codes sent to the IDBRequest object, asynchronous operations can also raise exceptions. The list describes problems that could occur when the request is being executed, but you might also encounter other problems when the request is being made. For example, if the the request failed and the result is not available, the InvalidStateError exception is thrown.

sourcereadonly Object

The source of the request, such as an Index or a ObjectStore. If no source exists (such as when calling indexedDB.open()), it returns null.

transactionreadonly IDBTransactionThe transaction for the request. This property can be null for certain requests, such as for request returned from IDBFactory.open (You're just connecting to a database, so there is no transaction to return).
readyStatereadonly enum

The state of the request. Every request starts in the pending state. The state changes to done when the request completes successfully or when an error occurs.

onerrorFunctionThe event handler for the error event.
onsuccessFunctionThe event handler for the success event.

Constants

readyState constants

已废弃 Gecko 25 (Firefox 25 / Thunderbird 25 / SeaMonkey 2.22)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

These constants are no longer available. You should use directly the string constants instead. (bug 887524)

ConstantValueDescription
DONE"done"The request has completed or an error has occurred. Initially false
LOADING"pending"The request has been started, but its result is not yet available.

Event handlers

Event handlerEvent handler type
onerrorerror
onsuccesssuccess

Derived interface

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support12 -webkit4.0 (2.0)未实现未实现未实现
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support未实现6.0 (6.0)?未实现未实现

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:49 次

字数:9820

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文