你的问题是你绑定到model:
this.model.bind('add', this.render, this);
但你要添加到收藏:
QuizMe.QuizCollection.create({
display: "this is a display2",
description: "this is a succinct description"
});
视图通常具有关联的集合或模型,但不能同时具有两者。如果你想要你的QuizView
列出已知的测验:
- 你可能应该称之为
QuizListView
或类似的东西。
- 创建一个新的
QuizView
显示单个测验;这个观点会有一个模型。
- 返工你的
QuizListView
与集合一起工作。
你最终应该得到这样的结果:
QuizMe.QuizListView = Backbone.View.extend({
// ...
initialize: function() {
// You don't need to bind event handlers anymore, newer
// Backbones use the right context by themselves.
_.bindAll(this, 'render');
this.collection.bind('add', this.render);
},
addQuizDialog: function(event) {
this.collection.create({
display: "this is a display2",
description: "this is a succinct description"
});
},
render: function() {
console.log("render called")
// And some stuff in here to add QuizView instances to this.$el
return this; // Your render() should always do this.
}
});
QuizMe.App = new QuizMe.QuizView({ collection: QuizMe.QuizCollection });
并注意后面的逗号render
,较旧的 IE 对此感到不安并导致难以追踪的错误。
我会给你一个快速演示但是http://jsfiddle.net/目前已关闭。当它回来时,你可以开始http://jsfiddle.net/ambiguously/RRXnK/为了发挥作用,该小提琴已经设置了所有适当的 Backbone 内容(jQuery、Backbone 和 Underscore)。