PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/practicals/modular-todo-app/js/views/app.js

https://github.com/logicaroma/backbone-fundamentals
JavaScript | 100 lines | 67 code | 15 blank | 18 comment | 7 complexity | ef891b2a1c14dd3d2b4560da56adb5dd MD5 | raw file
  1. define([
  2. 'jquery',
  3. 'underscore',
  4. 'backbone',
  5. 'collections/todos',
  6. 'views/todos',
  7. 'text!templates/stats.html'
  8. ], function($, _, Backbone, Todos, TodoView, statsTemplate){
  9. var AppView = Backbone.View.extend({
  10. // Instead of generating a new element, bind to the existing skeleton of
  11. // the App already present in the HTML.
  12. el: $("#todoapp"),
  13. // Our template for the line of statistics at the bottom of the app.
  14. statsTemplate: _.template(statsTemplate),
  15. // Delegated events for creating new items, and clearing completed ones.
  16. events: {
  17. "keypress #new-todo": "createOnEnter",
  18. "keyup #new-todo": "showTooltip",
  19. "click .todo-clear a": "clearCompleted"
  20. },
  21. // At initialization we bind to the relevant events on the `Todos`
  22. // collection, when items are added or changed. Kick things off by
  23. // loading any preexisting todos that might be saved in *localStorage*.
  24. initialize: function() {
  25. _.bindAll(this, 'addOne', 'addAll', 'render');
  26. this.input = this.$("#new-todo");
  27. Todos.bind('add', this.addOne);
  28. Todos.bind('reset', this.addAll);
  29. Todos.bind('all', this.render);
  30. Todos.fetch();
  31. },
  32. // Re-rendering the App just means refreshing the statistics -- the rest
  33. // of the app doesn't change.
  34. render: function() {
  35. var done = Todos.done().length;
  36. this.$('#todo-stats').html(this.statsTemplate({
  37. total: Todos.length,
  38. done: Todos.done().length,
  39. remaining: Todos.remaining().length
  40. }));
  41. },
  42. // Add a single todo item to the list by creating a view for it, and
  43. // appending its element to the `<ul>`.
  44. addOne: function(todo) {
  45. var view = new TodoView({model: todo});
  46. this.$("#todo-list").append(view.render().el);
  47. },
  48. // Add all items in the **Todos** collection at once.
  49. addAll: function() {
  50. Todos.each(this.addOne);
  51. },
  52. // Generate the attributes for a new Todo item.
  53. newAttributes: function() {
  54. return {
  55. content: this.input.val(),
  56. order: Todos.nextOrder(),
  57. done: false
  58. };
  59. },
  60. // If you hit return in the main input field, create new **Todo** model,
  61. // persisting it to *localStorage*.
  62. createOnEnter: function(e) {
  63. if (e.keyCode != 13) return;
  64. Todos.create(this.newAttributes());
  65. this.input.val('');
  66. },
  67. // Clear all done todo items, destroying their models.
  68. clearCompleted: function() {
  69. _.each(Todos.done(), function(todo){ todo.clear(); });
  70. return false;
  71. },
  72. // Lazily show the tooltip that tells you to press `enter` to save
  73. // a new todo item, after one second.
  74. showTooltip: function(e) {
  75. var tooltip = this.$(".ui-tooltip-top");
  76. var val = this.input.val();
  77. tooltip.fadeOut();
  78. if (this.tooltipTimeout) clearTimeout(this.tooltipTimeout);
  79. if (val == '' || val == this.input.attr('placeholder')) return;
  80. var show = function(){ tooltip.show().fadeIn(); };
  81. this.tooltipTimeout = _.delay(show, 1000);
  82. }
  83. });
  84. return AppView;
  85. });