백본에서 첫 번째 응용 프로그램을 만들고 있는데 여러 수준의 json을 구문 분석하는 데 가장 적합한 모드를 알고 싶습니다. COLLECTION :백본 구문 중첩 된 json
{
"hotels":[
{
"hotel" : [
{
"name" : "Hotel1"
}
]
},
{
"hotel" : [
{
"name" : "Hotel2"
}
]
},
{
"hotel" : [
{
"name" : "Hotel3"
}
]
}
]
}
내가 이런 백본 수집 및 뷰를 사용하고 그것을 인쇄하려면 : 이것은 JSON의 간단한 작은 예입니다
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}));
}
});
내 템플릿은 다음과 같습니다 :
내가 원하는 모든 호텔은 다른 견해를 가질 24,그러나 나는 또한 시도 이름을 인쇄하지 않습니다
<script type="text/template" id="hotel-list-template">
<div>
<h1>TEMPLATE HOTEL funziona
<a class="btn"><%= hotel.name %></a>
</h1>
</div>
</script>
하지만 값 이름을 인쇄 할 수 없습니다, 어떻게 할까? 감사합니다.
관련 항목 : http://stackoverflow.com/questions/8938841/underscore-js-nested-templates –