컬렉션을 보려면 어떻게 바인딩합니까? http://jsfiddle.net/zQYh5/컬렉션을 볼 수 있도록 바인드 할 수 없습니다.
HTML
<div class="row" id="quizs">
<script type="text/template" id="quizTemplate">
<h1><%= title %>< /h1>
</script>
</div>
<button id="next">Next</button>
자바 스크립트
- 내가 디버깅 할 때 나는내 코드 QuizView에 this.collection.get() 또는 this.collection.at()를 사용할 수 없습니다
$(function() {
var Quiz = Backbone.Model.extend({});
var QuizList = Backbone.Collection.extend({
model: Quiz
});
var quizs = {[{id: 1, title: "a"},{id: 2,title: "b"},{id: 3,title: "c"},{id: 4,title: "d"},]}
var QuestionView = Backbone.View.extend({
events: {
'click #next': 'showNextQuestion'
},
collection: quizs,
initialize: function() {
_.bindAll(this, "showNextQuestion", "render");
this.currentIndex = 0;
this.render();
},
showNextQuestion: function() {
this.currentIndex++;
if (this.currentIndex < this.collection.length) {
this.render();
}
},
render: function() {
var template = _.template($("#quizTemplate").html(), this.collection.at(this.currentIndex));
this.$el.html(template);
},
});
var app = new QuestionView({});
})
첫 번째와 두 번째 대답이 옳습니다. 각각 하나씩 사용할 수 있습니다. 그리고 나는 당신에게 한 가지 방법을 제안한다. 내부에'this.collection = your_coll'을 초기화 할 수있다. –