English 中文(简体)
地方推广 采用主干(js)+主干的存储数据。
原标题:Duplication localStorage data using backbone.js + backbone.localStorage.js

使用骨干+骨干的Im, 当地Storage, 坚持我的数据,我的行为是错误的:

Ive got a model context with one Depende knownuser

Settings = Backbone.Model.extend({
    localStorage : new Backbone.LocalStorage( settingsStore )
});
var settings = new Settings();
settings.set({user:  USERNAME });
settings.save();

之后,如果我产生结果,就会出现这种情况。 我收到以下数据:

  settings.attributes  
   Object  
   id: "3ac78cfb-ad60-1ab8-8391-f058ae9bfcfb"  
   user: "USERNAME"  
   __proto__: Object

Then I save the model to the localStorage, clear, and fetch it again:

settings.save();
settings.clear();
settings.fetch();

问题在于,如果我产生结果,情况就会随之而来。 属性现存于一个密封物体内:

   settings.attributes  
   Object  
   0: Object  
       id: "3ac78cfb-ad60-1ab8-8391-f058ae9bfcfb"  
       user: "USERNAME"  
       __proto__: Object  
   __proto__: Object

And the problem is when I set the user name again in order to modify, a new attribute is added like this:

   settings.attributes  
   Object  
   0: Object  
   id: "3ac78cfb-ad60-1ab8-8391-f058ae9bfcfb"  
   user: "USERNAME"  
   __proto__: Object  
   user: "NEWUSER"  
   __proto__: Object 

而且,如果我拯救这一模式,我就再次从属性上获得2个新物体,并且每次都保持增长。

最佳回答

The answer to the question given by fguillen link gives the correct answer to this problem.

如果你想正确挽救它,那么你就应该以硬编码的“ID”创建模型。

After doing this:

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

除(a)和(b)项外,这些方法正在正确运作。 显然,你必须注意不要重复2份身份证。

问题回答

暂无回答




相关问题
Is there an equivalent to localStorage in React Native?

I d like to implement the equivalent to the javascript localStorage in my React Native app. But I m unsure how to set. To function how I want it to, I would like the localStorage to be stored every ...

Local Database Storage default data

I have developed application using HTML 5 Localstorage. How can I create a TABLE and populate 10000+ rows before initializing my HTML 5 enabled application. Please suggest a pattern.

When do items in HTML5 local storage expire?

For how long is data stored in localStorage (as part of DOM Storage in HTML5) available? Can I set an expiration time for the data which I put into local storage?

Sharing variables between web workers? [global variables?]

Is there any way for me to share a variable between two web workers? (Web workers are basically threads in Javascript) In languages like c# you have: public static string message = ""; static void ...

How to store objects in HTML5 localStorage/sessionStorage

I d like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string. I can store and retrieve primitive JavaScript types and arrays using localStorage, ...

Limit of localstorage on iPhone?

I m wondering what s the limit of localstorage HTML 5 on iPhone? I read that it was like 5 Mb, but I m surprised is so little. Any ideas?

HTML5 localStorage & SQL

I understand that HTML5 "localStorage" is a key:value store but I am wondering if there is a Javascript library available that offers a more SQL-ish API?

热门标签