我试图实现嵌套集合完全一样,我发现这里的例子骨干localStorage的:https://stackoverflow.com/a/17453870/295133使用在嵌套集合
唯一的区别是,我想在本地存储数据使用localStorage的插件。
在这里,我的名单将是酒店在上面的例子:
var app = app || {};
(function(){
'use strict';
// List Collection - list of words
//---------------------
var listCollection = Backbone.Collection.extend({
//referebce to this collection's model
model: app.ListModel,
localStorage: new Backbone.LocalStorage('translate-lists')
});
app.listCollection = new listCollection();
})();
(function(){
'use strict';
app.ListModel = Backbone.Model.extend({
initialize: function() {
// because initialize is called after parse
_.defaults(this, {
words: new app.wordCollection
});
},
parse: function(response) {
if (_.has(response, "words")) {
this.words = new app.wordCollection(response.words, {
parse: true
});
delete response.words;
}
return response;
}
});
})();
什么localStorage的作用是存储ListModels,但如果我添加任何东西的话收集它很快消失我刷新后。
任何想法应该如何保存整个嵌套集合?
我刚刚意识到,也许我已经赶上了试图学习Backbone,我让事情过于复杂。骨干自己说他们不直接支持嵌套。我不能有两个单独的集合,只是使用标识符来匹配内容? – Adam 2014-10-29 06:16:43
你是否还需要为单词集合设置本地存储? – Quince 2014-10-29 12:26:53
我试过了,但它不起作用。解析方法从不触发_.has(响应,'单词')。也许这可能与它有关。 – Adam 2014-10-29 13:54:50