填充嵌套的集合与父模型取 [英] populating nested collections with parent model fetch

查看:124
本文介绍了填充嵌套的集合与父模型取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有嵌套集合下面的模型

I've got the following model with nested collection

var Mdl = Backbone.Model.extend({
  initialize: function() {

    // collection
    this.col1 = new NestedCollection();

  },
  ...
});

我要发送的数据为模型,并在一个请求看起来像集合中的模型:

I would like to send the data for both the model and the models in the collection in one request looking something like:

{
  att1: val,
  col1: [{obj1: val}, {...}]
}

我不确定即将到手的请求采集嵌套(COL1)的数据的最佳方式。我不能做...

I'm unsure about the best way to hand the data in the request to the nested collection (col1). I can't do ...

var Mdl = Backbone.Model.extend({
  initialize: function() {
    // collection
    this.col1 = new NestedCollection(this.get('col1');
  },
  ...
});

...因为在初始化的时间被称为该模型没有被调用,这意味着属性COL1是空的解析功能,另一种解决方案我认为是侦听像父模型的变化。 ..

... because at the time of initialize is called the parse function of the model has not been called which means that the attribute col1 is empty, another solution I thought of was to listen for the change in the parent model like...

model.bind("change:tags", function() {
  model.col1.refresh(model.get('col1'));
});

然而,这种解决方案交到感觉有点沉重,可能会潜在地打破任何

however this solution feels a little heavy handed and might potentially break any

this.col1.bind("add", function() {})

this.col1.bind("remove", function() {})

在收藏功能设置。

function set-up on the collection.

有没有人得到了这样的官方途径的任何想法?

Has anyone got any idea of the 'official' way of doing this?

感谢。

推荐答案

官方的方法是重写解析方法:

The "official" way is to override the parse method:

<一个href=\"http://documentcloud.github.com/backbone/#Model-parse\">http://documentcloud.github.com/backbone/#Model-parse

在特定的情况下,我可能会做的是,在分析方法,建立从COL1数据嵌套收集,从结果中删除,然后用手上的结果。那么骨干将会把其余数据为属性。

In your specific case, what I would probably do is, in the parse method, build the nested collection from the col1 data, delete it from the results, then hand the results on. Backbone will then turn the rest of the data into properties.

我还没有试过这一点,所以我不是100%肯定它的工作原理:

I have not tried this, so I'm not 100% sure it works:

parse: function(response) {
  this.col1 = new NestedCollection(response.col1);
  delete response.col1
  return response
}

修改:2012年11月28日

害指出,这可能不是做任何更的最佳方式。原来答案写得相当一段时间以前,原来的问题表明,用户想收集要对模型(不是属性)的属性,但是危害有一个点,具有集合作为属性是一个比较公认的做这些日子的方式。

Harm points out that this might not be the best way to do it any more. The original answer was written quite a while ago, and the original question indicated that the user wanted the collection to be a property on the model (not an attribute), but Harm has a point that having the collection as an attribute is a more accepted way of doing it these days.

今天,你可以使用类似骨干 - 关系处理很多这方面的东西给你,或者,如果你想自己做,并有集合作为一个模型属性,你可以这样做:

Today, you could use something like Backbone-Relational to handle a lot of this stuff for you, or, if you wanted to do it yourself, and have the collection as a model attribute, you could do something like:

Building = Backbone.Model.extend({
    parse: function(response) {
        console.log("Parse Called");
        response.rooms = new Rooms(response.rooms);
        return response;
    }
});
Room = Backbone.Model.extend({});
Rooms = Backbone.Collection.extend({
    model: Room
});

science_building = new Building();

science_building.fetch(
    {success: function(model,resp) {console.log(resp);}}
);

通过模型获取响应,如:

With a model fetch response like:

{ id: 1, 
  name: "Einstein Hall", 
  rooms: [
    {id:101, name:'Chem Lab'},
    {id:201, name:'Physics Lab'},
    {id:205, name:'Bio Lab'}
  ]
}

在建筑模型,让所得的:

Resulting in a Building model that allows:

science_building.get('rooms').get(101).get('name')   // ==> "Chem Lab"

一个工作的jsfiddle例如: http://jsfiddle.net/edwardmsmith/9bksp/

A working jsFiddle example: http://jsfiddle.net/edwardmsmith/9bksp/

这篇关于填充嵌套的集合与父模型取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆