我是 Backbone 的新手,我正在尝试从教程中获取要提交的表单。
我不断收到以下错误:
未捕获的类型错误:无法调用未定义的方法“添加”
为线this.collection.add( book );
我需要纠正什么?
JavaScript:
App.Views.AddBook = Backbone.View.extend({
events: {
'click #add': 'submit'
},
submit: function(e) {
e.preventDefault();
var formData = {};
$('#addBook div').children('input').each(function(i, el) {
if($(el).val() !== '') {
formData[ el.id ] = $(el).val();
}
});
var book = new App.Models.Book( formData );
this.collection.add(book);
}
});
var booksCollection = new App.Collections.Books([
{
title: 'JS: The Good Parts',
author: 'Douglas Crockford',
releaseDate: '2008'
},
{
title: 'The Little Book on CoffeeScript',
author: 'Alex MacCaw',
releaseDate: '2012'
}
]);
var addBook = new App.Views.AddBook({ el: $('#addBook') });
HTML:
<form id="addBook" action="#">
<div>
<label for="title">Title: </label><input id="title" type="text" />
<label for="author">Author: </label><input id="author" type="text" />
<label for="releaseDate">Release date: </label><input id="releaseDate" type="text" />
<br>
<button class="btn" id="add">Add</button>
</div>
</form>