todomvc
Version:
> Helping you select an MV\* framework
97 lines (80 loc) • 2.09 kB
JavaScript
/*global TodoMVC */
'use strict';
TodoMVC.module('Layout', function (Layout, App, Backbone) {
// Layout Header View
// ------------------
Layout.Header = Backbone.Marionette.ItemView.extend({
template: '#template-header',
// UI bindings create cached attributes that
// point to jQuery selected objects
ui: {
input: '#new-todo'
},
events: {
'keypress @ui.input': 'onInputKeypress'
},
onInputKeypress: function (e) {
var ENTER_KEY = 13,
todoText = this.ui.input.val().trim();
if (e.which === ENTER_KEY && todoText) {
this.collection.create({
title: todoText
});
this.ui.input.val('');
}
}
});
// Layout Footer View
// ------------------
Layout.Footer = Backbone.Marionette.ItemView.extend({
template: '#template-footer',
// UI bindings create cached attributes that
// point to jQuery selected objects
ui: {
filters: '#filters a',
completed: '.completed a',
active: '.active a',
all: '.all a',
summary: '#todo-count',
clear: '#clear-completed'
},
events: {
'click @ui.clear': 'onClearClick'
},
collectionEvents: {
'all': 'render'
},
templateHelpers: {
activeCountLabel: function () {
return (this.activeCount === 1 ? 'item' : 'items') + ' left';
}
},
initialize: function () {
this.listenTo(App.request('filterState'), 'change:filter', this.updateFilterSelection, this);
},
serializeData: function () {
var active = this.collection.getActive().length;
var total = this.collection.length;
return {
activeCount: active,
totalCount: total,
completedCount: total - active
};
},
onRender: function () {
this.$el.parent().toggle(this.collection.length > 0);
this.updateFilterSelection();
},
updateFilterSelection: function () {
this.ui.filters.removeClass('selected');
this.ui[App.request('filterState').get('filter')]
.addClass('selected');
},
onClearClick: function () {
var completed = this.collection.getCompleted();
completed.forEach(function (todo) {
todo.destroy();
});
}
});
});