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

/public/js/collections/todos.js

https://bitbucket.org/asivura/todos
JavaScript | 40 lines | 27 code | 7 blank | 6 comment | 1 complexity | 4cb2c92ba775abd5774914ce267c1af8 MD5 | raw file
  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. url:'/todos',
  11. // Filter down the list of all todo items that are finished.
  12. done:function () {
  13. return this.filter(function (todo) {
  14. return todo.get('done');
  15. });
  16. },
  17. // Filter down the list to only todo items that are still not finished.
  18. remaining:function () {
  19. return this.without.apply(this, this.done());
  20. },
  21. // We keep the Todos in sequential order, despite being saved by unordered
  22. // GUID in the database. This generates the next order number for new items.
  23. nextOrder:function () {
  24. if (!this.length) return 1;
  25. return this.last().get('order') + 1;
  26. },
  27. // Todos are sorted by their original insertion order.
  28. comparator:function (todo) {
  29. return todo.get('order');
  30. }
  31. });
  32. return new TodosCollection;
  33. });