3

我正在主干中构建我的第一个应用程序,我想知道哪种模式是解析多级 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>

但是我无法打印值名称,该怎么做?谢谢

4

1 回答 1

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属性是您最终需要解决的另一件事,以便您的应用程序与主干正常工作。

于 2013-06-30T17:11:20.293 回答