Commit 69cb6f7c authored by Dave Methvin's avatar Dave Methvin Committed by Sam Saccone

jQuery: Fixups

* Remove unused variables
* Simplify cached elements, IDs are unique
* Use chaining for event binding
* Use IDs directly, no benefit in caching
* DRY out the update logic
* Remove cacheElements since it's now named wrong
parent b2d25b5a
...@@ -41,7 +41,8 @@ jQuery(function ($) { ...@@ -41,7 +41,8 @@ jQuery(function ($) {
var App = { var App = {
init: function () { init: function () {
this.todos = util.store('todos-jquery'); this.todos = util.store('todos-jquery');
this.cacheElements(); this.todoTemplate = Handlebars.compile($('#todo-template').html());
this.footerTemplate = Handlebars.compile($('#footer-template').html());
this.bindEvents(); this.bindEvents();
new Router({ new Router({
...@@ -51,37 +52,24 @@ jQuery(function ($) { ...@@ -51,37 +52,24 @@ jQuery(function ($) {
}.bind(this) }.bind(this)
}).init('/all'); }).init('/all');
}, },
cacheElements: function () {
this.todoTemplate = Handlebars.compile($('#todo-template').html());
this.footerTemplate = Handlebars.compile($('#footer-template').html());
this.$todoApp = $('#todoapp');
this.$header = this.$todoApp.find('#header');
this.$main = this.$todoApp.find('#main');
this.$footer = this.$todoApp.find('#footer');
this.$newTodo = this.$header.find('#new-todo');
this.$toggleAll = this.$main.find('#toggle-all');
this.$todoList = this.$main.find('#todo-list');
this.$count = this.$footer.find('#todo-count');
this.$clearBtn = this.$footer.find('#clear-completed');
},
bindEvents: function () { bindEvents: function () {
var list = this.$todoList; $('#new-todo').on('keyup', this.create.bind(this));
this.$newTodo.on('keyup', this.create.bind(this)); $('#toggle-all').on('change', this.toggleAll.bind(this));
this.$toggleAll.on('change', this.toggleAll.bind(this)); $('#footer').on('click', '#clear-completed', this.destroyCompleted.bind(this));
this.$footer.on('click', '#clear-completed', this.destroyCompleted.bind(this)); $('#todo-list')
list.on('change', '.toggle', this.toggle.bind(this)); .on('change', '.toggle', this.toggle.bind(this))
list.on('dblclick', 'label', this.edit.bind(this)); .on('dblclick', 'label', this.edit.bind(this))
list.on('keyup', '.edit', this.editKeyup.bind(this)); .on('keyup', '.edit', this.editKeyup.bind(this))
list.on('focusout', '.edit', this.update.bind(this)); .on('focusout', '.edit', this.update.bind(this))
list.on('click', '.destroy', this.destroy.bind(this)); .on('click', '.destroy', this.destroy.bind(this));
}, },
render: function () { render: function () {
var todos = this.getFilteredTodos(); var todos = this.getFilteredTodos();
this.$todoList.html(this.todoTemplate(todos)); $('#todo-list').html(this.todoTemplate(todos));
this.$main.toggle(todos.length > 0); $('#main').toggle(todos.length > 0);
this.$toggleAll.prop('checked', this.getActiveTodos().length === 0); $('#toggle-all').prop('checked', this.getActiveTodos().length === 0);
this.renderFooter(); this.renderFooter();
this.$newTodo.focus(); $('#new-todo').focus();
util.store('todos-jquery', this.todos); util.store('todos-jquery', this.todos);
}, },
renderFooter: function () { renderFooter: function () {
...@@ -94,7 +82,7 @@ jQuery(function ($) { ...@@ -94,7 +82,7 @@ jQuery(function ($) {
filter: this.filter filter: this.filter
}); });
this.$footer.toggle(todoCount > 0).html(template); $('#footer').toggle(todoCount > 0).html(template);
}, },
toggleAll: function (e) { toggleAll: function (e) {
var isChecked = $(e.target).prop('checked'); var isChecked = $(e.target).prop('checked');
...@@ -185,18 +173,15 @@ jQuery(function ($) { ...@@ -185,18 +173,15 @@ jQuery(function ($) {
var $el = $(el); var $el = $(el);
var val = $el.val().trim(); var val = $el.val().trim();
if ($el.data('abort')) { if (!val) {
$el.data('abort', false); this.destroy(e);
this.render();
return; return;
} }
var i = this.indexFromEl(el); if ($el.data('abort')) {
$el.data('abort', false);
if (val) {
this.todos[i].title = val;
} else { } else {
this.todos.splice(i, 1); this.todos[this.indexFromEl(el)].title = val;
} }
this.render(); this.render();
......
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