Commit f7e1b55e authored by Stephen Sawchuk's avatar Stephen Sawchuk

Array.filter && !empty todos && `addItem` simplification.

parent e6f14976
......@@ -83,19 +83,16 @@
var title = title || '';
if (e.keyCode === this.ENTER_KEY) {
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);
if (e.target.value.trim() === '') {
return;
}
this.model.create(e.target.value, function (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]();
}
......
......@@ -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);
}));
};
/**
......
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