【问题标题】:Storing values in a newly created objectStore将值存储在新创建的 objectStore 中
【发布时间】:2014-09-15 05:22:25
【问题描述】:

this example中,作者使用了以下行:

var customerObjectStore = 
  db.transaction("customers", "readwrite").objectStore("customers");

我不明白为什么“客户”被使用了两次。 我已经尝试重命名其中任何一个,看看它可能会在哪里产生影响并且示例中断,因此它们显然有必要保留相同的名称。

【问题讨论】:

    标签: javascript indexeddb persistent-object-store


    【解决方案1】:

    transaction 方法有两个参数。第一个是您要使用的表(对象存储)数组,第二个是访问类型。在您的示例中,您只想使用一个表,因此您使用了字符串而不是数组。但是如果你想处理大型项目,你应该像这样使用多个表:

    var trans = db.transaction(["customers", "payments"], "readwrite");
    var customerObjectStore = trans.objectStore("customers");
    var paymentObjectStore = trans.objectStore("payments");
    

    希望这个例子能解决你的困惑。

    【讨论】:

    • 哦,所以 .objectStore 是我可以存储在变量中的交易链。是的……我明白了……
    • 确实,transaction 可以帮助您控制对表格的访问类型,objectStore 可以帮助您访问表格。这种结构显示了它在大型项目中的真正威力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2015-08-30
    • 2014-01-17
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多