Commit be29feaf authored by Addy Osmani's avatar Addy Osmani

Merge pull request #27 from sindresorhus/master

jQuery example - added count on Clear button
parents 32fdff67 d6dfa8c4
...@@ -30,9 +30,7 @@ ...@@ -30,9 +30,7 @@
<ul class="items"></ul> <ul class="items"></ul>
<footer> <footer>
<a class="clear">Clear completed</a> <a class="clear">Clear completed</a>
<div class="count"> <div class="count"></div>
<span class="countVal"></span> items left
</div>
</footer> </footer>
</div> </div>
<div id='instructions'> <div id='instructions'>
......
...@@ -25,13 +25,20 @@ jQuery(function($){ ...@@ -25,13 +25,20 @@ jQuery(function($){
uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16); uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);
} }
return uuid; return uuid;
},
pluralize: function(count, word) {
return count === 1 ? word : word + 's';
} }
}; };
var App = { var App = {
init: function() { init: function() {
this.element = $('#todoapp'); this.$template = $('#todo-template');
this.todoList = $('.items'); this.$todoApp = $('#todoapp');
this.$todoList = this.$todoApp.find('.items');
this.$footer = this.$todoApp.find('footer');
this.$count = this.$footer.find('.count');
this.$clearBtn = this.$footer.find('.clear');
// localStorage support // localStorage support
this.store = new Store('todoapp'); this.store = new Store('todoapp');
this.todos = this.store.get('todos') || []; this.todos = this.store.get('todos') || [];
...@@ -39,18 +46,14 @@ jQuery(function($){ ...@@ -39,18 +46,14 @@ jQuery(function($){
this.render(); this.render();
}, },
render: function() { render: function() {
var template = $('#todo-template').tmpl( this.todos ); var html = this.$template.tmpl( this.todos );
this.todoList.html( template ); this.$todoList.html( html );
this.renderActiveTodoCount(); this.renderFooter();
this.store.set('todos', this.todos); this.store.set('todos', this.todos);
// Only show the footer when there are at least one todo.
$('footer').toggle( !!this.todos.length );
// Only show the clear button when there are done todos.
$('.clear').toggle( !!( this.todos.length - this.renderActiveTodoCount() ) );
}, },
bindEvents: function() { bindEvents: function() {
var elem = this.element, var elem = this.$todoApp,
list = this.todoList; list = this.$todoList;
elem.on('click', '.clear', this.destroyDone); elem.on('click', '.clear', this.destroyDone);
elem.on('submit', 'form', this.create); elem.on('submit', 'form', this.create);
list.on('change', 'input[type="checkbox"]', this.toggle); list.on('change', 'input[type="checkbox"]', this.toggle);
...@@ -59,16 +62,28 @@ jQuery(function($){ ...@@ -59,16 +62,28 @@ jQuery(function($){
list.on('blur', 'input[type="text"]', this.update); list.on('blur', 'input[type="text"]', this.update);
list.on('click', '.destroy', this.destroy); list.on('click', '.destroy', this.destroy);
}, },
renderActiveTodoCount: function() { activeTodoCount: function() {
var count = 0; var count = 0;
$.each(this.todos, function(i, val) { $.each(this.todos, function(i, val) {
if ( !val.done ) { if ( !val.done ) {
count++; count++;
} }
}); });
$('.countVal').text( count );
return count; return count;
}, },
renderFooter: function() {
var todoCount = this.todos.length,
activeTodos = this.activeTodoCount(),
completedTodos = todoCount - activeTodos,
countTitle = '<b>' + activeTodos + '</b> ' + Utils.pluralize( activeTodos, 'item' ) + ' left';
clearTitle = 'Clear ' + completedTodos + ' completed ' + Utils.pluralize( completedTodos, 'item' );
// Only show the footer when there are at least one todo.
this.$footer.toggle( !!todoCount );
// Active todo count
this.$count.html( countTitle );
// Toggle clear button and update title
this.$clearBtn.text( clearTitle ).toggle( !!completedTodos );
},
destroyDone: function() { destroyDone: function() {
// Reverse loop; since we are dynamically removing items from the todos array // Reverse loop; since we are dynamically removing items from the todos array
for ( var i = App.todos.length; i--; ) { for ( var i = App.todos.length; i--; ) {
......
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