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

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

问题描述

我有以下带有嵌套集合的模型

var Mdl = Backbone.Model.extend({初始化:函数(){//收藏this.col1 = new NestedCollection();},...});

我想在一个请求中同时发送模型和集合中的模型的数据,如下所示:

<代码>{att1: val,col1: [{obj1: val}, {...}]}

我不确定将请求中的数据传递给嵌套集合 (col1) 的最佳方式.我做不到……

var Mdl = Backbone.Model.extend({初始化:函数(){//收藏this.col1 = new NestedCollection(this.get('col1');},...});

...因为在初始化的时候,模型的解析函数还没有被调用,这意味着属性col1是空的,我想到的另一个解决方案是监听父模型的变化...

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

但是这个解决方案感觉有点笨拙,可能会破坏任何

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

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

集合上的函数设置.

有没有人知道这样做的官方"方式?

谢谢.

解决方案

官方"的方式是覆盖解析方法:

http://documentcloud.github.com/backbone/#Model-parse

在您的特定情况下,我可能会做的是,在 parse 方法中,从 col1 数据构建嵌套集合,从结果中删除它,然后将结果传递给.Backbone 然后会将其余数据转换为属性.

我没有试过这个,所以我不能 100% 确定它有效:

解析:函数(响应){this.col1 = new NestedCollection(response.col1);删除 response.col1返回响应}

2012 年 11 月 28 日

Harm 指出这可能不再是最好的方法.最初的答案是很久以前写的,最初的问题表明用户希望集合是模型上的一个属性(而不是属性),但 Harm 有一点,将集合作为属性是更容易接受的这些天的做法.

今天,您可以使用Backbone-Relational 之类的东西来处理很多此类事情你,或者,如果你想自己做,并将集合作为模型属性,你可以这样做:

Building = Backbone.Model.extend({解析:函数(响应){console.log("解析调用");response.rooms = 新房间(response.rooms);返回响应;}});Room = Backbone.Model.extend({});房间 = Backbone.Collection.extend({型号: 房间});科学建筑 = 新建筑();science_building.fetch({成功:功能(模型,响应){console.log(响应);}});

模型获取响应如下:

{ id: 1,名称:爱因斯坦厅",房间: [{id:101, name:'Chem Lab'},{id:201, name:'物理实验室'},{id:205, name:'生物实验室'}]}

生成的建筑模型允许:

science_building.get('rooms').get(101).get('name')//==>化学实验室"

一个有效的 jsFiddle 示例:http://jsfiddle.net/edwardmsmith/9bksp/>

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}, {...}]
}

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');
  },
  ...
});

... 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() {})

and

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

function set-up on the collection.

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

Thanks.

解决方案

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

http://documentcloud.github.com/backbone/#Model-parse

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.

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
}

Edit: Nov 28th 2012

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"

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

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

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