PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/dependency-examples/backbone_require/js/views/app.js

https://github.com/1manStartup/todomvc
JavaScript | 134 lines | 94 code | 24 blank | 16 comment | 4 complexity | 4e399c9de7c3017e176b33a4eb0b91a5 MD5 | raw file
  1. define([
  2. 'jquery',
  3. 'underscore',
  4. 'backbone',
  5. 'collections/todos',
  6. 'views/todos',
  7. 'text!templates/stats.html',
  8. 'common'
  9. ], function( $, _, Backbone, Todos, TodoView, statsTemplate, Common ) {
  10. var AppView = Backbone.View.extend({
  11. // Instead of generating a new element, bind to the existing skeleton of
  12. // the App already present in the HTML.
  13. el: '#todoapp',
  14. // Compile our stats template
  15. template: _.template( statsTemplate ),
  16. // Delegated events for creating new items, and clearing completed ones.
  17. events: {
  18. 'keypress #new-todo': 'createOnEnter',
  19. 'click #clear-completed': 'clearCompleted',
  20. 'click #toggle-all': 'toggleAllComplete'
  21. },
  22. // At initialization we bind to the relevant events on the `Todos`
  23. // collection, when items are added or changed. Kick things off by
  24. // loading any preexisting todos that might be saved in *localStorage*.
  25. initialize: function() {
  26. this.input = this.$('#new-todo');
  27. this.allCheckbox = this.$('#toggle-all')[0];
  28. this.$footer = this.$('#footer');
  29. this.$main = this.$('#main');
  30. Todos.on( 'add', this.addOne, this );
  31. Todos.on( 'reset', this.addAll, this );
  32. Todos.on( 'change:completed', this.filterOne, this );
  33. Todos.on( "filter", this.filterAll, this);
  34. Todos.on( 'all', this.render, this );
  35. Todos.fetch();
  36. },
  37. // Re-rendering the App just means refreshing the statistics -- the rest
  38. // of the app doesn't change.
  39. render: function() {
  40. var completed = Todos.completed().length;
  41. var remaining = Todos.remaining().length;
  42. if ( Todos.length ) {
  43. this.$main.show();
  44. this.$footer.show();
  45. this.$footer.html(this.template({
  46. completed: completed,
  47. remaining: remaining
  48. }));
  49. this.$('#filters li a')
  50. .removeClass('selected')
  51. .filter( '[href="#/' + ( Common.TodoFilter || '' ) + '"]' )
  52. .addClass('selected');
  53. } else {
  54. this.$main.hide();
  55. this.$footer.hide();
  56. }
  57. this.allCheckbox.checked = !remaining;
  58. },
  59. // Add a single todo item to the list by creating a view for it, and
  60. // appending its element to the `<ul>`.
  61. addOne: function( todo ) {
  62. var view = new TodoView({ model: todo });
  63. $('#todo-list').append( view.render().el );
  64. },
  65. // Add all items in the **Todos** collection at once.
  66. addAll: function() {
  67. this.$('#todo-list').html('');
  68. Todos.each(this.addOne, this);
  69. },
  70. filterOne : function (todo) {
  71. todo.trigger("visible");
  72. },
  73. filterAll : function () {
  74. app.Todos.each(this.filterOne, this);
  75. },
  76. // Generate the attributes for a new Todo item.
  77. newAttributes: function() {
  78. return {
  79. title: this.input.val().trim(),
  80. order: Todos.nextOrder(),
  81. completed: false
  82. };
  83. },
  84. // If you hit return in the main input field, create new **Todo** model,
  85. // persisting it to *localStorage*.
  86. createOnEnter: function( e ) {
  87. if ( e.which !== Common.ENTER_KEY || !this.input.val().trim() ) {
  88. return;
  89. }
  90. Todos.create( this.newAttributes() );
  91. this.input.val('');
  92. },
  93. // Clear all completed todo items, destroying their models.
  94. clearCompleted: function() {
  95. _.each( Todos.completed(), function( todo ) {
  96. todo.destroy();
  97. });
  98. return false;
  99. },
  100. toggleAllComplete: function() {
  101. var completed = this.allCheckbox.checked;
  102. Todos.each(function( todo ) {
  103. todo.save({
  104. 'completed': completed
  105. });
  106. });
  107. }
  108. });
  109. return AppView;
  110. });