【问题标题】:using backbone localStorage on a nested Collection在嵌套集合上使用主干 localStorage
【发布时间】:2014-12-24 17:35:39
【问题描述】:

我正在尝试实现嵌套集合,就像我在此处找到的示例一样: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 的作用是存储 ListModel,但如果我在 words 集合中添加任何内容,刷新后它很快就会消失。

任何想法我应该如何保存整个嵌套集合?

【问题讨论】:

  • 我刚刚意识到,也许我正忙于尝试学习 Backbone,我让事情变得过于复杂。 Backbone 自己说他们不直接支持嵌套。我不能有两个单独的集合并使用标识符匹配内容吗?
  • 您是否还需要为单词集合设置本地存储?
  • 我试过了,但它不起作用。 _.has(response,'words') 的 parse 方法永远不会触发 true。也许这可能与它有关。
  • 我不记得了,但是主干本地存储是否使用model.toJSON() 方法来存储模型?如果是这样,您是否检查过 words 集合中的模型是否已存储?如果他们不是,我想我知道原因并且可能有一个修复
  • 我会调查的。为什么 listmodels 会被存储而不是 wordmodel?

标签: javascript backbone.js backbone-local-storage


【解决方案1】:

所以得到了这个工作,它归结为解析中的一些东西,但如果你想确保你只是从嵌套集合中获取属性,你应该覆盖 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(因为它会在本地存储以前的结果)并包含你想要的完整信息。

【讨论】:

  • 谢谢。我认为它正在工作,有点。我向 wordCollection 添加了一个本地存储,但是当我向 listModel 添加一个单词并运行 this.words.fetch({reset:true});所有 listModels 从存储中获取相同的 wordCollection。
  • 这很奇怪,就像他们共享同一个集合一样,如果在默认情况下实例化了一个新集合,这通常会发生,但这看起来不像你是如何设置你的模型的,所以不确定。但如果集合与列表模型一起存储,您肯定不必运行 words.fetch 吗?
  • 不应该将 ListCollection 保存到本地存储中并保存它的内容吗?没有任何意义。另外,当解析运行时,响应始终是“Object {title:“My List”,id:“45de3e40-69bb-fe36-69f0-fad11dba1f69”}”我不应该在那里得到单词集合吗?
  • 您尝试过上述方法吗?在查看主干本地存储代码时,它使用JSON.strigify,这反过来将调用模型 toJSON 方法。如果您执行 listCollection.toJSON() 的 console.log,您会得到什么?
  • 我的控制台中有一个 [object Object]。抱歉,如何暴露对象的内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多