Skip to content

Commit

Permalink
Array.filter && !empty todos && addItem simplification.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed May 1, 2013
1 parent e6f1497 commit f7e1b55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
18 changes: 8 additions & 10 deletions vanilla-examples/vanillajs/js/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

/**
Expand Down Expand Up @@ -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
Expand All @@ -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]();
}

Expand Down
25 changes: 7 additions & 18 deletions vanilla-examples/vanillajs/js/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
};

/**
Expand Down

0 comments on commit f7e1b55

Please sign in to comment.