IDBRequest:source 属性
注意:此功能在 Web Workers 中可用。
source 是 接口的一个只读属性,它返回请求的来源,例如一个 Index 或一个 object store。如果不存在来源(例如调用 IDBRequestIDBFactory.open 时),则返回 null。
值
一个代表请求来源的对象,例如 IDBIndex、IDBObjectStore 或 IDBCursor。
示例
下面的示例请求一个特定的记录标题,在 onsuccess 中从 IDBObjectStore(作为 objectStoreTitleRequest.result 可用)获取关联的记录,更新记录的一个属性,然后通过另一个请求将更新后的记录放回 object store。第二个请求的来源会打印到开发者控制台。完整的可运行示例,请参阅我们的 待办事项通知 应用(在线查看示例)。
js
const title = "Walk dog";
// Open up a transaction as usual
const objectStore = db
.transaction(["toDoList"], "readwrite")
.objectStore("toDoList");
// Get the to-do list object that has this title as its title
const objectStoreTitleRequest = objectStore.get(title);
objectStoreTitleRequest.onsuccess = () => {
// Grab the data object returned as the result
const data = objectStoreTitleRequest.result;
// Update the notified value in the object to "yes"
data.notified = "yes";
// Create another request that inserts the item
// back into the database
const updateTitleRequest = objectStore.put(data);
// Log the source of this request
console.log(`The source of this request is ${updateTitleRequest.source}`);
// When this new request succeeds, run the displayData()
// function again to update the display
updateTitleRequest.onsuccess = () => {
displayData();
};
};
规范
| 规范 |
|---|
| Indexed Database API 3.0 # ref-for-dom-idbrequest-source① |
浏览器兼容性
加载中…
另见
- 使用 IndexedDB
- 开始事务:
IDBDatabase - 使用事务:
IDBTransaction - 设置键的范围:
IDBKeyRange - 检索和修改数据:
IDBObjectStore - 使用游标:
IDBCursor - 参考示例:待办事项通知(查看实时示例)。