Backbone.js and local storage with multiple browser tabs / windows overwrite data

just a very short question about using Backbone.js with LocalStorage:

I store a list of things (Backbone collection) in LocalStorage. When my website is opened in several browser windows / tabs, and the user in both windows adds something to the list, one window changes to overwrite the changes made in another window.

If you want to try it yourself, just use the Backbone.js Todo sample application:

Any suggestions on how to prevent this, any standard way to handle this?

thanksx

+5
source share
3 answers

The problem is a known issue with lost concurrency updates, see Lost update in concurrency management? . For your understanding, I would suggest the following quick and dirty fix, file backbone-localstorage.js, Store.prototype.save:

save: function() {
    // reread data right before writing
    var store = localStorage.getItem(this.name);
    var data = (store && JSON.parse(store)) || {};
    // we may choose what is overwritten with what here
    _.extend(this.data, data);

    localStorage.setItem(this.name, JSON.stringify(this.data));
}

For the latest version of Github for LocalStorage LAN , I think it should look like this:

save: function() {
  var store = this.localStorage().getItem(this.name);
  var records = (store && store.split(",")) || [];
  var all = _.union(records, this.records);
  this.localStorage().setItem(this.name, all.join(","));
}
+4
source
+2
source

, , . , localStorage , , , . .

, , , , . , .

You need to think about not losing something that I was in the middle of adding (for example, I start to enter a new entry for my to-do list), switch to another tab and delete something, and then go back, I I want the entry to disappear, but my partially printed new item will still be available to me.

+2
source

All Articles