我在這裏有這個工作代碼。當你調用Collection.create的時候,在.bone中調用Backspace.js的時候,
我之前在下劃線模板中發生了錯誤,而我注意到的是,模型沒有被保存到數據庫,因爲渲染由於模板不好而無法工作。這應該暗示model.save()在渲染後被調用。或者,collections.create()可以完成所有的保存工作,而model.save()根本不會被調用?
模型數據如何通過以下代碼保存在數據庫中?
$(function(){
Todos = new TodoList.Collections.Todos;
TodoList.Views.TodoView = Backbone.View.extend({
tagName: "li",
events: {},
initialize: function(){},
template: _.template('<li> <%= task %></li>'),
render: function(){
var todo = this.model.toJSON();
//alert("render: " + JSON.stringify(todo));
return this.template(todo);
}
});
TodoView = TodoList.Views.TodoView;
TodoList.Views.AppView = Backbone.View.extend({
el: $("#todo_app"),
events: {
"submit form#new_todo": "createTodo",
"click div.new-todo-btn" : "showFormNew"
},
showFormNew: function(){
$(".new-todo-form").toggle();
},
initialize: function(){
_.bindAll(this, 'addOne', 'addAll','render');
Todos.bind("add", this.addOne);
Todos.bind("reset", this.addAll);
Todos.bind("all", this.render);
Todos.fetch();
},
addOne: function(todo){
var view = new TodoView({model: todo});
this.$("#todo_list").append(view.render());
},
addAll: function(){
Todos.each(this.addOne);
},
newAttributes: function(event){
var new_todo_form = $(event.currentTarget).serializeObject();
return {
'task': new_todo_form["todo[task]"],
'done': new_todo_form["todo[done]"]
};
},
createTodo: function (e){
e.preventDefault();
var params = this.newAttributes(e);
Todos.create(params);
}
});
});
這有幫助。謝謝。我現在要通過源代碼。 –