aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbobjectstore/keypath/index.html
blob: ecb161264a160f7b8d12ee0c068e8512aa6a5c81 (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
---
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 += '&lt;li&gt;Database initialised.&lt;/li&gt;';

  // 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 += '&lt;li&gt;Transaction completed.&lt;/li&gt;';
  };

  transaction.onerror = function(event) {
  note.innerHTML += '&lt;li&gt;Transaction not opened due to error. Duplicate items not allowed.&lt;/li&gt;';
  };

  // 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 += '&lt;li&gt;Request successful.&lt;/li&gt;';
  };
};</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>
<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>