我尝试开始使用 Backbone。我有以下JS:
var Question = Backbone.Model.extend({
defaults: function() {
return {
title: "Enter your question",
type: "smallTextField",
editMode: true
};
},
initialize: function() {
if (!this.get("title")) {
this.set({"title": this.defaults.title});
}
},
toggleEditMode: function() {
!this.get("editMode");
},
clear: function() {
this.destroy();
}
});
收藏:
var Questions = Backbone.Collection.extend({
initialize: function(models, options) {
this.bind("add", options.view.addQuestionView);
}
});
应用视图:
var AppView = Backbone.View.extend({
el: $("body"),
initialize: function () {
this.questions = new Questions( null, { view: this });
},
events: {
"click #addQuestion": "addQuestionModel"
},
addQuestionModel: function() {
var model = new Question();
this.questions.add(model);
},
addQuestionView: function(model) {
$("#questionBox").append("<div>" + model.get('title') + "</div>");
},
});
var appview = new AppView;
和以下 HTML:
<div id="questionBox"></div>
<a href="#" id="addQuestion">Add question</a>
在我的 Jasmine 规范中,我触发了 $("#addQuestion").click();
但是 - 当我运行测试时没有任何反应。例如,似乎没有函数绑定到#addQuestion 的点击事件。
我猜有些事情做错了,我根本无法弄清楚它是什么。
任何想法?