Skip to content

Commit

Permalink
Remove coupling from model to view in example app by listening to mod…
Browse files Browse the repository at this point in the history
…el's 'destroy' event to remove the view.
  • Loading branch information
jcoglan committed Jul 22, 2011
1 parent ca8a049 commit f77033d
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions examples/todos/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ $(function(){
// Toggle the `done` state of this todo item.
toggle: function() {
this.save({done: !this.get("done")});
},

// Remove this Todo from *localStorage* and delete its view.
clear: function() {
this.destroy();
this.view.remove();
}

});
Expand Down Expand Up @@ -102,9 +96,9 @@ $(function(){
// a one-to-one correspondence between a **Todo** and a **TodoView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
_.bindAll(this, 'render', 'close');
_.bindAll(this, 'render', 'close', 'remove');
this.model.bind('change', this.render);
this.model.view = this;
this.model.bind('destroy', this.remove);
},

// Re-render the contents of the todo item.
Expand Down Expand Up @@ -153,7 +147,7 @@ $(function(){

// Remove the item, destroy the model.
clear: function() {
this.model.clear();
this.model.destroy();
}

});
Expand Down Expand Up @@ -234,7 +228,7 @@ $(function(){

// Clear all done todo items, destroying their models.
clearCompleted: function() {
_.each(Todos.done(), function(todo){ todo.clear(); });
_.each(Todos.done(), function(todo){ todo.destroy(); });
return false;
},

Expand Down

0 comments on commit f77033d

Please sign in to comment.