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/idbobjectstore/keypath | |
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/idbobjectstore/keypath')
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/keypath/index.html | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbobjectstore/keypath/index.html b/files/zh-cn/web/api/idbobjectstore/keypath/index.html new file mode 100644 index 0000000000..e99f1ae519 --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/keypath/index.html @@ -0,0 +1,115 @@ +--- +title: IDBObjectStore.keyPath +slug: Web/API/IDBObjectStore/keyPath +translation_of: Web/API/IDBObjectStore/keyPath +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}}的只读属性keyPath接口返回当前objectStore的<a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_keypath">key path</a>。</p> + +<p>什么是keyPath呢?在indexedDB中,一条记录就是一个object,object里面有一个属性作为这条记录的主要依据用来进行查询,而这个属性的属性名就是keyPath,属性值就是key。在用indexedDB的get方法时,提供key,而不需要指定keyPath,因为get方法把参数作为这个最主要的属性的值,在数据库中进行查询。(译者注)</p> + +<p>如果该属性值为null,应用中必须在每一次进行修改性质的操作时提供一个key。</p> + +<p>add、put方法都可以传第二个参数,当你当前的objectStore的autoIncrement为true时,你一般不会设置keyPath,如果这个时候你在put的时候不提供第二个参数,indexedDB就不知道要更新哪一条记录了。(译者注)</p> +</div> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="句法">句法</h2> + +<pre class="syntaxbox">var <em>mykeyPath</em> = <em>objectStore</em>.keyPath;</pre> + +<h3 id="Value">Value</h3> + +<p><span style="line-height: 1.5;">任何类型。</span></p> + +<h2 id="例子">例子</h2> + +<p>在下面代码片段中,我们在数据库里打开了一个可读写的事务(transaction),并且用<code>add()</code>向一个objectStore中添加了一些数据。在objectStore被创建之后,我们在console中打印了objectStore.keyPath的值。想查看完整的例子,请查看我们的<a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a>应用(<a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">查看在线例子</a>)。</p> + +<pre class="brush: js">// Let us open our database +<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span> + note.innerHTML += '<li>Database initialised.</li>'; + + // store the result of opening the database in the db variable. + // This is used a lot below + db = DBOpenRequest.result; + + // Run the addData() function to add the data to the database + addData(); +}; + +function addData() { + // Create a new object ready to insert into the IDB + var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ]; + + // open a read/write db transaction, ready for adding the data + var transaction = db.transaction(["toDoList"], "readwrite"); + + // report on the success of the transaction completing, when everything is done + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; + }; + + transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; + }; + + // create an object store on the transaction + var objectStore = transaction.objectStore("toDoList"); + console.log(objectStore.keyPath); + + // Make a request to add our newItem object to the object store + var objectStoreRequest = objectStore.add(newItem[0]); + + objectStoreRequest.onsuccess = function(event) { + // report the success of our request + note.innerHTML += '<li>Request successful.</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-IDBObjectStore-keyPath', 'keyPath')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-keypath", "keyPath")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> +<div class="hidden">本页的兼容性列表是通过结构化数据生成的。如果你想贡献数据,请check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并给我们发pull request.</div> + +<p>{{Compat("api.IDBObjectStore.keyPath")}}</p> +</div> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li> + <li>开始学习事务 transactions: {{domxref("IDBDatabase")}}</li> + <li>使用事务 transactions: {{domxref("IDBTransaction")}}</li> + <li>值域range的使用: {{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> +</ul> |