PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/labs/architecture-examples/backbone.xmpp/js/views/app.js

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