From f7e1b55e3ef0378b185312a3a95b7b84ac387fe5 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Wed, 1 May 2013 11:08:06 -0400 Subject: [PATCH] Array.filter && !empty todos && `addItem` simplification. --- vanilla-examples/vanillajs/js/controller.js | 18 +++++++-------- vanilla-examples/vanillajs/js/store.js | 25 ++++++--------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/vanilla-examples/vanillajs/js/controller.js b/vanilla-examples/vanillajs/js/controller.js index de55108fb7..56b9109dea 100644 --- a/vanilla-examples/vanillajs/js/controller.js +++ b/vanilla-examples/vanillajs/js/controller.js @@ -83,19 +83,16 @@ var title = title || ''; if (e.keyCode === this.ENTER_KEY) { + if (e.target.value.trim() === '') { + return; + } + this.model.create(e.target.value, function (data) { - // We want to make sure we don't add incomplete - // items to the completed tab when you go to - // add an item and you're viewing the completed - // items - if (this._getCurrentPage() !== 'completed') { - this.$todoList.innerHTML = this.$todoList.innerHTML + this.view.show(data); - } input.value = ''; + this._filter(true); }.bind(this)); } - this._filter(); }; /** @@ -290,8 +287,9 @@ /** * Re-filters the todo items, based on the active route. + * @param {boolean|undefined} force forces a re-painting of todo items. */ - Controller.prototype._filter = function () { + Controller.prototype._filter = function (force) { var activeRoute = this._activeRoute.charAt(0).toUpperCase() + this._activeRoute.substr(1); // Update the elements on the page, which change with each completed todo @@ -300,7 +298,7 @@ // If the last active route isn't "All", or we're switching routes, we // re-create the todo item elements, calling: // this.show[All|Active|Completed](); - if (this._lastActiveRoute !== 'All' || this._lastActiveRoute !== activeRoute) { + if (force || this._lastActiveRoute !== 'All' || this._lastActiveRoute !== activeRoute) { this['show' + activeRoute](); } diff --git a/vanilla-examples/vanillajs/js/store.js b/vanilla-examples/vanillajs/js/store.js index 2fd27fb399..0e37ae196c 100644 --- a/vanilla-examples/vanillajs/js/store.js +++ b/vanilla-examples/vanillajs/js/store.js @@ -43,28 +43,17 @@ * }); */ Store.prototype.find = function (query, callback) { - var data = JSON.parse(localStorage[this._dbName]).todos; - var items = []; - var found; + if (!callback) { + return; + } - callback = callback || function () {}; + var todos = JSON.parse(localStorage[this._dbName]).todos; - for (var i = 0; i < data.length; i++) { + callback.call(this, todos.filter(function (todo) { for (var q in query) { - if (query[q] !== data[i][q]) { - found = false; - break; - } else { - found = true; - } + return query[q] === todo[q]; } - - if (found) { - items.push(data[i]); - } - } - - callback.call(this, items); + })); }; /**