浏览器数据存储与响应式Web应用开发
1. 浏览器数据存储
1.1 通过键定位对象
在数据库中定位对象时,可使用 getProductByID 函数,该函数基于 id 属性的值来定位对象。创建数据库时,已将 id 属性指定为对象存储的键:
var objectStore = db.createObjectStore("products", {keyPath: "id"});
以下是 getProductByID 函数的代码:
function getProductByID(id, callback) {
var transaction = DBO.db.transaction(["products"]);
var objectStore = transaction.objectStore("products");
var req = objectStore.get(id);
req.onsuccess = function(e) {
callback(this.result);
};
}
此函数展示了查询数据库中对象存储的基本模式:
1. 使用 transaction 方法创建事务,声明要操作的对象存储。
2.
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



