PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/dependency-examples/backbone_require/js/collections/todos.js

http://github.com/addyosmani/todomvc
JavaScript | 40 lines | 25 code | 8 blank | 7 comment | 1 complexity | e24725541b114076ce7b0b2f153ec7af MD5 | raw file
Possible License(s): MIT, Apache-2.0, 0BSD, CC-BY-4.0, BSD-3-Clause
  1. define([
  2. 'underscore',
  3. 'backbone',
  4. 'libs/backbone/localstorage',
  5. 'models/todo'
  6. ], function(_, Backbone, Store, Todo){
  7. var TodosCollection = Backbone.Collection.extend({
  8. // Reference to this collection's model.
  9. model: Todo,
  10. // Save all of the todo items under the `"todos"` namespace.
  11. localStorage: new Store("todos-backbone-require"),
  12. // Filter down the list of all todo items that are finished.
  13. done: function() {
  14. return this.filter(function(todo){ return todo.get('done'); });
  15. },
  16. // Filter down the list to only todo items that are still not finished.
  17. remaining: function() {
  18. return this.without.apply(this, this.done());
  19. },
  20. // We keep the Todos in sequential order, despite being saved by unordered
  21. // GUID in the database. This generates the next order number for new items.
  22. nextOrder: function() {
  23. if (!this.length) return 1;
  24. return this.last().get('order') + 1;
  25. },
  26. // Todos are sorted by their original insertion order.
  27. comparator: function(todo) {
  28. return todo.get('order');
  29. }
  30. });
  31. return new TodosCollection;
  32. });