2014-10-29 99 views
1

我試圖實現嵌套集合完全一樣,我發現這裏的例子骨幹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,但如果我添加任何東西的話收集它很快消失我刷新後。

任何想法應該如何保存整個嵌套集合?

+0

我剛剛意識到,也許我已經趕上了試圖學習Backbone,我讓事情過於複雜。骨幹自己說他們不直接支持嵌套。我不能有兩個單獨的集合,只是使用標識符來匹配內容? – Adam 2014-10-29 06:16:43

+0

你是否還需要爲單詞集合設置本地存儲? – Quince 2014-10-29 12:26:53

+0

我試過了,但它不起作用。解析方法從不觸發_.has(響應,'單詞')。也許這可能與它有關。 – Adam 2014-10-29 13:54:50

回答

0

所以得到了這個工作,它解析了一些東西,但是如果你想確保你只是從你的嵌套集合中獲得屬性,你應該重寫toJSON,否則你會得到完整的集合。

Backbone.Model.prototype.toJSON = function() { 
    var json = _.clone(this.attributes); 
    for (var attr in json) { 
     if ((json[attr] instanceof Backbone.Model) || (json[attr] instanceof Backbone.Collection)) { 
     json[attr] = json[attr].toJSON(); 
     } 
    } 
    return json; 
    }; 

破解的主要問題在於解析。直接分配的話模型,

this.words = new app.wordCollection(response.words, { 
        parse: true 
       }); 

但這意味着它不會顯示出來的toJSON時被調用,因爲它是不是在屬性(這也意味着你無法通過model.get訪問)

所以這應該改爲

initialize: function() { 
     // because initialize is called after parse 
     _.defaults(this.attributes, { 
      words: new app.WordCollection() 
     }); 
    }, 
    parse: function (response) { 
     if (_.has(response, "words")) { 
      this.attributes.words = new app.WordCollection(response.words, { 
       parse: true 
      }); 
      delete response.words; 
     } 
     return response; 
    } 

這種方式被添加到模型的屬性上不能直接在模型上。如果你看看這個小提琴http://jsfiddle.net/leighking2/t2qcc7my/並繼續運行,它將在集合中創建一個新模型,將其保存在本地存儲器中,然後將結果打印到控制檯。每次運行時,您都會看到它增長1(因爲它會獲得以前的結果本地存儲)幷包含您想要的全部信息。

+0

謝謝。我認爲它工作,有點。我爲wordCollection添加了一個localstorage,但是當我向listModel添加一個單詞並運行this.words.fetch({reset:true});所有listModels從存儲中獲取相同的wordCollection。 – Adam 2014-10-29 15:17:55

+0

這很奇怪,就像他們共享相同的集合,這通常會發生,如果在默認情況下一個新的集合已被實例化,但看起來不像你如何設置你的模型,所以不知道這一點。但是,如果集合與列表模型一起存儲,那麼您肯定不必運行words.fetch? – Quince 2014-10-29 15:24:50

+0

不應該將ListCollection保存到localstorage保存它的內容?沒有任何意義。另外,當解析運行時,響應始終爲「Object {title:」My List「,id:」45de3e40-69bb-fe36-69f0-fad11dba1f69「}」我不應該在那裏收集單詞集合? – Adam 2014-10-29 16:09:19