2014-10-29 94 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