重复命名事务和对象存储
repetition of naming both the transaction and the object store
如果交易仅针对 1 "table".
,则在 Object Store 方法中指定 "table" 的名称似乎是重复的
问:有没有办法减少重复:
var transaction = db.transaction(["toDoList"], "readwrite");
var objectStore = transaction.objectStore("toDoList");
重复的原因是您可以在多个对象存储上打开一个事务。如果您通常只是在一个对象存储上打开一个事务,您可以将它包装在一个函数中:
function getObjectStore(name) {
var transaction = db.transaction([name], "readwrite");
return transaction.objectStore(name);
}
var objectStore = getObjectStore("toDoList");
更一般地说,整个 IndexedDB API 相当冗长,使用像 http://dexie.org/ or https://github.com/jakearchibald/idb
这样的包装库会更愉快
如果交易仅针对 1 "table".
,则在 Object Store 方法中指定 "table" 的名称似乎是重复的问:有没有办法减少重复:
var transaction = db.transaction(["toDoList"], "readwrite");
var objectStore = transaction.objectStore("toDoList");
重复的原因是您可以在多个对象存储上打开一个事务。如果您通常只是在一个对象存储上打开一个事务,您可以将它包装在一个函数中:
function getObjectStore(name) {
var transaction = db.transaction([name], "readwrite");
return transaction.objectStore(name);
}
var objectStore = getObjectStore("toDoList");
更一般地说,整个 IndexedDB API 相当冗长,使用像 http://dexie.org/ or https://github.com/jakearchibald/idb
这样的包装库会更愉快