2013-02-06 47 views
1

我正在尝试填充模型内部的嵌套集合,以及其他集合中的最后一个模型。 代码如下主干嵌套集合

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/beds的服务器发出请求,然后我回到每张床上。

它填充床集合吗? 我做错了什么?

感谢您的时间伴侣。

回答

2

看起来您需要将Room Model传递给Room Collections

+0

谢谢jaja,愚蠢的错误 – Pablo

+0

它发生在每个人身上:) –