ここにこの作業コードがあります。
前にアンダースコア テンプレートにエラーがありましたが、不適切なテンプレートが原因でレンダリングが機能しなかったため、モデルがデータベースに保存されていないことに気付きました。これは、レンダリング後に 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);
}
});
});