我正在尝试实现嵌套集合,就像我在这里找到的示例一样: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,但如果我在 words 集合中添加任何内容,它会在我刷新后很快消失。
任何想法我应该如何保存整个嵌套集合?