Commit 868271f6 authored by Addy Osmani's avatar Addy Osmani

Merge pull request #558 from thebakeryio/refactoring/update-count

Refactoring/update count
parents d3e1ac8b 5f742d27
...@@ -23,10 +23,13 @@ ...@@ -23,10 +23,13 @@
<p>Further variations on the Backbone.Marionette app are also <a href="https://github.com/marionettejs/backbone.marionette/wiki/Projects-and-websites-using-marionette">available</a>.</p> <p>Further variations on the Backbone.Marionette app are also <a href="https://github.com/marionettejs/backbone.marionette/wiki/Projects-and-websites-using-marionette">available</a>.</p>
</footer> </footer>
<script type="text/html" id="template-footer"> <script type="text/html" id="template-footer">
<span id="todo-count"><strong></strong><span></span></span> <span id="todo-count">
<strong><%= activeCount %></strong>
<span> <%= activeCountLabel() %></span>
</span>
<ul id="filters"> <ul id="filters">
<li> <li>
<a href="#">All</a> <a href="#" class="selected">All</a>
</li> </li>
<li> <li>
<a href="#active">Active</a> <a href="#active">Active</a>
...@@ -35,7 +38,10 @@ ...@@ -35,7 +38,10 @@
<a href="#completed">Completed</a> <a href="#completed">Completed</a>
</li> </li>
</ul> </ul>
<button id="clear-completed"></button>
<% if (completedCount > 0) { %>
<button id="clear-completed">Clear completed (<%= completedCount %>)</button>
<% } %>
</script> </script>
<script type="text/html" id="template-header"> <script type="text/html" id="template-header">
<h1>todos</h1> <h1>todos</h1>
......
...@@ -38,10 +38,7 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) { ...@@ -38,10 +38,7 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) {
// UI bindings create cached attributes that // UI bindings create cached attributes that
// point to jQuery selected objects // point to jQuery selected objects
ui: { ui: {
count: '#todo-count strong', filters: '#filters a'
itemsString: '#todo-count span',
filters: '#filters a',
clearCompleted: '#clear-completed'
}, },
events: { events: {
...@@ -49,33 +46,32 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) { ...@@ -49,33 +46,32 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) {
}, },
collectionEvents: { collectionEvents: {
'all': 'updateCount' 'all': 'render'
}, },
initialize: function () { templateHelpers: {
this.listenTo(App.vent, 'todoList:filter', this.updateFilterSelection, this); activeCountLabel : function(){
return (this.activeCount === 1 ? 'item' : 'items') + ' left';
}
}, },
onRender: function () { initialize: function () {
this.updateCount(); this.listenTo(App.vent, 'todoList:filter', this.updateFilterSelection, this);
}, },
updateCount: function () { serializeData : function(){
var count = this.collection.getActive().length; var active = this.collection.getActive().length;
var length = this.collection.length; var total = this.collection.length;
var completed = length - count;
this.ui.count.html(count);
this.ui.itemsString.html(' ' + (count === 1 ? 'item' : 'items') + ' left');
this.$el.parent().toggle(length > 0);
if (completed > 0) { return {
this.ui.clearCompleted.show(); activeCount : active,
this.ui.clearCompleted.html('Clear completed (' + completed + ')'); totalCount : total,
} else { completedCount : total - active
this.ui.clearCompleted.hide(); };
} },
onRender: function () {
this.$el.parent().toggle(this.collection.length > 0);
}, },
updateFilterSelection: function (filter) { updateFilterSelection: function (filter) {
......
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