Backbone如何正确构造json

时间:2013-07-02 19:53:52

标签: json backbone.js

我想构建一个酒店和房间的应用程序 每个酒店都可以有更多的房间,我用XML从外部服务器检索这些数据,我解析它,现在我分成了两个阵列:酒店和这样的房间:
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"
  }
]

在我的骨干应用程序中,我必须制作一些控制器和一些解析来检索其酒店的房间 我想知道骨干是否更适合构建像这样的Json:

[
      {
        "id": "1", 
        "name": "Hotel1",
        "rooms": [
                 {
                   "id" : "r1",
                   "hotel_id" : "1",
                   "name" : "Singola",
                   "level" : "1"
                 },
                 {
                   "id" : "r1_1",
                   "hotel_id" : "1",
                   "name" : "Doppia",
                   "level" : "2"
                 }
                 ]

      }, 
      {
        "id": "2", 
        "name": "Hotel2",
        "rooms": [
                 {
                   "id" : "r2",
                   "hotel_id" : "2",
                   "name" : "Singola",
                   "level" : "1"
                 },
                 {
                   "id" : "r2_1",
                   "hotel_id" : "1",
                   "name" : "Doppia",
                   "level" : "2"
                 }
                 ]
      }, 
      {
        "id": "3", 
        "name": "Hotel3"
      }
    ]

在效率和解析方面,哪种骨干模式更好? 我认为第一种情况,但在构建应用程序后,我不确定。

1 个答案:

答案 0 :(得分:1)

我建议保持数据结构不变,因为Backbone并不真正支持嵌套集合而不需要额外的努力。保持数据模型平坦也将使您更容易映射到REST端点(即'/ hotels / 1 / rooms','rooms / 1'等)。

为了证明这些复杂性,下面是一个如何将集合与模型相关联的示例:

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

使用平面数据结构,您可以执行以下操作:

HotelModel = Backbone.Model.extend({
    idAttribute:'hotel_id',
    urlRoot:'/hotels'
});
RoomModel = Backbone.Model.extend({
    idAttribute:'room_id',
    urlRoot:'/rooms'
});

HotelCollection = Backbone.Collection.extend({
    url: '/hotels',
    model:HotelModel
});
RoomCollection = Backbone.Collection.extend({
    url: '/rooms',
    model:RoomModel,
    getByHotelId: function(hotelId){
        return this.findWhere({hotel_id:hotelId});
    }
});