我試圖實現嵌套集合完全一樣,我發現這裏的例子骨幹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