如何将集合绑定到视图?当我调试时,我不能在 QuizView 中使用 this.collection.get() 或 this.collection.at()
我的代码 - 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>
Javascript
$(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({});
})