我正在使用此待办事项列表作为参考:http ://backbonejs.org/examples/todos/todos.js
简短的回答:todoinaddOne: function(todo){指的是 Todos 集合中的单个 Todo 模型。
更长的答案:
让我们分解一下。
Todos.each(this.addOne, this);
这Todos是一个Collection。Todo Models此代码正在遍历集合,集合Todo中的每个模型都 addOne作为todo. 它还将函数this内的上下文(或值)设置addOne为当前this(指的是App)。如果他们不这样做,那么函数this内addOne将引用集合中的当前模型 ( todo)
// Add a single todo item to the list by creating a view for it, and
// appending its element to the `<ul>`.
addOne: function(todo) { // todo refers to a single model in the collection
var view = new TodoView({model: todo}); // create a new TodoView with the todo model
this.$("#todo-list").append(view.render().el); //<- 'this' refers to 'App'. append the newly created view into the DOM, hence displaying it.
},
在addOne函数中,我们创建一个新的TodoView并将其附加到要显示的 DOM 中。