/framework/js/todos.js

https://github.com/dtuite/backbone-presentation-part-2 · JavaScript · 154 lines · 69 code · 37 blank · 48 comment · 1 complexity · 3be8f2c42ad6e320f0237149b703c54c MD5 · raw file

  1. // An example Backbone application contributed by
  2. // [Jérôme Gravel-Niquet](http://jgn.me/). This demo uses a simple
  3. // [LocalStorage adapter](backbone-localstorage.html)
  4. // to persist Backbone models within your browser.
  5. // Load the application once the DOM is ready, using `jQuery.ready`:
  6. $(function(){
  7. // Todo Model
  8. // ----------
  9. // Our basic **Todo** model has `text`, `order`, and `done` attributes.
  10. window.Todo = Backbone.Model.extend({
  11. // Default attributes for a todo item.
  12. defaults: function() {
  13. return {
  14. done: false,
  15. order: Todos.nextOrder()
  16. };
  17. },
  18. // Toggle the `done` state of this todo item.
  19. toggle: function() {
  20. this.save({done: !this.get("done")});
  21. }
  22. });
  23. // Todo Collection
  24. // ---------------
  25. // The collection of todos is backed by *localStorage* instead of a remote
  26. // server.
  27. window.TodoList = Backbone.Collection.extend({
  28. // Reference to this collection's model.
  29. model: Todo,
  30. // Save all of the todo items under the `"todos"` namespace.
  31. localStorage: new Store("todos"),
  32. // Filter down the list of all todo items that are finished.
  33. done: function() {
  34. // Looks through each value in the list, returning an array of all the values that pass a truth test
  35. return this.filter(function(todo){ return todo.get('done'); });
  36. },
  37. // Filter down the list to only todo items that are still not finished.
  38. remaining: function() {
  39. // Returns a copy of the array with all instances of the values removed
  40. return this.without.apply(this, this.done());
  41. },
  42. // We keep the Todos in sequential order, despite being saved by unordered
  43. // GUID in the database. This generates the next order number for new items.
  44. nextOrder: function() {
  45. if (!this.length) return 1;
  46. return this.last().get('order') + 1;
  47. },
  48. // Todos are sorted by their original insertion order.
  49. // comparators are used to maintain order in a collection
  50. // this comparator orders todo's based on the value of the 'order' attribute
  51. comparator: function(todo) {
  52. return todo.get('order');
  53. }
  54. });
  55. // Create our global collection of **Todos**.
  56. window.Todos = new TodoList;
  57. // Todo Item View
  58. // --------------
  59. // The DOM element for a todo item...
  60. window.TodoView = Backbone.View.extend({
  61. //... is a list tag.
  62. tagName: "li",
  63. // Cache the template function for a single item.
  64. template: _.template($('#item-template').html()),
  65. // The TodoView listens for changes to its model, re-rendering.
  66. initialize: function() {
  67. },
  68. // Re-render the contents of the todo item.
  69. render: function() {
  70. $(this.el).html(this.template(this.model.toJSON()));
  71. this.setText();
  72. return this;
  73. },
  74. // To avoid XSS (not that it would be harmful in this particular app),
  75. // we use `jQuery.text` to set the contents of the todo item.
  76. setText: function() {
  77. var text = this.model.get('text');
  78. this.$('.todo-text').text(text);
  79. this.input = this.$('.todo-input');
  80. },
  81. // Remove this view from the DOM.
  82. remove: function() {
  83. $(this.el).remove();
  84. },
  85. });
  86. // The Application
  87. // ---------------
  88. // Our overall **AppView** is the top-level piece of UI.
  89. window.AppView = Backbone.View.extend({
  90. // Instead of generating a new element, bind to the existing skeleton of
  91. // the App already present in the HTML.
  92. el: $("#todoapp"),
  93. // At initialization we bind to the relevant events on the `Todos`
  94. // collection, when items are added or changed. Kick things off by
  95. // loading any preexisting todos that might be saved in *localStorage*.
  96. initialize: function() {
  97. this.input = this.$("#new-todo");
  98. Todos.bind('add', this.addOne, this);
  99. Todos.bind('reset', this.addAll, this);
  100. Todos.bind('all', this.render, this);
  101. Todos.fetch();
  102. },
  103. render: function() {},
  104. // Add a single todo item to the list by creating a view for it, and
  105. // appending its element to the `<ul>`.
  106. addOne: function(todo) {
  107. var view = new TodoView({model: todo});
  108. this.$("#todo-list").append(view.render().el);
  109. },
  110. // Add all items in the **Todos** collection at once.
  111. addAll: function() {
  112. Todos.each(this.addOne);
  113. },
  114. // put the createOnEnter fn here
  115. });
  116. window.App = new AppView();
  117. });