如何使用Backbone.LocalStorage在localStorage中存储值

时间:2012-09-27 09:56:21

标签: javascript backbone.js local-storage

如果我尝试以下代码:

var c = new Backbone.Collection(); 

c.localStorage = new Backbone.LocalStorage("items");  

c.add({title: 'exemple'});

c.toJSON(); // [object]

如果我试图看到localStorage.items我什么也看不见。

我应该如何将数据存储在localStorage中?

2 个答案:

答案 0 :(得分:2)

要将您的收藏集保存在localStorage中,您需要create a new instance of a model within a collection

var c = new Backbone.Collection(); 

c.localStorage = new Backbone.LocalStorage("items");  

c.create({title: 'exemple'});

c.toJSON(); // [object]

localStorage.items; // will be defined 

答案 1 :(得分:-1)

你想做什么? Backbones localStorage适配器用于将模型保存到localStorage而不是REST服务。如果您尝试在localStorage中存储一些额外的值,您仍然可以使用localStorage.setItem("foo", "bar")