骨干解析嵌套json

时间:2013-06-30 16:57:31

标签: jquery json backbone.js underscore.js

我在骨干网中建立我的第一个应用程序,我想知道哪个是解析多级json的最佳模式。这是json的一个简单的小例子:

{
  "hotels":[
    {
      "hotel" : [
        {
          "name" : "Hotel1"
        }
      ]
    },
    {
      "hotel" : [
        {
          "name" : "Hotel2"
        }
      ]
    },
    {
      "hotel" : [
        {
          "name" : "Hotel3"
        }
      ]
    }
  ]
}

要打印它,我在骨干中使用集合和视图,如下所示: COLLECTION:

var HotelsCollection = Backbone.Collection.extend({
            model: Hotel,
            url: "includes/test-data.json",
            parse : function(response){
                return response.hotels;  
           }    
        });

这是两个视图,称为视图,因为我希望每个酒店都有不同的观点:

var AppView = Backbone.View.extend({ 
            initialize: function(){ 
                this.collection = new HotelsCollection();
                this.collection.bind('sync', this.render, this);
                this.collection.fetch();
            },
            render: function(){
                console.log('Data is fetched');
                var element = this.$el;
                element.html('');
                this.collection.each(function(model){
                    console.log('new hotel');

                    var hotelView = new HotelView({model:model});

                    element.append(hotelView.el);
                });
            } 
        }); 

        var HotelView = Backbone.View.extend({

            template: _.template($("#hotel-list-template").html()),

            initialize: function(){
                console.log('HotelView initialized');
                this.render();
            },
            render: function(){
                console.log('HotelView render');

                $(this.el).html(this.template({model: this.options.model}));
            }
        });

我的模板是:

<script type="text/template" id="hotel-list-template">
    <div>
        <h1>TEMPLATE HOTEL funziona?
        <% _.each(hotel, function(acs) { %> 
            <a class="btn"><%= name %></a>
        <% }); %>
        </h1>
    </div>
    </script>

但是没有打印我也尝试过的名字:

<script type="text/template" id="hotel-list-template">
    <div>
        <h1>TEMPLATE HOTEL funziona
            <a class="btn"><%= hotel.name %></a>
        </h1>
    </div>
    </script>

但我无法打印超值名称,怎么做? 感谢

1 个答案:

答案 0 :(得分:5)

首先,JSON结构确实非常奇怪。修复您的服务器或要求您的服务器团队寻求治疗。但是假设你无法解密服务器的JSON,这里是如何将它变成骨干兼容的数组:

var HotelsCollection = Backbone.Collection.extend({
  model: Hotel,
  url: "includes/test-data.json",
  parse: function(response){
    //remove prefix/wrapper object and collect "hotel" 1-element arrays
    var sillyArrays = _.pluck(response.hotels, 'hotel');
    //Extract the hotel objects from their silly 1-element arrays
    //Synthesize a fake id attribute since backbone expects that
    var hotels = _.map(sillyArrays, function (hotelArray, index) {
     return {name: hotelArray[0].name, id: index + 1};
    });
    return hotels;
  }    
});

parse函数将返回此数据,主干将理解。

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

另请注意,缺少id属性是您最终需要解决的另一件事,以便您的应用程序能够与主干一起正常工作。