PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/js/models/Todo.js

http://github.com/radekstepan/fundamental.js
JavaScript | 59 lines | 30 code | 13 blank | 16 comment | 1 complexity | 47222fba5dc9e08a6a45567773c03bcc MD5 | raw file
  1. // Todo Item Model
  2. // ----------
  3. var Todo = Backbone.Model.extend({
  4. // Default attributes for a todo item. Attribute `text` is implied.
  5. defaults: function() {
  6. return {
  7. "done": false,
  8. "order": App.Models.Todos.nextOrder() // what if I can not haz initialized?
  9. };
  10. },
  11. // Toggle the `done` state of this todo item.
  12. toggle: function() {
  13. this.save({"done": !this.get("done")});
  14. }
  15. });
  16. // Todo Items Collection
  17. // ---------------
  18. // The collection of todos is backed by *localStorage* instead of a remote server.
  19. var TodoList = Backbone.Collection.extend({
  20. // Reference to this collection's model.
  21. // Override this property to specify the model class that the collection contains.
  22. "model": Todo,
  23. // Save all of the todo items under the `"todos"` namespace.
  24. "localStorage": new Store("todos"),
  25. // Filter down the list of all todo items that are finished.
  26. done: function() {
  27. return this.filter(function (todo) {
  28. return todo.get("done"); // all items with `done` attr set to true
  29. });
  30. },
  31. // Filter down the list to only todo items that are still not finished.
  32. remaining: function() {
  33. return this.without.apply(this, this.done()); // reverse of `done()` filter
  34. },
  35. // We keep the **Todos** in sequential order, despite being saved by unordered
  36. // GUID in the database. This generates the next order number for new items.
  37. nextOrder: function() {
  38. if (!this.length) return 1; // first item
  39. return this.last().get("order") + 1; // last +1
  40. },
  41. // Todos are sorted by their original insertion order.
  42. // If you define a comparator, it will be used to maintain the collection in sorted order.
  43. comparator: function(todo) {
  44. return todo.get("order");
  45. }
  46. });