PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/architecture-examples/backbone/js/collections/todos.js

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