Commit b36d9633 authored by addyosmani's avatar addyosmani

Add changes for #259, started in #260

parent c6e5015a
...@@ -15,8 +15,9 @@ var app = app || {}; ...@@ -15,8 +15,9 @@ var app = app || {};
// Set the current filter to be used // Set the current filter to be used
window.app.TodoFilter = param.trim() || ''; window.app.TodoFilter = param.trim() || '';
// Trigger a collection reset/addAll // Trigger a collection filter event, causing hiding/unhiding
window.app.Todos.trigger('reset'); // of Todo view items
window.app.Todos.trigger('filter');
} }
}); });
......
...@@ -29,14 +29,15 @@ $(function( $ ) { ...@@ -29,14 +29,15 @@ $(function( $ ) {
initialize: function() { initialize: function() {
this.input = this.$('#new-todo'); this.input = this.$('#new-todo');
this.allCheckbox = this.$('#toggle-all')[0]; this.allCheckbox = this.$('#toggle-all')[0];
this.$footer = this.$('#footer');
this.$main = this.$('#main');
window.app.Todos.on( 'add', this.addAll, this ); window.app.Todos.on( 'add', this.addAll, this );
window.app.Todos.on( 'reset', this.addAll, this ); window.app.Todos.on( 'reset', this.addAll, this );
window.app.Todos.on( 'change:completed', this.addAll, this ); window.app.Todos.on('change:completed', this.filterOne, this);
window.app.Todos.on( 'all', this.render, this ); window.app.Todos.on("filter", this.filterAll, this);
this.$footer = this.$('#footer'); window.app.Todos.on( 'all', this.render, this );
this.$main = this.$('#main');
app.Todos.fetch(); app.Todos.fetch();
}, },
...@@ -78,18 +79,15 @@ $(function( $ ) { ...@@ -78,18 +79,15 @@ $(function( $ ) {
// Add all items in the **Todos** collection at once. // Add all items in the **Todos** collection at once.
addAll: function() { addAll: function() {
this.$('#todo-list').html(''); this.$('#todo-list').html('');
app.Todos.each(this.addOne, this);
},
switch( app.TodoFilter ) { filterOne : function (todo) {
case 'active': todo.trigger("visible");
_.each( app.Todos.remaining(), this.addOne ); },
break;
case 'completed': filterAll : function () {
_.each( app.Todos.completed(), this.addOne ); app.Todos.each(this.filterOne, this);
break;
default:
app.Todos.each( this.addOne, this );
break;
}
}, },
// Generate the attributes for a new Todo item. // Generate the attributes for a new Todo item.
......
...@@ -30,6 +30,7 @@ $(function() { ...@@ -30,6 +30,7 @@ $(function() {
initialize: function() { initialize: function() {
this.model.on( 'change', this.render, this ); this.model.on( 'change', this.render, this );
this.model.on( 'destroy', this.remove, this ); this.model.on( 'destroy', this.remove, this );
this.model.on( 'visible', this.toggleVisible, this );
}, },
// Re-render the titles of the todo item. // Re-render the titles of the todo item.
...@@ -37,10 +38,23 @@ $(function() { ...@@ -37,10 +38,23 @@ $(function() {
this.$el.html( this.template( this.model.toJSON() ) ); this.$el.html( this.template( this.model.toJSON() ) );
this.$el.toggleClass( 'completed', this.model.get('completed') ); this.$el.toggleClass( 'completed', this.model.get('completed') );
this.toggleVisible();
this.input = this.$('.edit'); this.input = this.$('.edit');
return this; return this;
}, },
toggleVisible : function () {
this.$el.toggleClass( 'hidden', this.isHidden());
},
isHidden : function () {
var isCompleted = this.model.get('completed');
return ( // hidden cases only
(!isCompleted && app.TodoFilter === 'completed')
|| (isCompleted && app.TodoFilter === 'active')
);
},
// Toggle the `"completed"` state of the model. // Toggle the `"completed"` state of the model.
togglecompleted: function() { togglecompleted: function() {
this.model.toggle(); this.model.toggle();
......
...@@ -404,3 +404,7 @@ label[for='toggle-all'] { ...@@ -404,3 +404,7 @@ label[for='toggle-all'] {
background: none; background: none;
} }
} }
.hidden{
display:none;
}
\ No newline at end of file
...@@ -14,8 +14,9 @@ define([ ...@@ -14,8 +14,9 @@ define([
// Set the current filter to be used // Set the current filter to be used
Common.TodoFilter = param.trim() || ''; Common.TodoFilter = param.trim() || '';
// Trigger a collection reset/addAll // Trigger a collection filter event, causing hiding/unhiding
Todos.trigger('reset'); // of the Todo view items
Todos.trigger('filter');
} }
}); });
......
...@@ -33,10 +33,12 @@ define([ ...@@ -33,10 +33,12 @@ define([
this.$footer = this.$('#footer'); this.$footer = this.$('#footer');
this.$main = this.$('#main'); this.$main = this.$('#main');
Todos.on( 'add', this.addAll, this ); Todos.on( 'add', this.addOne, this );
Todos.on( 'reset', this.addAll, this ); Todos.on( 'reset', this.addAll, this );
Todos.on( 'change:completed', this.addAll, this ); Todos.on( 'change:completed', this.filterOne, this );
Todos.on( "filter", this.filterAll, this);
Todos.on( 'all', this.render, this ); Todos.on( 'all', this.render, this );
Todos.fetch(); Todos.fetch();
}, },
...@@ -77,18 +79,15 @@ define([ ...@@ -77,18 +79,15 @@ define([
// Add all items in the **Todos** collection at once. // Add all items in the **Todos** collection at once.
addAll: function() { addAll: function() {
this.$('#todo-list').html(''); this.$('#todo-list').html('');
Todos.each(this.addOne, this);
},
switch( Common.TodoFilter ) { filterOne : function (todo) {
case 'active': todo.trigger("visible");
_.each( Todos.remaining(), this.addOne ); },
break;
case 'completed': filterAll : function () {
_.each( Todos.completed(), this.addOne ); app.Todos.each(this.filterOne, this);
break;
default:
Todos.each( this.addOne, this );
break;
}
}, },
// Generate the attributes for a new Todo item. // Generate the attributes for a new Todo item.
......
...@@ -27,6 +27,7 @@ define([ ...@@ -27,6 +27,7 @@ define([
initialize: function() { initialize: function() {
this.model.on( 'change', this.render, this ); this.model.on( 'change', this.render, this );
this.model.on( 'destroy', this.remove, this ); this.model.on( 'destroy', this.remove, this );
this.model.on( 'visible', this.toggleVisible, this );
}, },
// Re-render the titles of the todo item. // Re-render the titles of the todo item.
...@@ -34,10 +35,23 @@ define([ ...@@ -34,10 +35,23 @@ define([
this.$el.html( this.template( this.model.toJSON() ) ); this.$el.html( this.template( this.model.toJSON() ) );
this.$el.toggleClass( 'completed', this.model.get('completed') ); this.$el.toggleClass( 'completed', this.model.get('completed') );
this.toggleVisible();
this.input = this.$('.edit'); this.input = this.$('.edit');
return this; return this;
}, },
toggleVisible : function () {
this.$el.toggleClass( 'hidden', this.isHidden());
},
isHidden : function () {
var isCompleted = this.model.get('completed');
return ( // hidden cases only
(!isCompleted && Common.TodoFilter === 'completed')
|| (isCompleted && Common.TodoFilter === 'active')
);
},
// Toggle the `"completed"` state of the model. // Toggle the `"completed"` state of the model.
togglecompleted: function() { togglecompleted: function() {
this.model.toggle(); this.model.toggle();
......
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