Backbone嵌套集合

时间:2013-02-06 22:59:41

标签: javascript web-applications backbone.js

我正在尝试填充模型中的嵌套集合,并在另一个集合中填充最后一个模型。 代码如下

App.Models.Bed = Backbone.Model.extend();

App.Collections.Beds = Backbone.Collection.extend({
model: App.Models.Bed,
initialize: function(models, options){
    this.room = options.room;
}
});

App.Models.Room = Backbone.Model.extend();

App.Collections.Room = Backbone.Collection.extend({
url: 'rooms/',
initialize: function(){
    this.on('reset', this.getBeds, this);
},

getBeds: function(){
    this.each(function( room ){
        room.beds = new App.Collections.Beds([], { room: room});
        room.beds.url = 'rooms/' + room.id + '/beds';
        room.beds.fetch();
        console.log(room.beds.toJSON());
    });
}
});

现在,如果我执行:

var rooms = new App.Collections.Room();
rooms.fetch();
rooms.toJSON();

它让我回到了房间,但每张床都没有床位。 有线的事情是,它向/ rooms / 1 /床的服务器发出请求,然后我回到每张床。

是否填充了床品系列? 我做错了吗?

感谢您的时间伙伴。

1 个答案:

答案 0 :(得分:2)

您似乎需要将Room Model传递给Room Collections

相关问题