Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  "1de5770c-1431-3b15-539b-695cedf3a415":{
    "title":"First run",
    "id":"1de5770c-1431-3b15-539b-695cedf3a415"
  },
  // Created after second run
  "26c1fdb7-5803-a61f-ca12-2701dba9a09e":{
    "0":{
      "title":"First run",
      "id":"1de5770c-1431-3b15-539b-695cedf3a415"
    },
      "title":"Second run",
      "id":"26c1fdb7-5803-a61f-ca12-2701dba9a09e"
    }
  }
like image 591
Sebastian Avatar asked Sep 25 '11 08:09

Sebastian


People also ask

Can you save an object to local storage?

Local storage can only save strings, so storing objects requires that they be turned into strings using JSON. stringify - you can't ask local storage to store an object directly because it'll store “[object Object]”, which isn't right at all! That also means the object must be run through JSON.

Is local storage a good idea?

If it's sensitive information or data that is better suited to be stored in the server, then it shouldn't be stored in Local or SessionStorage. But if you want your user to be able to toggle light or dark mode and have that state persisted, then LocalStorage is a good option!

Where local storage is saved?

Many browser extensions store their data in the browser's so-called Local Storage, which is nothing else than a storage location managed by the web browser. And as the same suggests, all is saved locally on the machine where the browser is installed. Local storage is not in the cloud.


2 Answers

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;
s.fetch();

I changed to

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

localStorage adapter check for id like

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

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

like image 196
Igor Alekseev Avatar answered Sep 23 '22 19:09

Igor Alekseev


I'm new to backbone.js too, but it looks like the persistence model is analogous to database tables. That is to say, it's designed to create/delete/read records from a table. The localStorage adapter does the same, so what you are doing there is creating a Msg "table" in localStorage, and creating a new Msg "record" each time, and the adapter gives each new Msg a unique id.

If you just have one object, it's probably easier to just use localStorage directly. The API is really straight forward:

localStorage.setItem("key","value");

Keep in mind that localStorage only deals with key/value pairs as strings, so you'd need to convert to/from string format.

Take a look a this question for more on doing that:

Storing Objects in HTML5 localStorage

like image 41
vihacker Avatar answered Sep 21 '22 19:09

vihacker