Skip to content

Commit

Permalink
Merge pull request tastejs#558 from thebakeryio/refactoring/update-count
Browse files Browse the repository at this point in the history
Refactoring/update count
  • Loading branch information
addyosmani committed May 17, 2013
2 parents d3e1ac8 + 5f742d2 commit 868271f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
12 changes: 9 additions & 3 deletions labs/architecture-examples/backbone_marionette/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
</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">
<li>
<a href="#">All</a>
<a href="#" class="selected">All</a>
</li>
<li>
<a href="#active">Active</a>
Expand All @@ -35,7 +38,10 @@
<a href="#completed">Completed</a>
</li>
</ul>
<button id="clear-completed"></button>

<% if (completedCount > 0) { %>
<button id="clear-completed">Clear completed (<%= completedCount %>)</button>
<% } %>
</script>
<script type="text/html" id="template-header">
<h1>todos</h1>
Expand Down
42 changes: 19 additions & 23 deletions labs/architecture-examples/backbone_marionette/js/TodoMVC.Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,40 @@ TodoMVC.module('Layout', function (Layout, App, Backbone) {
// UI bindings create cached attributes that
// point to jQuery selected objects
ui: {
count: '#todo-count strong',
itemsString: '#todo-count span',
filters: '#filters a',
clearCompleted: '#clear-completed'
filters: '#filters a'
},

events: {
'click #clear-completed': 'onClearClick'
},

collectionEvents: {
'all': 'updateCount'
'all': 'render'
},

initialize: function () {
this.listenTo(App.vent, 'todoList:filter', this.updateFilterSelection, this);
templateHelpers: {
activeCountLabel : function(){
return (this.activeCount === 1 ? 'item' : 'items') + ' left';
}
},

onRender: function () {
this.updateCount();
initialize: function () {
this.listenTo(App.vent, 'todoList:filter', this.updateFilterSelection, this);
},

updateCount: function () {
var count = this.collection.getActive().length;
var length = 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);
serializeData : function(){
var active = this.collection.getActive().length;
var total = this.collection.length;

if (completed > 0) {
this.ui.clearCompleted.show();
this.ui.clearCompleted.html('Clear completed (' + completed + ')');
} else {
this.ui.clearCompleted.hide();
}
return {
activeCount : active,
totalCount : total,
completedCount : total - active
};
},

onRender: function () {
this.$el.parent().toggle(this.collection.length > 0);
},

updateFilterSelection: function (filter) {
Expand Down

0 comments on commit 868271f

Please sign in to comment.