使用Backbone.js的+ backbone.localStorage.js复制localStorage的数据 [英] Duplication localStorage data using backbone.js + backbone.localStorage.js

查看:154
本文介绍了使用Backbone.js的+ backbone.localStorage.js复制localStorage的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的骨干+ backbone.localStorage坚持我的数据,我得到一个错误的行为:

I'm using backbone + backbone.localStorage to persist my data, and I get a wrong behavior:

我有一个属性,叫做用户模式设置

I've got a model settings with one attribute called user

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

这code之后,如果我输出weinre的settings.attributes数据我得到以下几点:

After this code if I output the settings.attributes data in weinre I get the following:

  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,现在这个属性被存储在一个嵌套的对象中:

And the problem is that if I output the settings.attributes, now this attributes are stored inside a nested object:

   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新对象......它不断每次增长。

And if I save this model, and fetch it again I get 2 new objects on the attributes... and it keeps growing each time.

推荐答案

答案由fguillen给出的链接问题给出了正确的答案这个问题。

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

您只需要创建模型对象用硬codeDID,如果你想正确的储存。

You just need to create the model object with a hardcoded "ID" if you want to save it correctly.

这样做后:

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

在save()和fecth()方法的正常工作。很明显,你必须要小心不要重复2的ID ...

The save() and fecth() methods are working correctly. Obviously you have to take care not repeating 2 ID's...

这篇关于使用Backbone.js的+ backbone.localStorage.js复制localStorage的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆