PageRenderTime 250ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/practicals/modular-todo-app/js/collections/todos.js

https://github.com/bbt123/backbone-fundamentals
JavaScript | 40 lines | 25 code | 8 blank | 7 comment | 1 complexity | 31b6dabe16085aa1982be36853f4a438 MD5 | raw file
  1. define([
  2. 'underscore',
  3. 'backbone',
  4. 'storage',
  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'),
  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. });