|
@@ -2,6 +2,7 @@
|
|
|
const TodoListView = Backbone.View.extend({
|
|
|
initialize() {
|
|
|
this.collection.on('add', this.addOne, this);
|
|
|
+ this.collection.on('reset', this.addAll, this);
|
|
|
},
|
|
|
|
|
|
// First attempt (bad) at doing a list render.
|
|
@@ -13,6 +14,11 @@ const TodoListView = Backbone.View.extend({
|
|
|
});
|
|
|
},
|
|
|
|
|
|
+ addAll: function (todoItems) {
|
|
|
+ this.collection.forEach(this.addOne.bind(this));
|
|
|
+ return this;
|
|
|
+ },
|
|
|
+
|
|
|
addOne: function (todoItem) {
|
|
|
const todoView = new TodoView({ model: todoItem });
|
|
|
const rendered = todoView.render();
|
|
@@ -21,8 +27,9 @@ const TodoListView = Backbone.View.extend({
|
|
|
|
|
|
render: function () {
|
|
|
// Now pass the proper context to the loop callback.
|
|
|
- this.collection.forEach(this.addOne.bind(this));
|
|
|
- return this;
|
|
|
+ // this.collection.forEach(this.addOne.bind(this));
|
|
|
+ // Or use our new addAll()
|
|
|
+ return this.addAll();
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -36,9 +43,10 @@ function todoViewListExample(list) {
|
|
|
console.log(todoListView.el);
|
|
|
// Apply to the DOM.
|
|
|
$('#app').html(todoListView.el);
|
|
|
+ return todoListView;
|
|
|
}
|
|
|
|
|
|
$(function () {
|
|
|
globalList = todoListFromServer();
|
|
|
- todoViewListExample(globalList);
|
|
|
+ globalListView = todoViewListExample(globalList);
|
|
|
});
|