Lorraine Bernard
Lorraine Bernard

Reputation: 13400

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?

Upvotes: 1

Views: 324

Answers (2)

antonjs
antonjs

Reputation: 14318

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 

Upvotes: 2

sra
sra

Reputation: 6408

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

Upvotes: -1

Related Questions