aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbrequest/index.html
blob: 47e81fb300e442cc2d1a2c3a8a6d31531b9219bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
---
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>

{{Compat("api.IDBRequest")}}