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 - 參考示例:待辦事項通知(檢視即時示例)。