IDBRequest:source 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

sourceIDBRequest 介面的一個只讀屬性,它返回請求的來源,例如一個 Index 或一個 object store。如果不存在來源(例如呼叫 IDBFactory.open 時),則返回 null。

一個代表請求來源的物件,例如 IDBIndexIDBObjectStoreIDBCursor

示例

下面的示例請求一個特定的記錄標題,在 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①

瀏覽器相容性

另見