diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/idbdatabase | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/idbdatabase')
4 files changed, 616 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html b/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html new file mode 100644 index 0000000000..99ea2480df --- /dev/null +++ b/files/zh-cn/web/api/idbdatabase/createobjectstore/index.html @@ -0,0 +1,178 @@ +--- +title: IDBDatabase.createObjectStore() +slug: Web/API/IDBDatabase/createObjectStore +translation_of: Web/API/IDBDatabase/createObjectStore +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBDatabase")}} 接口的 <strong><code>createObjectStore()</code></strong> 方法创建并返回一个新的 object store 或 index。</p> +</div> + +<p>此方法接受一个参数作为 store 的名称,也接受一个可选的参数对象让你可以定义重要的可选属性。你可以用这个属性唯一的标识此 store 中的每个对象。因为参数是一个标识符,所以 store 中的每一个对象都应有此属性并保证此属性唯一。</p> + +<p>此方法只能在 <a href="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE"><code>versionchange</code></a> 事务中被调用。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>objectStore</em> = <em>IDBDatabase</em>.createObjectStore(<em>name</em>); +var <em>objectStore</em> = <em>IDBDatabase</em>.createObjectStore(<em>name</em>, <em>options</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>被创建的 object store 的名称。请注意创建空名称的 object store 是被允许的。</dd> + <dt>optionalParameters {{optional_inline}}</dt> + <dd> + <p>可选的对象,它的属性是此方法的可选参数,其中包括以下的属性:</p> + + <table class="standard-table"> + <thead> + <tr> + <th scope="col">Attribute</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>keyPath</code></td> + <td> + <p><a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_keypath">key path</a> 被用在新的 object store 上。如果为空或未指定,object store 创建时将没有 key path,而是使用 <a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_outofline_key">out-of-line keys</a> 。你也能传一个数组作为 <code>keyPath</code> 。</p> + </td> + </tr> + <tr> + <td><code>autoIncrement</code></td> + <td>如果为 <code>true</code>, object store 有一个 <a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_keygenerator">key generator</a>. 默认为 <code>false</code>。</td> + </tr> + </tbody> + </table> + + <p>未知参数将被忽略。</p> + </dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>{{domxref("IDBObjectStore")}}</dt> + <dd>新创建的 object store 对象。</dd> +</dl> + +<h3 id="异常">异常</h3> + +<p>This method may raise a此方法可能会抛出一个 {{domxref("DOMException")}} 带有以下所列其中一种类型的 {{domxref("DOMError")}} :</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>InvalidStateError</code></td> + <td> + <p>在非<code>versionchange</code>事务中调用时发生。在一些旧版本的 Webkit 浏览器,你必须先调用{{APIRef("IDBVersionChangeRequest.setVersion")}}方法。</p> + </td> + </tr> + <tr> + <td><code>TransactionInactiveError</code></td> + <td> + <p>如果对不存在的源数据库发出请求(例如,已被删除的)。此外,在 Firefox 版本小于 41 中,会抛出误导性的 <code>InvalidStateError</code> 错误,这一问题现已修复(请参阅 {{Bug("1176165")}})。</p> + </td> + </tr> + <tr> + <td><code>ConstraintError</code></td> + <td> + <p>数据库中已存同名的对象存储(名字区分大小写)</p> + </td> + </tr> + <tr> + <td><code><a href="/en-US/docs/IndexedDB/IDBDatabaseException#NON_TRANSIENT_ERR">I</a>nvalidAccessError</code></td> + <td> + <p>如果 <code>autoIncrement</code> 设置为 true,并且 keyPath 是空字符串或包含空字符串的数组。</p> + </td> + </tr> + </tbody> +</table> + +<h2 id="Example">Example</h2> + +<pre class="brush: js;highlight:[18]"> // 打开一个数据库 + var request = window.indexedDB.open("toDoList", 4); + + // This handler is called when a new version of the database + // is created, either when one has not been created before + // or when a new version number is submitted by calling + // window.indexedDB.open(). + // This handler is only supported in recent browsers. + request.onupgradeneeded = function(event) { + var db = event.target.result; + + db.onerror = function(event) { + note.innerHTML += '<li>Error loading database.</li>'; + }; + + // Create an objectStore for this database + + var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" }); + + // define what data items the objectStore will contain + + objectStore.createIndex("hours", "hours", { unique: false }); + objectStore.createIndex("minutes", "minutes", { unique: false }); + objectStore.createIndex("day", "day", { unique: false }); + objectStore.createIndex("month", "month", { unique: false }); + objectStore.createIndex("year", "year", { unique: false }); + + objectStore.createIndex("notified", "notified", { unique: false }); + + note.innerHTML += '<li>Object store created.</li>'; + };</pre> + +<h2 id="Specification">Specification</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBDatabase-createObjectStore-IDBObjectStore-DOMString-name-IDBObjectStoreParameters-optionalParameters', 'createObjectStore()')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbdatabase-createobjectstore", "createObjectStore()")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.IDBDatabase.createObjectStore")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbdatabase/deleteobjectstore/index.html b/files/zh-cn/web/api/idbdatabase/deleteobjectstore/index.html new file mode 100644 index 0000000000..67e4d08b30 --- /dev/null +++ b/files/zh-cn/web/api/idbdatabase/deleteobjectstore/index.html @@ -0,0 +1,114 @@ +--- +title: IDBDatabase.deleteObjectStore() +slug: Web/API/IDBDatabase/deleteObjectStore +translation_of: Web/API/IDBDatabase/deleteObjectStore +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p> <strong><code>deleteObjectStore()</code></strong> 方法从 {{domxref("IDBDatabase")}} 中销毁指定名称的对象存储,及这个对象存储所包含的任何索引。</p> +</div> + +<p>与 {{ domxref("IDBDatabase.createObjectStore") }} 一样,<font><font>此方法</font></font><em><font><font>只能</font></font></em><font><font>在</font></font><a href="https://developer.mozilla.org/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE"><code>versionchange</code></a><font><font>事务中</font><font>调用</font><font>。</font></font></p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>dbInstance</em>.deleteObjectStore(<em>name</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>name</code></dt> + <dd>将要删除的对象存储的名字</dd> +</dl> + +<h3 id="异常">异常</h3> + +<p>此方法可能会引发下列 {{domxref("DOMException")}} 异常:</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>InvalidStateError</code></td> + <td>Occurs if the method was not called from a <code>versionchange</code> transaction callback. For older WebKit browsers, you must call {{ APIRef("IDBVersionChangeRequest.setVersion")}} first.</td> + </tr> + <tr> + <td><code>TransactionInactiveError</code></td> + <td>Occurs if a request is made on a source database that doesn't exist (e.g. has been deleted or removed.) In Firefox previous to version 41, an <code>InvalidStateError</code> was raised in this case as well, which was misleading; this has now been fixed (see {{Bug("1176165")}}.)</td> + </tr> + <tr> + <td><code>NotFoundError</code></td> + <td>You are trying to delete an object store that does not exist. Names are case sensitive.</td> + </tr> + </tbody> +</table> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">var dbName = "sampleDB"; +var dbVersion = 2; +var request = indexedDB.open(dbName, dbVersion); + +request.onupgradeneeded = function(e) { + var db = request.result; + if (e.oldVersion < 1) { + db.createObjectStore("store1"); + } + + if (e.oldVersion < 2) { + db.deleteObjectStore("store1"); + db.createObjectStore("store2"); + } + + // etc. for version < 3, 4... +};</pre> + +<h2 id="Specification"><span style="font-size: 2.14285714285714rem;">Specification</span></h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBDatabase-deleteObjectStore-void-DOMString-name', 'deleteObjectStore()')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbdatabase-deleteobjectstore", "deleteObjectStore()")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.IDBDatabase.deleteObjectStore")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbdatabase/index.html b/files/zh-cn/web/api/idbdatabase/index.html new file mode 100644 index 0000000000..1bf1663f7b --- /dev/null +++ b/files/zh-cn/web/api/idbdatabase/index.html @@ -0,0 +1,225 @@ +--- +title: IDBDatabase +slug: Web/API/IDBDatabase +tags: + - IDBDatabase +translation_of: Web/API/IDBDatabase +--- +<p>{{APIRef("IndexedDB")}}</p> + +<div> +<p>IndexedDB 中的 <strong><code>IDBDatabase</code></strong> 接口提供一个到 <a href="/en-US/docs/IndexedDB#database_connection">数据库的连接</a>; 你可以使用 <code>IDBDatabase</code> 对象在数据库中打开一个<a href="/en-US/docs/IndexedDB#gloss_transaction">transaction</a> , 然后进行操作或者删除数据库中的对象。这是唯一一个能够访问和管理数据库版本的接口。 </p> + + + +<p>{{AvailableInWorkers}}</p> +</div> + +<div class="note"> +<p><strong>注意</strong>:在IndexedDB中所做的所有事情总是发生在<a href="/en-US/docs/IndexedDB/Basic_Concepts_Behind_IndexedDB#gloss_transaction">事务</a>的上下文中,表示与数据库中的数据的交互。IndexedDB中的所有对象——包括对象存储、索引和游标——都与特定事务绑定。因此,在事务之外您不能执行命令、访问数据或打开任何东西。</p> +</div> + +<div class="note"> +<p><strong>注意</strong>:请注意,从Firefox 40开始,IndexedDB事务具有宽松的持久性保证以提高性能(请参阅<a class="external external-icon" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1112702" rel="noopener" title="FIXED: Switch IndexedDB transactions to be non-durable by default">bug 1112702</a>)以前在<code>readwrite</code>事务中<a class="new" href="/zh-CN/docs/Web/API/IDBTransaction/oncomplete" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>IDBTransaction.oncomplete</code></a>被触发只有当所有数据都保证已刷新到磁盘时。在Firefox 40+中,<code>complete</code>事件在操作系统被告知写入数据之后被触发,但可能在该数据实际上被刷新到磁盘之前。该<code>complete</code>因此,事件可以比以前更快地传递,但是,如果操作系统崩溃或者在将数据刷新到磁盘之前系统电源丢失,则整个事务将丢失的可能性很小。由于这种灾难性事件很少见,大多数消费者不应该进一步关注自己。<font>如果由于某种原因必须确保持久性(例如,您要存储以后无法重新计算的关键数据),则可以<code>complete</code>通过使用实验(非标准)<code>readwriteflush</code>模式创建事务来强制事务在传递事件之前刷新到磁盘(请参阅<a class="new" href="/zh-CN/docs/Web/API/IDBDatabase/transaction" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>IDBDatabase.transaction</code></a>)。</font></p> +</div> + +<h2 id="方法"><font>方法</font></h2> + +<p><font>继承自: <a href="/en-US/docs/DOM/EventTarget">EventTarget</a></font></p> + +<dl> + <dt><font>{{domxref("IDBDatabase.close()")}}</font></dt> + <dd><font>在一个单独的线程中关闭数据库连接并立即返回。</font></dd> + <dt><font>{{domxref("IDBDatabase.createObjectStore()")}}</font></dt> + <dd><font>创建并返回一个新的 object store 或者 index。</font></dd> + <dt><font>{{domxref("IDBDatabase.deleteObjectStore()")}}</font></dt> + <dd><font>根据给定的名字,删除在当前连接的数据库中的 object store 和 相关的索引。 </font></dd> + <dt><font>{{domxref("IDBDatabase.transaction()")}}</font></dt> + <dd><font>立即返回一个包含{{domxref("IDBTransaction.objectStore")}} 方法的 transaction 对象。你可以用这个对象来操作object store。这个操作是在一个单独的线程中执行的。</font></dd> +</dl> + +<h2 id="属性"><font>属性</font></h2> + +<dl> + <dt><font>{{domxref("IDBDatabase.name")}} {{readonlyInline}}</font></dt> + <dd><font>{{ domxref("DOMString") }}类型,当前连接数据库名 。</font></dd> + <dt><font>{{domxref("IDBDatabase.version")}} {{readonlyInline}}</font></dt> + <dd><font><a href="/en-US/docs/NSPR_API_Reference/Long_Long_(64-bit)_Integers">64-bit </a>整型数,当前连接数据库的版本 。当数据第一次被创建时,这个属性是一个空的字符串。 </font></dd> + <dt><font>{{domxref("IDBDatabase.objectStoreNames")}} {{readonlyInline}}</font></dt> + <dd><font>{{ domxref("DOMStringList") }}类型,当前连接连接数据库中所有的object store 名字列表。</font></dd> +</dl> + +<h3 id="Event_handlers"><font>Event handlers</font></h3> + +<dl> + <dt><font>{{domxref("IDBDatabase.onabort")}}</font></dt> + <dd><font>在中断数据库访问时触发。</font></dd> + <dt><font>{{domxref("IDBDatabase.onerror")}}</font></dt> + <dd><font>当访问数据库失败时触发。</font></dd> + <dt><font>{{domxref("IDBDatabase.onversionchange")}}</font></dt> + <dd> + <p><font>当数据库结构发生更改时触发</font></p> + + <p><font>({{domxref("IDBOpenDBRequest.onupgradeneeded")}}事件或</font>在其他地方请求<font><code> </code>{{domxref("IDBFactory.deleteDatabase")}} 时(</font>最可能在同一台计算机上的另一个窗口/选项卡中<font>)</font>这与版本更改事务(请参阅参考资料<font>{{domxref("IDBVersionChangeEvent")}})</font>不同,但它是相关的。</p> + </dd> +</dl> + +<h2 id="example" name="example"><font>示例</font></h2> + +<p><font>在下面的代码中, 异步打开了一个数据库连接 ({{domxref("IDBFactory")}}), 并处理成功或者异常事件, 如果触发了upgrade事件就需要创建一个新的object store ({{ domxref("IDBdatabase") }})。如果想看完整的例子, 可以使用 <a href="https://github.com/mdn/to-do-notifications/">To-do Notifications</a> 应用(<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</font></p> + +<pre class="brush: js;highlight:[13,24,26,27,28,32] notranslate"><font> +// 我们先打开一个数据库 + var DBOpenRequest = window.indexedDB.open("toDoList", 4); + + // 当数据库打开出错/成功时,以下两个事件处理程序将分别对IDBDatabase对象进行下一步操作 + DBOpenRequest.onerror = function(event) { + note.innerHTML += '<li>Error loading database.</li>'; + }; + + DBOpenRequest.onsuccess = function(event) { + note.innerHTML += '<li>Database initialised.</li>'; + + // 将打开数据库的结果存储在db变量中,该变量将在后面的代码中被频繁使用 + db = DBOpenRequest.result; + + // 运行displayData()方法,用IDB中已经存在的所有待办事项列表数据填充到任务列表中 + displayData(); + }; + + // 当试图打开一个尚未被创建的数据库,或者试图连接一个数据库还没被创立的版本时,onupgradeneeded事件会被触发 + + DBOpenRequest.onupgradeneeded = function(event) { + var db = event.target.result; + + db.onerror = function(event) { + note.innerHTML += '<li>Error loading database.</li>'; + }; + + // 使用IDBDatabase.createObjectStore方法,可创建一个对象存储区 + + var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" }); + + // 定义objectStore将包含哪些数据项 + + objectStore.createIndex("hours", "hours", { unique: false }); + objectStore.createIndex("minutes", "minutes", { unique: false }); + objectStore.createIndex("day", "day", { unique: false }); + objectStore.createIndex("month", "month", { unique: false }); + objectStore.createIndex("year", "year", { unique: false }); + + objectStore.createIndex("notified", "notified", { unique: false }); + + note.innerHTML += '<li>Object store created.</li>'; + };</font></pre> + +<p><font>下一行打开数据库上的事务,然后打开一个对象存储,然后我们可以在其中操作数据。</font></p> + +<pre class="brush: js notranslate"><font> + var objectStore = db.transaction('toDoList').objectStore('toDoList'); </font></pre> + +<h2 id="规范"><font>规范</font></h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col"><font>Specification</font></th> + <th scope="col"><font>Status</font></th> + <th scope="col"><font>Comment</font></th> + </tr> + <tr> + <td><font>{{SpecName('IndexedDB', '#idl-def-IDBDatabase', 'IDBDatabase')}}</font></td> + <td><font>{{Spec2('IndexedDB')}}</font></td> + <td><font> </font></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility"><font>浏览器兼容性</font></h2> + +<div><font>{{CompatibilityTable}}</font></div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th><font>Feature</font></th> + <th><font>Chrome</font></th> + <th><font>Firefox (Gecko)</font></th> + <th><font>Internet Explorer</font></th> + <th><font>Opera</font></th> + <th><font>Safari (WebKit)</font></th> + </tr> + <tr> + <td><font>Basic support</font></td> + <td><font>23{{property_prefix("webkit")}}<br> + 24</font></td> + <td><font>10 {{property_prefix("moz")}}<br> + {{CompatGeckoDesktop("16.0")}}</font></td> + <td><font>10, partial</font></td> + <td><font>15</font></td> + <td><font>7.1</font></td> + </tr> + <tr> + <td><font>Available in workers</font></td> + <td><font>{{CompatVersionUnknown}}</font></td> + <td><font>{{CompatGeckoMobile("37.0")}}</font></td> + <td><font>{{CompatUnknown}}</font></td> + <td><font>{{CompatVersionUnknown}}</font></td> + <td><font>{{CompatUnknown}}</font></td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th><font>Feature</font></th> + <th><font>Android</font></th> + <th><font>Firefox Mobile (Gecko)</font></th> + <th><font>Firefox OS</font></th> + <th><font>IE Phone</font></th> + <th><font>Opera Mobile</font></th> + <th><font>Safari Mobile</font></th> + </tr> + <tr> + <td><font>Basic support</font></td> + <td><font>4.4</font></td> + <td><font>{{CompatGeckoMobile("22.0")}}</font></td> + <td><font>1.0.1</font></td> + <td><font>10</font></td> + <td><font>22</font></td> + <td><font>8</font></td> + </tr> + <tr> + <td><font>Available in workers</font></td> + <td><font>{{CompatVersionUnknown}}</font></td> + <td><font>{{CompatGeckoMobile("37.0")}}</font></td> + <td><font>{{CompatVersionUnknown}}</font></td> + <td><font>{{CompatUnknown}}</font></td> + <td><font>{{CompatVersionUnknown}}</font></td> + <td><font>{{CompatUnknown}}</font></td> + </tr> + </tbody> +</table> +</div> + +<div class="warning"> +<p><font>Be careful in Chrome as it still implements the old specification along with the new one. Similarly it still has the prefixed <code>webkitIndexedDB</code> property even if the unprefixed <code>indexedDB</code> is present.</font></p> +</div> + +<h2 id="See_also"><font>See also</font></h2> + +<ul> + <li><font><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></font></li> + <li><font>Starting transactions: {{domxref("IDBDatabase")}}</font></li> + <li><font>Using transactions: {{domxref("IDBTransaction")}}</font></li> + <li><font>Setting a range of keys: {{domxref("IDBKeyRange")}}</font></li> + <li><font>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</font></li> + <li><font>Using cursors: {{domxref("IDBCursor")}}</font></li> + <li><font>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</font></li> +</ul> + +<p><font> </font></p> diff --git a/files/zh-cn/web/api/idbdatabase/onversionchange/index.html b/files/zh-cn/web/api/idbdatabase/onversionchange/index.html new file mode 100644 index 0000000000..97227eacac --- /dev/null +++ b/files/zh-cn/web/api/idbdatabase/onversionchange/index.html @@ -0,0 +1,99 @@ +--- +title: IDBDatabase.onversionchange +slug: Web/API/IDBDatabase/onversionchange +translation_of: Web/API/IDBDatabase/onversionchange +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<p> {{domxref("IDBDatabase")}} 中的 <strong><code>onversionchange</code></strong> 事件处理器能处理版本更新事件,此事件能在任意地方 (很可能在同一台计算机上的另一个窗口/选项卡中)导致数据库结构更改({{ domxref("IDBOpenDBRequest.onupgradeneeded")}} 事件 或 {{ domxref("IDBFactory.deleteDatabase")}} 事件)的时候被触发 。</p> + +<div> +<p><strong><code>onversionchange</code></strong> 与 <code>versionchange</code> 是不相同的事件(但两者是有关联的)。</p> + +<p>{{AvailableInWorkers}}</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox" style="font-size: 14px;"><em>IDBDatabase</em>.onversionchange = <em>function</em>(event) { ... }</pre> + +<h2 id="举例">举例</h2> + +<p>本例展示了一个创建新对象仓库的 {{domxref("IDBOpenDBRequest.onupgradeneeded")}} 代码块;代码中包含用于处理失败操作的 <code style="font-style: normal;">onerror</code> 和 <code style="font-style: normal;">onabort</code> 函数,以及一个 onversionchange 函数用以在数据库结构被改变时通知用户。</p> + +<pre class="brush: js;highlight:[28,29,30,31,32]">request.onupgradeneeded = function(event) { + var db = event.target.result; + + db.onerror = function(event) { + note.innerHTML += '<li>Error opening database.</li>'; + }; + + db.onabort = function(event) { + note.innerHTML += '<li>Database opening aborted!</li>'; + }; + + // 给这个数据库创建对象仓库 + + var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" }); + + // 定义对象仓库中包含的数据项 + + objectStore.createIndex("hours", "hours", { unique: false }); + objectStore.createIndex("minutes", "minutes", { unique: false }); + objectStore.createIndex("day", "day", { unique: false }); + objectStore.createIndex("month", "month", { unique: false }); + objectStore.createIndex("year", "year", { unique: false }); + + objectStore.createIndex("notified", "notified", { unique: false }); + + note.innerHTML += '<li>Object store created.</li>'; + + db.onversionchange = function(event) { + note.innerHTML += '<li>a database change has occurred; you should refresh this + browser window, or close it down and use the other open version of + this application, wherever it exists.</li>'; + }; +};</pre> + +<h2 id="格式">格式</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">格式</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBDatabase-onversionchange', 'onversionchange')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbdatabase-onversionchange", "onversionchange")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> +<div class="hidden">本页面的兼容性表生成自结构化数据。如果你想对该数据做出贡献, 请查看 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并向我们发送请求。</div> + +<p>{{Compat("api.IDBDatabase.onversionchange")}}</p> +</div> + +<h2 id="更多参考">更多参考</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li> + <li>开始了解事务: {{domxref("IDBDatabase")}}</li> + <li>使用事务: {{domxref("IDBTransaction")}}</li> + <li>设置键值范围: {{domxref("IDBKeyRange")}}</li> + <li>检索与修改数据: {{domxref("IDBObjectStore")}}</li> + <li>使用游标: {{domxref("IDBCursor")}}</li> + <li>参考用例: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> + <li><code><a href="/en-US/docs/Web/API/IDBDatabase/versionchange_event">versionchange</a></code> 事件</li> +</ul> |