PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/1manStartup/todomvc
JavaScript | 43 lines | 29 code | 7 blank | 7 comment | 1 complexity | 9cc86bccfb7002378921271efea7c82c MD5 | raw file
  1. define([
  2. 'underscore',
  3. 'backbone',
  4. 'lib/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'),
  12. // Filter down the list of all todo items that are finished.
  13. completed: function() {
  14. return this.filter(function( todo ) {
  15. return todo.get('completed');
  16. });
  17. },
  18. // Filter down the list to only todo items that are still not finished.
  19. remaining: function() {
  20. return this.without.apply( this, this.completed() );
  21. },
  22. // We keep the Todos in sequential order, despite being saved by unordered
  23. // GUID in the database. This generates the next order number for new items.
  24. nextOrder: function() {
  25. if ( !this.length ) {
  26. return 1;
  27. }
  28. return this.last().get('order') + 1;
  29. },
  30. // Todos are sorted by their original insertion order.
  31. comparator: function( todo ) {
  32. return todo.get('order');
  33. }
  34. });
  35. return new TodosCollection();
  36. });