Commit 410f3a46 authored by Gilad Peleg's avatar Gilad Peleg

improve collection flow in backbone examples

parent 6ddcbab2
......@@ -29,17 +29,11 @@ var app = app || {};
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function () {
if (!this.length) {
return 1;
}
return this.last().get('order') + 1;
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function (todo) {
return todo.get('order');
}
comparator: 'order'
});
// Create our global collection of **Todos**.
......
......@@ -27,17 +27,11 @@ define([
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function () {
if (!this.length) {
return 1;
}
return this.last().get('order') + 1;
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function (todo) {
return todo.get('order');
}
comparator: 'order'
});
return new TodosCollection();
......
......@@ -41,9 +41,7 @@ TodoMVC.module('Todos', function (Todos, App, Backbone) {
return this.reject(this._isCompleted);
},
comparator: function (todo) {
return todo.get('created');
},
comparator: 'created',
_isCompleted: function (todo) {
return todo.isCompleted();
......
......@@ -18,31 +18,22 @@ var app = app || {};
// Filter down the list of all todo items that are finished.
completed: function () {
return this.filter(function (todo) {
return todo.get('completed');
});
return this.where({completed: true});
},
// Filter down the list to only todo items that are still not finished.
remaining: function () {
return this.filter(function (todo) {
return !todo.get('completed');
});
return this.where({completed: false});
},
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function () {
if (!this.length) {
return 1;
}
return this.at(this.length - 1).get('order') + 1;
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function (todo) {
return todo.get('order');
}
comparator: 'order'
});
// Create our global collection of **Todos**.
......
......@@ -18,29 +18,22 @@ var app = app || {};
// Filter down the list of all todo items that are finished.
completed: function () {
return this.filter(function (todo) {
return todo.get('completed');
});
return this.where({completed: true});
},
// Filter down the list to only todo items that are still not finished.
remaining: function () {
return this.without.apply(this, this.completed());
return this.where({completed: false});
},
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function () {
if (!this.length) {
return 1;
}
return this.last().get('order') + 1;
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function (todo) {
return todo.get('order');
}
comparator: 'order'
});
// Create our global collection of **Todos**.
......
......@@ -17,29 +17,22 @@
// Filter down the list of all todo items that are finished.
completed: function () {
return this.filter(function (todo) {
return todo.get('completed');
});
return this.where({completed: true});
},
// Filter down the list to only todo items that are still not finished.
remaining: function () {
return this.without.apply(this, this.completed());
return this.where({completed: false});
},
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function () {
if (!this.length) {
return 1;
}
return this.last().get('order') + 1;
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function (todo) {
return todo.get('order');
}
comparator: 'order'
});
// Create our global collection of **Todos**.
......
......@@ -19,8 +19,6 @@ define([
return this.where({completed: false});
},
comparator: function (todo) {
return todo.get('created');
}
comparator: 'created'
});
});
......@@ -63,29 +63,22 @@ module.routes = {"":"setFilter",":filter":"setFilter"};
// Filter down the list of all todo items that are finished.
completed: function() {
return this.filter(function( todo ) {
return todo.get('completed');
});
return this.where({completed: true});
},
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.without.apply( this, this.completed() );
return this.where({completed: false});
},
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function() {
if ( !this.length ) {
return 1;
}
return this.last().get('order') + 1;
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function( todo ) {
return todo.get('order');
}
comparator: 'order'
});
// Create our global collection of **Todos**.
......
......@@ -15,30 +15,23 @@
localStorage: new Store('todos-backbone-thorax'),
// Filter down the list of all todo items that are finished.
completed: function() {
return this.filter(function( todo ) {
return todo.get('completed');
});
completed: function () {
return this.where({completed: true});
},
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.without.apply( this, this.completed() );
remaining: function () {
return this.where({completed: false});
},
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function() {
if ( !this.length ) {
return 1;
}
return this.last().get('order') + 1;
nextOrder: function () {
return this.length ? this.last().get('order') + 1 : 1;
},
// Todos are sorted by their original insertion order.
comparator: function( todo ) {
return todo.get('order');
}
comparator: 'order'
});
// Create our global collection of **Todos**.
......
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