2013-07-03 61 views
11

我有一个骨干应用程序检索服务器的应用程序。这些数据是酒店和foreach酒店,我有更多的房间。我已经分成宾馆成JSON和另一房间内的JSON像这样:骨干嵌套集合

hotel.json

[ 
    { 
    "id": "1", 
    "name": "Hotel1" 
    }, 
    { 
    "id": "2", 
    "name": "Hotel2" 
    }, 
    { 
    "id": "3", 
    "name": "Hotel3" 
    } 
] 

rooms.json

[ 
    { 
    "id" : "r1", 
    "hotel_id" : "1", 
    "name" : "Singola", 
    "level" : "1" 
    }, 
    { 
    "id" : "r1_1", 
    "hotel_id" : "1", 
    "name" : "Doppia", 
    "level" : "2" 
    }, 
    { 
    "id" : "r1_3", 
    "hotel_id" : "1", 
    "name" : "Doppia Uso singol", 
    "level" : "1" 
    }, 
    { 
    "id" : "r2", 
    "hotel_id" : "2", 
    "name" : "Singola", 
    "level" : "1" 
    }, 
    { 
    "id" : "r2_1", 
    "hotel_id" : "2", 
    "name" : "Tripla", 
    "level" : "1" 
    } 
] 

我想利用每家酒店和其客房结合(外部键入rooms.jsonhotel_id)并打印房间的组合:每个级别组合不同的房间。 1级的

一个房间,第2级的一个房间和水平中的一个房间3.

水平的最大是3,但我只能有一个电平或只有两个级别。 如果我有3个级别,我不想1级和2级的组合没有3级

像这样的事情

Room "Single", "level" : "1" , "hotel_id" : "1" 
Room "Double", "level" : "2" , , "hotel_id" : "1" 
Room "Triple", "level" : "3" , , "hotel_id" : "1" 

Room "Double for single", "level" : "1" , "hotel_id" : "1" 
Room "Double", "level" : "2" , , "hotel_id" : "1" 
Room "Triple", "level" : "3" , , "hotel_id" : "1" 

的这个房间,我认为是构造函数将renderRooms放入我的应用程序中。

这是我的应用程序:

var Room = Backbone.Model.extend(); 
var Rooms = Backbone.Collection.extend({ 
    model: Room, 
    url: "includes/rooms.json" 
}); 
var Hotel = Backbone.Model.extend({ 
    defaults: function() { 
     return { 
      "id": "1", 
      "name": "Hotel1", 
      "rooms": [] 
     } 
    } 
}); 
var HotelCollection = Backbone.Collection.extend({ 
    model: Hotel, 
    url: "includes/test-data.json", 
    initialize: function() { 
     console.log("Collection Hotel initialize"); 
    } 
}); 
var HotelView = Backbone.View.extend({ 
    template: _.template($("#hotel-list-template").html()), 
    initialize: function() { 
     this.collection = new HotelCollection(); 
     this.collection.bind('sync', this.render, this); 
     this.collection.fetch(); 
    }, 
    render: function() { 
     console.log('Data hotel is fetched'); 
     this.bindRoomToHotel(); 
     var element = this.$el; 
     element.html(''); 
    }, 
    bindRoomToHotel: function() { 
     allRooms = new Rooms(); 
     allRooms.on("sync", this.renderRooms, this) 
     allRooms.fetch(); 
    }, 
    renderRooms: function() { 


     $(this.el).html(this.template({ hotels: this.collection.models })); 
    } 
}); 

var hotelView = new HotelView({ 
    el: $("#hotel") 
}); 

我怎么能创造这个房间组合并打印出来?
有没有好的方法或有更好的方法?

+0

我一直在关注有关此问题的其他问题,但好像你有几个问题需要解决。你真正需要的是@Bergi在这个问题中提出的笛卡儿函数http://stackoverflow.com/questions/17417589/recursive-function-jquery-with-backbone。我会继续走下去。 –

+0

我已经试过但似乎不起作用,第一组数组是空的我不知道为什么.. @moderndegree –

+0

我提交了一个更新到@ Bergi的这个问题的答案(http://stackoverflow.com/问题/ 17417589 /递归函数jQuery的有骨架)。同时,你可以在这里看到一个工作示例:http://plnkr.co/edit/NHE9V5?p=preview。 –

回答

20

这里是你如何结构类别:

HotelModel = Backbone.Model.extend({ 
    initialize: function() { 
     // because initialize is called after parse 
     _.defaults(this, { 
      rooms: new RoomCollection 
     }); 
    }, 
    parse: function(response) { 
     if (_.has(response, "rooms")) { 
      this.rooms = new RoomCollection(response.rooms, { 
       parse: true 
      }); 
      delete response.rooms; 
     } 
     return response; 
    }, 
    toJSON: function() { 
     var json = _.clone(this.attributes); 
     json.rooms = this.rooms.toJSON(); 
     return json; 
    } 
}); 

RoomModel = Backbone.Model.extend({ 
}); 

HotelCollection = Backbone.Collection.extend({ 
    model: HotelModel 
}); 

RoomCollection = Backbone.Collection.extend({ 
    model: RoomModel 
}); 

然后,你可以做这样的事情:

var hotels = new HotelCollection(); 
hotels.reset([{ 
    id: 1, 
    name: 'Hotel California', 
    rooms: [{ 
     id: 1, 
     name: 'Super Deluxe' 
    }] 
}], { 
    parse: true // tell the collection to parse the data 
}); 

// retrieve a room from a hotel 
hotels.get(1).rooms.get(1); 

// add a room to the hotel 
hotels.get(1).rooms.add({id:2, name:'Another Room'}); 
+0

嗨,我知道这是一个旧的答案,但不应该房间被添加到'this.attributes'不只是'this',因为你不能'model.get('rooms')' – Quince

+1

这是因为'房间'是一个集合,需要直接解决,而不是通过get()或set()。例如,如果您要调用'set('rooms',[])',则Collection将不同步。我想你可以重做它来听'change:rooms'并以这种方式处理它,但它看起来像很多工作。 –

+0

如果'rooms'是你的模型属性哈希中的一个集合,你可以使用'get('rooms')。reset([])'来避免不同步的集合,并且避免收听。 –