Sebastian Sebastian - 5 months ago 43
Javascript Question

Saving a model in local storage

I'm using Jerome's localStorage adapter with Backbone and it works great for collections.

But, now I have a single model that I need to save. So in my model I set:

localStorage: new Store("msg")

I then do my saves and fetch. My problem is that everytime I do a refresh and initialize my app a new representation of my model is added to localStorage, see below.

What am I doing wrong?

window.localStorage.msg = {
// Created after first run
"title":"First run",
// Created after second run
"title":"First run",
"title":"Second run",


I ran into same issue. Maybe you have something similar to this

var Settings = Backbone.Model.extend({
  localStorage: new Store("Settings"),
  defaults: { a: 1 }

var s = new Settings;

I changed to

var s = new Settings({ id: 1 });

localStorage adapter check for id like

 case "read":    resp = ? store.find(model) : store.findAll(); break;

so 0 or "" for id wont work and it will return all models in one