PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/todos-requirejs-4/collections/todos.js

https://github.com/pmichelberger/backbone
JavaScript | 45 lines | 24 code | 11 blank | 10 comment | 1 complexity | 531e63ea419e6306903541b4bb046b41 MD5 | raw file
  1. // Todo Collection
  2. // ---------------
  3. define(function () {
  4. 'use strict';
  5. var module = {}, TodoList;
  6. // The collection of todos is backed by *localStorage* instead of a remote
  7. // server.
  8. TodoList = Backbone.Collection.extend({
  9. // Save all of the todo items under the 'todos' namespace.
  10. localStorage: new Store('todos'),
  11. // Filter down the list of all todo items that are finished.
  12. done: function () {
  13. return this.filter(function (todo) { return todo.get('done'); });
  14. },
  15. // Filter down the list to only todo items that are still not finished.
  16. remaining: function () {
  17. return this.without.apply(this, this.done());
  18. },
  19. // We keep the Todos in sequential order, despite being saved by unordered
  20. // GUID in the database. This generates the next order number for new items.
  21. nextOrder: function () {
  22. if (!this.length) return 1;
  23. return this.last().get('order') + 1;
  24. },
  25. // Todos are sorted by their original insertion order.
  26. comparator: function (todo) {
  27. return todo.get('order');
  28. }
  29. });
  30. module.create = function (properties) {
  31. return new TodoList(properties);
  32. }
  33. return module;
  34. });