Backbone.js的:节省本地存储一个模型 [英] Backbone.js: saving one model in local storage
问题描述
我使用杰罗姆的localStorage的适配器(<一个href=\"https://github.com/jeromegn/Backbone.localStorage\">https://github.com/jeromegn/Backbone.localStorage)与骨干网和它收藏的伟大工程。
不过,现在我有我需要保存一个单一的模式。所以,在我的模型我设置:
的localStorage:新店(味精)
然后我做我的保存和读取。我的问题是,每次我做了刷新和初始化我的应用我的模型的新的再presentation被添加到本地存储,请看下面。
我在做什么错了?
window.localStorage.msg = {
//第一次运行后创建
1de5770c-1431-3b15-539b-695cedf3a415:{
头衔:首先运行
ID:1de5770c-1431-3b15-539b-695cedf3a415
},
//第二次运行后创建
26c1fdb7-5803-A61F-ca12-2701dba9a09e:{
0:{
头衔:首先运行
ID:1de5770c-1431-3b15-539b-695cedf3a415
},
头衔:第二轮
ID:26c1fdb7-5803-A61F-ca12-2701dba9a09e
}
}
我遇到了同样的问题。也许你有类似这样的东西。
VAR设置= Backbone.Model.extend({
localStorage的:新店(设置),
默认值:{A:1}
});变种S =新的设置;
s.fetch();
我改成
变种S =新的设置({ID:1});
id的
localStorage的适配器检查像
案读:RESP = model.id? store.find(模型):store.findAll();打破;
所以 0
或,
中的id不会工作,这将返回所有车型在一个
I'm using Jerome's localStorage adapter (https://github.com/jeromegn/Backbone.localStorage) 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"
}
}
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
这篇关于Backbone.js的:节省本地存储一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!