Backbone嵌套集合

时间:2013-07-03 15:16:17

标签: javascript backbone.js backbone-collections

我在骨干网中有一个从服务器检索数据的应用程序。这些数据是酒店和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.json hotel_id)并打印房间的组合:foreach级别结合不同的房间。

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")
});

如何创建此房间组合并打印出来? 有好的方法还是有更好的方法?

1 个答案:

答案 0 :(得分: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'});