aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbrequest/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/idbrequest/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/idbrequest/index.html')
-rw-r--r--files/zh-cn/web/api/idbrequest/index.html230
1 files changed, 230 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbrequest/index.html b/files/zh-cn/web/api/idbrequest/index.html
new file mode 100644
index 0000000000..9ce7a57e72
--- /dev/null
+++ b/files/zh-cn/web/api/idbrequest/index.html
@@ -0,0 +1,230 @@
+---
+title: IDBRequest
+slug: Web/API/IDBRequest
+translation_of: Web/API/IDBRequest
+---
+<div>
+<p>{{APIRef("IndexedDB")}}</p>
+</div>
+
+<div>IndexedDB api中的IDBRequest接口提供了根据绑定事件处理函数访问结果集的方法。其中结果集来自对数据库和数据库对象发起的异步查询。对数据库的读写操作都是通过request的方式来实现。</div>
+
+<div> </div>
+
+<p><span style="line-height: inherit;">该request对象初始时不包括任何关于操作结果的信息,当request上的事件触发时,可以通过IDBRequest实例上的事件处理函数访问相关信息。</span></p>
+
+<p><span style="line-height: inherit;">继承自: </span><a href="/en/DOM/EventTarget" style="line-height: inherit;" title="en/DOM/EventTarget">EventTarget</a></p>
+
+<h2 id="About_this_document">About this document</h2>
+
+<p>This document was last updated on August 17, 2012 and follows the <a class="external" href="http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#request-api">W3C Specifications (Editor's Draft)</a> drafted on July 24, 2012. It has not yet been verified.</p>
+
+<h2 id="基础概念">基础概念</h2>
+
+<p>所有异步操作立即返回一个IDBRequest实例。每一个请求都有一个readyState属性,初始时为pending,当请求完成或失败的时候,readyState会变为done。当状态值变为done时,每一个请求都会返回result和error属性,并且会触发一个事件。当状态保持为pending时,任何尝试访问result或error属性的行为会触发一个InvalidStateError异常。</p>
+
+<p>用直白的话来说就是:所有的异步方法返回一个request对象。如果request对象成功执行了,结果可以通过result属性访问到,并且<span style="line-height: inherit;">该request对象上会触发</span><span style="line-height: inherit;">success事件。如果操作中有错误发生,一个error事件会触发,并且会通过result属性抛出一个异常。</span></p>
+
+<p><span style="font-family: Georgia, Times, 'Times New Roman', serif; font-size: 1.428em; line-height: inherit;">示例</span></p>
+
+<p>下面的代码片段中,我们异步打开一个数据库并且发起一个请求。注册了几个事件处理函数来展示不同的情况。</p>
+
+<pre class="brush: js" style="margin-left: 40px;">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:
+ ...;
+ };
+</pre>
+
+<h2 id="Attributes">Attributes</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Attribute</th>
+ <th scope="col">Type</th>
+ <th scope="col">Description</th>
+ </tr>
+ <tr>
+ <td><code><a name="attr_result">result</a></code></td>
+ <td><code>readonly any</code></td>
+ <td>
+ <p>Returns the result of the request.</p>
+
+ <p>If the the request failed and the result is not available, the <span style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;">InvalidStateError</span> exception is thrown.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code><a name="attr_errorCode">error</a></code></td>
+ <td><code>readonly <a href="/en-US/docs/DOM/DOMError" style="color: rgb(34, 85, 170); line-height: 21px;" title="/en-US/docs/DOM/DOMError">DOMError</a></code></td>
+ <td>
+ <p>The following error codes are returned under certain conditions:</p>
+
+ <ul>
+ <li><code>AbortError</code> — If you abort the transaction, then all requests still in progress receive this error.</li>
+ <li><code>ConstraintError</code> — 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.</li>
+ <li><code>QuotaExceededError</code> — If you run out of disk quota and the user declined to grant you more space.</li>
+ <li><code>UnknownError</code> — If the operation failed for reasons unrelated to the database itself. A failure due to disk IO errors is such an example.</li>
+ <li><code>NoError</code> — If the request succeeds.</li>
+ <li><code>VersionError</code> — If you try to open a database with a version lower than the one it already has.</li>
+ </ul>
+
+ <p>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 <span style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;">InvalidStateError</span> exception is thrown.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code><a name="attr_source">source</a></code></td>
+ <td><code>readonly Object</code></td>
+ <td>
+ <p>The source of the request, such as an Index or a ObjectStore. If no source exists (such as when calling <code>indexedDB.open()</code>), it returns null.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code><a name="attr_transaction">transaction</a></code></td>
+ <td><code>readonly <a href="/en/IndexedDB/IDBTransaction" title="en/IndexedDB/IDBTransaction">IDBTransaction</a></code></td>
+ <td>The transaction for the request. This property can be null for certain requests, such as for request returned from <code><a href="/en/IndexedDB/IDBFactory#open" title="en/IndexedDB/IDBFactory#open">IDBFactory.open</a></code> (You're just connecting to a database, so there is no transaction to return).</td>
+ </tr>
+ <tr>
+ <td><code><a name="attr_readyState">readyState</a></code></td>
+ <td><code>readonly enum</code></td>
+ <td>
+ <p>The state of the request. Every request starts in the <code>pending</code> state. The state changes to <code>done</code> when the request completes successfully or when an error occurs.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code><a name="attr_onerror">onerror</a></code></td>
+ <td><code>Function </code></td>
+ <td>The event handler for the error event.</td>
+ </tr>
+ <tr>
+ <td><code><a name="attr_onsuccess">onsuccess</a></code></td>
+ <td><code>Function </code></td>
+ <td>The event handler for the success event.</td>
+ </tr>
+ </thead>
+ <tbody>
+ </tbody>
+</table>
+
+<h2 id="Constants">Constants</h2>
+
+<h3 id="readyState_constants"><code>readyState</code> constants</h3>
+
+<div>{{ obsolete_header(25) }}</div>
+
+<div class="warning">
+<p>These constants are no longer available. You should use directly the string constants instead. ({{ bug(887524) }})</p>
+</div>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Constant</th>
+ <th scope="col">Value</th>
+ <th scope="col">Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><code><a name="const_done">DONE</a></code></td>
+ <td>"done"</td>
+ <td>The request has completed or an error has occurred. Initially false</td>
+ </tr>
+ <tr>
+ <td><code><a name="const_loading">LOADING</a></code></td>
+ <td>"pending"</td>
+ <td>The request has been started, but its result is not yet available.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Event_handlers">Event handlers</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Event handler</th>
+ <th scope="col">Event handler type</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><code>onerror </code></td>
+ <td><code>error </code></td>
+ </tr>
+ <tr>
+ <td><code>onsuccess </code></td>
+ <td><code>success </code></td>
+ </tr>
+ </tbody>
+</table>
+
+
+<h2 id="Derived_interface">Derived interface</h2>
+
+<ul>
+ <li><code><a href="/en/IndexedDB/IDBOpenDBRequest" title="en/IndexedDB/IDBOpenDBRequest">IDBOpenDBRequest</a></code></li>
+</ul>
+
+<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>
+
+<p>{{ CompatibilityTable() }}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>12 {{ property_prefix("-webkit") }}</td>
+ <td>{{ CompatGeckoDesktop("2.0") }}</td>
+ <td>{{ CompatNo() }}</td>
+ <td>{{ CompatNo() }}</td>
+ <td>{{ CompatNo() }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Phone</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{ CompatNo() }}</td>
+ <td>{{ CompatGeckoMobile("6.0") }}</td>
+ <td>{{ CompatUnknown() }}</td>
+ <td>{{ CompatNo() }}</td>
+ <td>{{ CompatNo() }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p> </p>