3

サーバーからデータを取得し、ホテルに関するデータを返すバックボーンにアプリがあります。各ホテルには多くの部屋 (シングル、ダブル、トリプルなど) を含めることができます。このために、2 つの json hotel.json を作成しました。

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

部屋.json

[
    {
      "room" : [
        {
          "id" : "r1",
          "hotel_id" : "1",
          "name" : "Singola",
        }
      ]
    },
    {
      "room" : [
        {
          "id" : "r1_1",
          "hotel_id" : "1",
          "name" : "Doppia",
        }
      ]
    },
    {
      "room" : [
        {
          "id" : "r2",
          "hotel_id" : "2",
          "name" : "Singola",
        }
      ]
    },
    {
      "room" : [
        {
          "id" : "r2_1",
          "hotel_id" : "2",
          "name" : "Tripla",
        }
      ]
    },
  ]

rooms.json には、部屋をホテルにリンクするための hotel_id というフィールドがあります。さて、これはホテルを表示するバックボーンの私のアプリです:

var Hotel = Backbone.Model.extend({
            defaults: function() {
                return {
                    name: 'HOTEL NAME',
                    star: '5'
                }
            }
        });

        var HotelsCollection = Backbone.Collection.extend({
            model: Hotel,
            url: "includes/test-data.json",

            initialize: function(){
                console.log("Collection Hotel initialize");
            },

            parse: function(response){
                return(response);
            }     
        });

        var HotelView = Backbone.View.extend({ 
            template: _.template($("#hotel-list-template").html()),
            initialize: function(){ 
                this.collection = new HotelsCollection();
                this.collection.bind('sync', this.render, this);
                this.collection.fetch();
            },
            render: function(){
                console.log('Data hotel is fetched');
                var element = this.$el;
                element.html('');

                $(this.el).html(this.template({hotel: this.collection.models}));
            } 
        });

これは、uderscore の私のテンプレートです。

<script type="text/template" id="hotel-list-template">
    <% _.each(hotel, function(name) { %> 
    <div id="hotel-container-<%= name.attributes.id %>">
        <p>Nome Hotel: <%= name.attributes.name %></p>
    </div>
    <% }); %>
    </script>

では、ホテル内の各部屋を同じ div に挿入するにはどうすればよいでしょうか。データをフェッチするコレクションであるクラシック モデルを作成することを考えましたが、レンダリングのビューで、hotel_id=1 の部屋のみを id=hotel-container-1 の div に挿入するようにバックボーンに指示するにはどうすればよいですか?

- - - - - - - - - アップデート - - - - - - - - - - -

var Hotel = Backbone.Model.extend({
            defaults: function() {
                return {
                    name: 'HOTEL NAME',
                    star: '5'
                }
            }
        });

        var HotelsCollection = Backbone.Collection.extend({
            model: Hotel,
            url: "includes/test-data.json",

            initialize: function(){
                console.log("Collection Hotel initialize");
            },

            parse: function(response){
                return(response);
            },
            getRooms : function() {
                return _.map(allRooms.where({"hotel_id" : this.get("id")}), function(room) {
                    return room.toJSON();
                });
            },
            addRoom : function(room) {
                room.set("hotel_id", this.get("id"));
                allRooms.add(room);
            },
            // this will return Backbone´s native toJSON Object
            // with an extra field "rooms" which you can access
            toJSON : function() {
                var parent = Backbone.Collection.prototype.toJSON.call(this);
                return _.extend({}, parent, {rooms : this.getRooms().toJSON()});
            }
        });

        var HotelView = Backbone.View.extend({ 
            template: _.template($("#hotel-list-template").html()),
            initialize: function(){ 
                this.collection = new HotelsCollection();
                this.collection.bind('sync', this.render, this);
                this.collection.fetch();
            },
            render: function(){
                console.log('Data hotel is fetched');
                var viewModel = this.collection.toJSON();
                this.$el.html(this.template({models:viewModel}));

            } 
        }); 


var Room = Backbone.Model.extend();
var Rooms = Backbone.Collection.extend({model:Room});

var allRooms = new Rooms();

        var hotelView = new HotelView({ 
            el: $("#hotel") 
        });
4

3 に答える 3

2

まず、Room Model と Room Collection がありません。次に、すべての Room のどこかにインスタンスを作成する必要があります。

var Room = Backbone.Model.extend();
var Rooms = Backbone.Collection.extend({
    model:Room,
    url : "rooms.json"
});

var allRooms = new Rooms();

ここまでは至ってシンプル。

いいえ、ホテルモデル用にいくつかのメソッドを作成し、部屋に対していくつかのアクションを有効にします

var Hotel = Backbone.Model.extend({
    getRooms : function() {
        return _.map(allRooms.where({"hotel_id" : this.get("id")}), function(room) {
            return room.toJSON();
        });
    },
    addRoom : function(room) {
        room.set("hotel_id", this.get("id"));
        allRooms.add(room);
    },
    // this will return Backbone´s native toJSON Object
    // with an extra field "rooms" which you can access
    toJSON : function() {
        var parent = Backbone.Model.prototype.toJSON.call(this);
        return _.extend({}, parent, {rooms : this.getRooms().toJSON()});
    }
});
var HotelsCollection = Backbone.Collection.extend({
    model: Hotel,
    url: "includes/test-data.json",
    initialize: function(){
        console.log("Collection Hotel initialize");
    },
    parse: function(response){
        return(response);
    }

});

これは単なるサンプルであり、さらに多くのことができます。

今あなたのレンダリング機能で....

underscores render メソッドには何でも入れることができます。

例えば ​​:

render : function() {
    //this will give you a nice formatted array (see toJSON override above)
    var viewModel = this.collection.toJSON();
    this.$el.html(this.template({models:viewModel});
}

ビューでは、「部屋」配列にアクセスできます...

<script type="text/template" id="hotel-list-template">
    <% _.each(models, function(hotel) { %> 
    <div id="hotel-container-<%= hotel.id %>">
        <p>Nome Hotel: <%= hotel.name %></p>
        <ul>
        <% _.each(hote.rooms, function(room) { %>
            <li><%=room.name%></li>
        <% }); %>
        </ul>
    </div>
    <% }); %>
</script>
于 2013-06-30T22:47:26.417 に答える