我一直在看backbone.js 教程,但我仍然对如何将表单数据保存到新对象中以便以后保存到数据库中感到困惑。
所以基本上我有一个动态生成的表格,叫做“真/假”,用户填写它来生成一个真假问题。现在,在提交时,我需要将该表单数据保存到一个对象中。希望这是有道理的。现在它只在 console.log 中显示一个属性,但我需要的是代码中定义的已编辑属性和默认值。非常感谢任何建议/帮助,是的,我有 RTFM
(function() {
window.App = {
Models: {},
Collections: {},
Views: {}
};
App.Models.TestTypes = Backbone.Model.extend({
});
App.Collections.TestTypes = Backbone.Collection.extend({
model: App.Models.TestTypes
});
App.Views.TestTypes = Backbone.View.extend({
el: $('div.add-btn'),
events: {
'click button.btn-add-tf' : 'addTrueFalse',
'click button.btn-add-mult' : 'addMult',
'click button.btn-add-short' : 'addShort',
'click button.btn-add-essay' : 'addEssay'
},
// event handling functions
addTrueFalse: function(e) {
e.preventDefault();
this.addTrueFalseForm();
this.addTrueFalsePreview();
},
addTrueFalseForm: function(e) {
$('.new-questions').append('<div class="add-question"><form action="">' +
'<div class="remove-btn">x</div>' +
'<span style="font-weight: normal;;"><em>True / False</em></span></p>' +
'<label for="Question_description">Question</label><br />' +
'<input type="text" name="question[description]" class="tf-desc" /><br />' +
'True <input type="radio" name="question_answer[answer]" id="question_answer[correct]" value="1" />' +
' False <input type="radio" name="question_answer[answer]" id="question_answer[correct]" value="0" /><br />' +
'<input type="submit" value="Save" /></form>' +
'</div>');
},
addTrueFalsePreview: function(e) {
$('#test-preview').append("<p>Preview</p>");
}
});
App.Views.NewTestType = Backbone.View.extend({
el: '.new-questions',
events: {
'submit': 'addTest'
},
addTest: function(e) {
e.preventDefault();
var addTestData = $(e.currentTarget).find('.tf-desc').val();
var task = new App.Models.TestTypes({ description: addTestData });
this.collection.add(task);
console.log(task);
}
});
var testTypesCollection = new App.Collections.TestTypes([
{
defaults: {
questions: 0,
type: "",
description: "",
require_review: 0,
max_points: 0,
is_bonus: 0,
order: 0,
case_sensitive: 0,
answer: "",
points: 0,
is_correct: 0
}
}]);
var addTest = new App.Views.NewTestType({ collection: testTypesCollection });
var question = new App.Views.TestTypes({ collection: testTypesCollection });
})();