javascript Backbone - 嵌套在模型中的集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7288475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:35:44  来源:igfitidea点击:

Backbone - Collections nested in Models

javascriptjsonbackbone.js

提问by Jon Raasch

Is it possible to nest collections within models?

是否可以在模型中嵌套集合?

I know that you can create new collections in the initialize callback of a model, and create references that you can pass back and forth between the collection and parent model. But is it possible to set the collection as part of the model, such that the JSON it exports looks like this:

我知道您可以在模型的初始化回调中创建新集合,并创建可以在集合和父模型之间来回传递的引用。但是是否可以将集合设置为模型的一部分,使其导出的 JSON 如下所示:

{
  blah: 'blah',
  myCollection: [
      {
         foo: 'asdf',
         bar: 'qwer'
      },
      {
         foo: 'asdf123',
         bar: 'qwer123'
      }
  ]
}

If not, how do you handle syncing a model with related collections to the backend? Do you have to tap into backbone's sync and rebuild the JSON or is there something more seamless?

如果没有,您如何处理将具有相关集合的模型同步到后端?您是否必须利用主干的同步并重建 JSON 或者是否有更无缝的东西?

Sorry if this question has been answered elsewhere. I've looked around and seen some workarounds, but nothing that really answers what I'm looking for.

抱歉,如果此问题已在其他地方得到解答。我环顾四周,看到了一些解决方法,但没有什么能真正回答我正在寻找的问题。

回答by Elf Sternberg

There are two approaches. The first is to define a root Model that gets everything. You override it's parse()method to create sub-collections and sub-models for nested attributes, and override the toJSON()method to convert back to the JSON structure, suitable for saving to the server.

有两种方法。第一个是定义一个获取一切的根模型。您可以重写它的parse()方法来为嵌套属性创建子集合和子模型,并重写该toJSON()方法以转换回 JSON 结构,适合保存到服务器。

This is perfectly acceptable for small subcollections. It takes a bit of programming, but if you can read the Backbone source code, how to do it should be, well, not obvious, but at least understandable.

这对于小型子集合来说是完全可以接受的。这需要一点编程,但是如果你能读懂 Backbone 的源代码,那么怎么做应该是,嗯,不明显,但至少可以理解。

Or you can use Backbone Relational, which does all the work for you.

或者您可以使用Backbone Relational,它为您完成所有工作。

回答by Brian Lewis

Renato was close but "has" and "set" will not be available yet. I believe Reckoner pointed out part of that. Also, you will need to delete the property from the response, else it will override the default value.

Renato 很接近,但“has”和“set”尚不可用。我相信 Reckoner 指出了其中的一部分。此外,您需要从响应中删除该属性,否则它将覆盖默认值。

_.extend(Backbone.Model.prototype, {
    parse: function(resp, xhr) {
        var attr, model, models, collection, options;
        for (var prop in resp) {
            if (this.defaults && this.defaults[prop]) {
                attr = this.defaults[prop];
                if (attr instanceof Backbone.Model) {
                    model = attr.clone();
                    model.set(resp[prop]);
                    resp[prop] = model;
                } else if (attr instanceof Backbone.Collection) {
                    models = attr.map(function (model) { return model.clone(); });
                    options = _.clone(attr.options);
                    collection = new attr.constructor(models, options);
                    collection.add(resp[prop]);
                    resp[prop] = collection;
                }
            }
        }
        return resp;
    }
});

Hope that helps someone.

希望能帮助某人。