1

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?

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

-1

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")

1 Comment

I am trying to store a backbone.collection in my localStorage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.