Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring/update count #558

Merged
merged 1 commit into from
May 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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