Commit bb277681 authored by Gilad Peleg's avatar Gilad Peleg

Ampersand.js - extract methods to collection

- Extract method to count total completed todos
- Listen only to relevant events in handleTodosUpdate
- Use .listenTo instead of .on
parent 18d4f472
...@@ -14,13 +14,12 @@ module.exports = State.extend({ ...@@ -14,13 +14,12 @@ module.exports = State.extend({
initialize: function () { initialize: function () {
// Listen to changes to the todos collection that will // Listen to changes to the todos collection that will
// affect lengths we want to calculate. // affect lengths we want to calculate.
this.listenTo(this.todos, 'change:completed change:title add remove', this.handleTodosUpdate); this.listenTo(this.todos, 'change:completed add remove', this.handleTodosUpdate);
// We also want to calculate these values once on init // We also want to calculate these values once on init
this.handleTodosUpdate(); this.handleTodosUpdate();
// Listen for changes to `mode` so we can update // Listen for changes to `mode` so we can update
// the collection mode. // the collection mode.
this.on('change:mode', this.handleModeChange, this); this.listenTo(this, 'change:mode', this.handleModeChange);
}, },
collections: { collections: {
todos: Todos todos: Todos
...@@ -71,18 +70,15 @@ module.exports = State.extend({ ...@@ -71,18 +70,15 @@ module.exports = State.extend({
// so they're easy to listen to and bind to DOM // so they're easy to listen to and bind to DOM
// where needed. // where needed.
handleTodosUpdate: function () { handleTodosUpdate: function () {
var completed = 0; var total = this.todos.length;
var todos = this.todos; var completed = this.todos.countCompleted();
todos.each(function (todo) { // We use `set` here in order to update multiple attributes at once
if (todo.completed) { // It's possible to set directely using `this.completedCount = completed` ...
completed++;
}
});
this.set({ this.set({
completedCount: completed, completedCount: completed,
activeCount: todos.length - completed, activeCount: total - completed,
totalCount: todos.length, totalCount: total,
allCompleted: todos.length === completed allCompleted: total === completed
}); });
}, },
handleModeChange: function () { handleModeChange: function () {
......
...@@ -30,6 +30,11 @@ module.exports = Collection.extend({ ...@@ -30,6 +30,11 @@ module.exports = Collection.extend({
// and persist on change // and persist on change
this.on('all', this.writeToLocalStorage, this); this.on('all', this.writeToLocalStorage, this);
}, },
countCompleted: function() {
return this.reduce(function(total, todo){
return todo.completed ? ++total : total;
}, 0);
},
// Helper for removing all completed items // Helper for removing all completed items
clearCompleted: function () { clearCompleted: function () {
var toRemove = this.filter(function (todo) { var toRemove = this.filter(function (todo) {
......
...@@ -43,13 +43,12 @@ module.exports = State.extend({ ...@@ -43,13 +43,12 @@ module.exports = State.extend({
initialize: function () { initialize: function () {
// Listen to changes to the todos collection that will // Listen to changes to the todos collection that will
// affect lengths we want to calculate. // affect lengths we want to calculate.
this.listenTo(this.todos, 'change:completed change:title add remove', this.handleTodosUpdate); this.listenTo(this.todos, 'change:completed add remove', this.handleTodosUpdate);
// We also want to calculate these values once on init // We also want to calculate these values once on init
this.handleTodosUpdate(); this.handleTodosUpdate();
// Listen for changes to `mode` so we can update // Listen for changes to `mode` so we can update
// the collection mode. // the collection mode.
this.on('change:mode', this.handleModeChange, this); this.listenTo(this, 'change:mode', this.handleModeChange);
}, },
collections: { collections: {
todos: Todos todos: Todos
...@@ -100,18 +99,15 @@ module.exports = State.extend({ ...@@ -100,18 +99,15 @@ module.exports = State.extend({
// so they're easy to listen to and bind to DOM // so they're easy to listen to and bind to DOM
// where needed. // where needed.
handleTodosUpdate: function () { handleTodosUpdate: function () {
var completed = 0; var total = this.todos.length;
var todos = this.todos; var completed = this.todos.countCompleted();
todos.each(function (todo) { // We use `set` here in order to update multiple attributes at once
if (todo.completed) { // It's possible to set directely using `this.completedCount = completed` ...
completed++;
}
});
this.set({ this.set({
completedCount: completed, completedCount: completed,
activeCount: todos.length - completed, activeCount: total - completed,
totalCount: todos.length, totalCount: total,
allCompleted: todos.length === completed allCompleted: total === completed
}); });
}, },
handleModeChange: function () { handleModeChange: function () {
...@@ -188,6 +184,11 @@ module.exports = Collection.extend({ ...@@ -188,6 +184,11 @@ module.exports = Collection.extend({
// and persist on change // and persist on change
this.on('all', this.writeToLocalStorage, this); this.on('all', this.writeToLocalStorage, this);
}, },
countCompleted: function() {
return this.reduce(function(total, todo){
return todo.completed ? ++total : total;
}, 0);
},
// Helper for removing all completed items // Helper for removing all completed items
clearCompleted: function () { clearCompleted: function () {
var toRemove = this.filter(function (todo) { var toRemove = this.filter(function (todo) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment