我終於得到了我的Rails Todo List應用程序來顯示使用Backbone的渲染器的Todos。雖然我有一個小問題。在addOne函數中,我使用了view.render()而不是view.render()。el,本教程中教授的內容。爲什麼視圖不能用.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);
}
});
});