PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/Backbone/Scripts/todos/app.js

http://github.com/andrewdavey/cassette
JavaScript | 172 lines | 127 code | 42 blank | 3 comment | 11 complexity | d8d89bd1a6cabb638441e6b3b6083864 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // @reference templates
  2. // @reference ../lib
  3. // @reference ~/styles/todos
  4. $(function(){
  5. window.Todo = Backbone.Model.extend({
  6. defaults: function() {
  7. return {
  8. done: false,
  9. order: Todos.nextOrder()
  10. };
  11. },
  12. toggle: function() {
  13. this.save({done: !this.get("done")});
  14. }
  15. });
  16. window.TodoList = Backbone.Collection.extend({
  17. model: Todo,
  18. localStorage: new Store("todos"),
  19. done: function() {
  20. return this.filter(function(todo){ return todo.get('done'); });
  21. },
  22. remaining: function() {
  23. return this.without.apply(this, this.done());
  24. },
  25. nextOrder: function() {
  26. if (!this.length) return 1;
  27. return this.last().get('order') + 1;
  28. },
  29. comparator: function(todo) {
  30. return todo.get('order');
  31. }
  32. });
  33. window.Todos = new TodoList;
  34. window.TodoView = Backbone.View.extend({
  35. tagName: "li",
  36. events: {
  37. "click .check" : "toggleDone",
  38. "dblclick div.todo-text" : "edit",
  39. "click span.todo-destroy" : "clear",
  40. "keypress .todo-input" : "updateOnEnter"
  41. },
  42. initialize: function() {
  43. this.template = JST["item"];
  44. this.model.bind('change', this.render, this);
  45. this.model.bind('destroy', this.remove, this);
  46. },
  47. render: function() {
  48. $(this.el).html(this.template.render(this.model.toJSON()));
  49. this.setText();
  50. return this;
  51. },
  52. setText: function() {
  53. var text = this.model.get('text');
  54. this.$('.todo-text').text(text);
  55. this.input = this.$('.todo-input');
  56. this.input.bind('blur', _.bind(this.close, this)).val(text);
  57. },
  58. toggleDone: function() {
  59. this.model.toggle();
  60. },
  61. edit: function() {
  62. $(this.el).addClass("editing");
  63. this.input.focus();
  64. },
  65. close: function() {
  66. this.model.save({text: this.input.val()});
  67. $(this.el).removeClass("editing");
  68. },
  69. updateOnEnter: function(e) {
  70. if (e.keyCode == 13) this.close();
  71. },
  72. remove: function() {
  73. $(this.el).remove();
  74. },
  75. clear: function() {
  76. this.model.destroy();
  77. }
  78. });
  79. window.AppView = Backbone.View.extend({
  80. el: $("#todoapp"),
  81. events: {
  82. "keypress #new-todo": "createOnEnter",
  83. "keyup #new-todo": "showTooltip",
  84. "click .todo-clear a": "clearCompleted"
  85. },
  86. initialize: function() {
  87. this.template = JST["stats"];
  88. this.input = this.$("#new-todo");
  89. Todos.bind('add', this.addOne, this);
  90. Todos.bind('reset', this.addAll, this);
  91. Todos.bind('all', this.render, this);
  92. Todos.fetch();
  93. },
  94. render: function() {
  95. this.$('#todo-stats').html(this.template.render({
  96. total: Todos.length,
  97. done: Todos.done().length,
  98. remaining: Todos.remaining().length,
  99. isItemsPlural: Todos.remaining().length !== 1
  100. }));
  101. },
  102. addOne: function(todo) {
  103. var view = new TodoView({model: todo});
  104. $("#todo-list").append(view.render().el);
  105. },
  106. addAll: function() {
  107. Todos.each(this.addOne);
  108. },
  109. createOnEnter: function(e) {
  110. var text = this.input.val();
  111. if (!text || e.keyCode != 13) return;
  112. Todos.create({text: text});
  113. this.input.val('');
  114. },
  115. clearCompleted: function() {
  116. _.each(Todos.done(), function(todo){ todo.destroy(); });
  117. return false;
  118. },
  119. showTooltip: function(e) {
  120. var tooltip = this.$(".ui-tooltip-top");
  121. var val = this.input.val();
  122. tooltip.fadeOut();
  123. if (this.tooltipTimeout) clearTimeout(this.tooltipTimeout);
  124. if (val == '' || val == this.input.attr('placeholder')) return;
  125. var show = function(){ tooltip.show().fadeIn(); };
  126. this.tooltipTimeout = _.delay(show, 1000);
  127. }
  128. });
  129. window.App = new AppView;
  130. });