我有一个应用程序在主干,我从服务器获取数据并返回有关酒店的数据。 每个酒店可以包含很多房间(单人,双人,三人..)。 为此,我必须创建两个JSON hotel.json:骨干插入一个应用程序内的应用程序
[
{
"id": "1",
"name": "Hotel1"
},
{
"id": "2",
"name": "Hotel2"
},
{
"id": "3",
"name": "Hotel3"
}
]
rooms.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中?
------------------ UPDATE ---------------------
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")
});
谢谢今天晚上我会试试这个解决方案! –
我不明白你的firts部分,但如果我使用你的代码返回给我这个错误:Object [object Array]没有方法'add'。你能帮我么? –
@AlessandroMinoccheri,我编辑了我的答案,我想,它现在不显示错误 –