1

我想构建一个酒店和房间的应用程序。
每家酒店都可以有更多房间,我从外部服务器以 XML 格式检索这些数据,我对其进行解析,现在我分为两个数组:酒店和房间,如下所示:
hotel.json

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

房间.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"
      }
    ]

就效率和解析而言,哪种模式对骨干网更好?我想到了第一种情况,但在构建应用程序后我不确定。

4

1 回答 1

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});
    }
});
于 2013-07-02T20:00:23.163 回答