简体   繁体   中英

How to store a value in localStorage by using Backbone.LocalStorage

If I try the following code:

var c = new Backbone.Collection(); 

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

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

c.toJSON(); // [object]

If I try to see the localStorage.items I can not see anything.

How should I store the data in localStorage?

In order to save your collection in localStorage you need to 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 

What are you trying to do? Backbones localStorage adapter is for saving models to localStorage instead of a REST service. If you try to store some additional value in localStorage you can still use localStorage.setItem("foo", "bar")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM