我终于让我的 Rails Todo List 应用程序使用 Backbone 的渲染器显示 Todos。不过我有一个小问题。在 addOne 函数中,我使用了 view.render() 而不是 view.render().el,这是教程中所教的。
视图不使用 view.render.el() 渲染。有什么解释吗?
$(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();
return this.template(todo);
}
});
TodoView = TodoList.Views.TodoView;
TodoList.Views.AppView = Backbone.View.extend({
el: $("#todo_app"),
events: {
"submit form#new_todo": "createTodo"
},
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 {
dog: {
name: new_todo_form["todo[task]"],
age: new_todo_form["todo[done]"]
}
};
},
createTodo: function (e){
e.preventDefault();
var params = this.newAttributes(e);
Dogs.create(params);
}
});
});