Commit 81906dc8 authored by Pascal Hartig's avatar Pascal Hartig

ts+backbone: Fixed footer, remaining count

The remaining todos counter was the same bug fixed in
89e5d5c7.

\#main and \#footer weren't hidden if there were no todos in the list.
parent 92fe080c
...@@ -11,20 +11,20 @@ var Todo = (function (_super) { ...@@ -11,20 +11,20 @@ var Todo = (function (_super) {
} }
Todo.prototype.defaults = function () { Todo.prototype.defaults = function () {
return { return {
content: "empty todo...", content: '',
done: false done: false
}; };
}; };
Todo.prototype.initialize = function () { Todo.prototype.initialize = function () {
if(!this.get("content")) { if(!this.get('content')) {
this.set({ this.set({
"content": this.defaults().content 'content': this.defaults().content
}); });
} }
}; };
Todo.prototype.toggle = function () { Todo.prototype.toggle = function () {
this.save({ this.save({
done: !this.get("done") done: !this.get('done')
}); });
}; };
Todo.prototype.clear = function () { Todo.prototype.clear = function () {
...@@ -38,7 +38,7 @@ var TodoList = (function (_super) { ...@@ -38,7 +38,7 @@ var TodoList = (function (_super) {
_super.apply(this, arguments); _super.apply(this, arguments);
this.model = Todo; this.model = Todo;
this.localStorage = new Store("todos-backbone"); this.localStorage = new Store('todos-typescript-backbone');
} }
TodoList.prototype.done = function () { TodoList.prototype.done = function () {
return this.filter(function (todo) { return this.filter(function (todo) {
...@@ -63,13 +63,13 @@ var Todos = new TodoList(); ...@@ -63,13 +63,13 @@ var Todos = new TodoList();
var TodoView = (function (_super) { var TodoView = (function (_super) {
__extends(TodoView, _super); __extends(TodoView, _super);
function TodoView(options) { function TodoView(options) {
this.tagName = "li"; this.tagName = 'li';
this.events = { this.events = {
"click .check": "toggleDone", 'click .check': 'toggleDone',
"dblclick label.todo-content": "edit", 'dblclick label.todo-content': 'edit',
"click button.destroy": "clear", 'click button.destroy': 'clear',
"keypress .todo-input": "updateOnEnter", 'keypress .todo-input': 'updateOnEnter',
"blur .todo-input": "close" 'blur .todo-input': 'close'
}; };
_super.call(this, options); _super.call(this, options);
this.template = _.template($('#item-template').html()); this.template = _.template($('#item-template').html());
...@@ -77,6 +77,7 @@ var TodoView = (function (_super) { ...@@ -77,6 +77,7 @@ var TodoView = (function (_super) {
this.model.bind('change', this.render); this.model.bind('change', this.render);
this.model.bind('destroy', this.remove); this.model.bind('destroy', this.remove);
} }
TodoView.ENTER_KEY = 13;
TodoView.prototype.render = function () { TodoView.prototype.render = function () {
this.$el.html(this.template(this.model.toJSON())); this.$el.html(this.template(this.model.toJSON()));
this.input = this.$('.todo-input'); this.input = this.$('.todo-input');
...@@ -86,17 +87,17 @@ var TodoView = (function (_super) { ...@@ -86,17 +87,17 @@ var TodoView = (function (_super) {
this.model.toggle(); this.model.toggle();
}; };
TodoView.prototype.edit = function () { TodoView.prototype.edit = function () {
this.$el.addClass("editing"); this.$el.addClass('editing');
this.input.focus(); this.input.focus();
}; };
TodoView.prototype.close = function () { TodoView.prototype.close = function () {
this.model.save({ this.model.save({
content: this.input.val() content: this.input.val()
}); });
this.$el.removeClass("editing"); this.$el.removeClass('editing');
}; };
TodoView.prototype.updateOnEnter = function (e) { TodoView.prototype.updateOnEnter = function (e) {
if(e.keyCode == 13) { if(e.keyCode == TodoView.ENTER_KEY) {
close(); close();
} }
}; };
...@@ -110,16 +111,16 @@ var AppView = (function (_super) { ...@@ -110,16 +111,16 @@ var AppView = (function (_super) {
function AppView() { function AppView() {
_super.call(this); _super.call(this);
this.events = { this.events = {
"keypress #new-todo": "createOnEnter", 'keypress #new-todo': 'createOnEnter',
"keyup #new-todo": "showTooltip", 'click .todo-clear button': 'clearCompleted',
"click .todo-clear button": "clearCompleted", 'click .mark-all-done': 'toggleAllComplete'
"click .mark-all-done": "toggleAllComplete"
}; };
this.tooltipTimeout = null; this.setElement($('#todoapp'), true);
this.setElement($("#todoapp"), true);
_.bindAll(this, 'addOne', 'addAll', 'render', 'toggleAllComplete'); _.bindAll(this, 'addOne', 'addAll', 'render', 'toggleAllComplete');
this.input = this.$("#new-todo"); this.input = this.$('#new-todo');
this.allCheckbox = this.$(".mark-all-done")[0]; this.allCheckbox = this.$('.mark-all-done')[0];
this.mainElement = this.$('#main')[0];
this.footerElement = this.$('#footer')[0];
this.statsTemplate = _.template($('#stats-template').html()); this.statsTemplate = _.template($('#stats-template').html());
Todos.bind('add', this.addOne); Todos.bind('add', this.addOne);
Todos.bind('reset', this.addAll); Todos.bind('reset', this.addAll);
...@@ -129,18 +130,25 @@ var AppView = (function (_super) { ...@@ -129,18 +130,25 @@ var AppView = (function (_super) {
AppView.prototype.render = function () { AppView.prototype.render = function () {
var done = Todos.done().length; var done = Todos.done().length;
var remaining = Todos.remaining().length; var remaining = Todos.remaining().length;
if(Todos.length) {
this.mainElement.style.display = 'block';
this.footerElement.style.display = 'block';
this.$('#todo-stats').html(this.statsTemplate({ this.$('#todo-stats').html(this.statsTemplate({
total: Todos.length, total: Todos.length,
done: done, done: done,
remaining: remaining remaining: remaining
})); }));
} else {
this.mainElement.style.display = 'none';
this.footerElement.style.display = 'none';
}
this.allCheckbox.checked = !remaining; this.allCheckbox.checked = !remaining;
}; };
AppView.prototype.addOne = function (todo) { AppView.prototype.addOne = function (todo) {
var view = new TodoView({ var view = new TodoView({
model: todo model: todo
}); });
this.$("#todo-list").append(view.render().el); this.$('#todo-list').append(view.render().el);
}; };
AppView.prototype.addAll = function () { AppView.prototype.addAll = function () {
Todos.each(this.addOne); Todos.each(this.addOne);
...@@ -165,20 +173,6 @@ var AppView = (function (_super) { ...@@ -165,20 +173,6 @@ var AppView = (function (_super) {
}); });
return false; return false;
}; };
AppView.prototype.showTooltip = function (e) {
var tooltip = $(".ui-tooltip-top");
var val = this.input.val();
tooltip.fadeOut();
if(this.tooltipTimeout) {
clearTimeout(this.tooltipTimeout);
}
if(val == '' || val == this.input.attr('placeholder')) {
return;
}
this.tooltipTimeout = _.delay(function () {
return tooltip.show().fadeIn();
}, 1000);
};
AppView.prototype.toggleAllComplete = function () { AppView.prototype.toggleAllComplete = function () {
var done = this.allCheckbox.checked; var done = this.allCheckbox.checked;
Todos.each(function (todo) { Todos.each(function (todo) {
......
...@@ -143,7 +143,7 @@ class TodoList extends Backbone.Collection { ...@@ -143,7 +143,7 @@ class TodoList extends Backbone.Collection {
// Filter down the list to only todo items that are still not finished. // Filter down the list to only todo items that are still not finished.
remaining() { remaining() {
return this.without(this.done()); return this.without.apply(this, this.done());
} }
// We keep the Todos in sequential order, despite being saved by unordered // We keep the Todos in sequential order, despite being saved by unordered
...@@ -254,6 +254,8 @@ class AppView extends Backbone.View { ...@@ -254,6 +254,8 @@ class AppView extends Backbone.View {
input: any; input: any;
allCheckbox: HTMLInputElement; allCheckbox: HTMLInputElement;
mainElement: HTMLElement;
footerElement: HTMLElement;
statsTemplate: (params: any) => string; statsTemplate: (params: any) => string;
constructor () { constructor () {
...@@ -269,6 +271,8 @@ class AppView extends Backbone.View { ...@@ -269,6 +271,8 @@ class AppView extends Backbone.View {
this.input = this.$('#new-todo'); this.input = this.$('#new-todo');
this.allCheckbox = this.$('.mark-all-done')[0]; this.allCheckbox = this.$('.mark-all-done')[0];
this.mainElement = this.$('#main')[0];
this.footerElement = this.$('#footer')[0];
this.statsTemplate = _.template($('#stats-template').html()); this.statsTemplate = _.template($('#stats-template').html());
Todos.bind('add', this.addOne); Todos.bind('add', this.addOne);
...@@ -284,11 +288,19 @@ class AppView extends Backbone.View { ...@@ -284,11 +288,19 @@ class AppView extends Backbone.View {
var done = Todos.done().length; var done = Todos.done().length;
var remaining = Todos.remaining().length; var remaining = Todos.remaining().length;
if (Todos.length) {
this.mainElement.style.display = 'block';
this.footerElement.style.display = 'block';
this.$('#todo-stats').html(this.statsTemplate({ this.$('#todo-stats').html(this.statsTemplate({
total: Todos.length, total: Todos.length,
done: done, done: done,
remaining: remaining remaining: remaining
})); }));
} else {
this.mainElement.style.display = 'none';
this.footerElement.style.display = 'none';
}
this.allCheckbox.checked = !remaining; this.allCheckbox.checked = !remaining;
} }
......
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