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