主干如何正确地构建JSON [英] Backbone how to construct json correctly

查看:152
本文介绍了主干如何正确地构建JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建造的酒店和房间的一个应用程序。结果
每家酒店可以有更多的房间,我检索XML中的外部服务器这个数据,我分析它,现在我已经分成两个数组:酒店和房间是这样的:结果
hotel.json

I want to construct an app of hotel and rooms.
Every hotel can have more rooms, I retrieve this data from external server in XML, I parse it and now I have divided into two arrays: hotel and rooms like this:
hotel.json

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

rooms.json

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这样的:

Into my backbone app I have to make some controller and some parse to retrieve rooms for its hotel.
I want to know if is better for backbone to construct a Json like that:

[
      {
        "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"
      }
    ]

这是骨干效率和解析方面更好的模式?
我鑫卡特第一种情况,但构造之后的应用程序,我不知道。

Which is the better mode for backbone in terms of efficiency and parsing? I thinked the first case but after construct the app I'm not sure.

推荐答案

我会建议保持数据结构扁平,作为骨干并没有真正支持嵌套集合没有一些额外的努力。保持数据模型平也将使您更方便地映射到REST端点(即'/酒店/ 1 /房,房/ 1,等等)。

I would recommend keeping the data structures flat, as Backbone doesn't really support nested collections without some extra effort. Keeping the data model flat will also make it easier for you to map to REST endpoints (ie. '/hotels/1/rooms', 'rooms/1', etc.).

只是为了演示的复杂性,这里是怎么一会要集合到一个模型相关联的例子:

Just to demonstrate the complexities, here is an example of how one would have to associate a collection to a model:

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

通过扁平的数据结构,你可以做这样的事情:

With a flat data structure, you could do something like this:

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

这篇关于主干如何正确地构建JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆