PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/backbone-tutorial.html

https://github.com/elparkino/Backbone-tutorial
HTML | 227 lines | 196 code | 18 blank | 13 comment | 0 complexity | c10cdf98b2acb45c145b0e12f5319e6a MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>To-do App in Backbone.js</title>
  7. <!-- ========= -->
  8. <!-- CSS -->
  9. <!-- ========= -->
  10. <style type="text/css">
  11. #todoapp ul {
  12. list-style-type: none; /* Hides bullet points from todo list */
  13. }
  14. #todo-list input.edit {
  15. display: none; /* Hides input box*/
  16. }
  17. #todo-list .editing label {
  18. display: none; /* Hides label text when .editing*/
  19. }
  20. #todo-list .editing input.edit {
  21. display: inline; /* Shows input text box when .editing*/
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <!-- ========= -->
  27. <!-- Your HTML -->
  28. <!-- ========= -->
  29. <section id="todoapp">
  30. <header id="header">
  31. <h1>Todos</h1>
  32. <input id="new-todo" placeholder="What needs to be done?" autofocus>
  33. <div>
  34. <a href="#/">show all</a> |
  35. <a href="#/pending">show pending</a> |
  36. <a href="#/completed">show completed</a>
  37. </div>
  38. </header>
  39. <section id="main">
  40. <ul id="todo-list"></ul>
  41. </section>
  42. </section>
  43. <div>
  44. <p>Find the tutorial and code in <a href="http://adrianmejia.com/blog/2012/09/11/backbone-dot-js-for-absolute-beginners-getting-started/">here</a></p>
  45. </div>
  46. <!-- Templates -->
  47. <script type="text/template" id="item-template">
  48. <div class="view">
  49. <input class="toggle" type="checkbox" <%= completed ? 'checked' : '' %>>
  50. <label><%- title %></label>
  51. <input class="edit" value="<%- title %>">
  52. <button class="destroy">remove</button>
  53. </div>
  54. </script>
  55. <!-- ========= -->
  56. <!-- Libraries -->
  57. <!-- ========= -->
  58. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
  59. <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
  60. <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
  61. <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js" type="text/javascript"></script>
  62. <!-- =============== -->
  63. <!-- Javascript code -->
  64. <!-- =============== -->
  65. <script type="text/javascript">
  66. 'use strict';
  67. var app = {}; // create namespace for our app
  68. //--------------
  69. // Models
  70. //--------------
  71. app.Todo = Backbone.Model.extend({
  72. defaults: {
  73. title: '',
  74. completed: false
  75. },
  76. toggle: function(){
  77. this.save({ completed: !this.get('completed')});
  78. }
  79. });
  80. //--------------
  81. // Collections
  82. //--------------
  83. app.TodoList = Backbone.Collection.extend({
  84. model: app.Todo,
  85. localStorage: new Store("backbone-todo"),
  86. completed: function() {
  87. return this.filter(function( todo ) {
  88. return todo.get('completed');
  89. });
  90. },
  91. remaining: function() {
  92. return this.without.apply( this, this.completed() );
  93. }
  94. });
  95. // instance of the Collection
  96. app.todoList = new app.TodoList();
  97. //--------------
  98. // Views
  99. //--------------
  100. // renders individual todo items list (li)
  101. app.TodoView = Backbone.View.extend({
  102. tagName: 'li',
  103. template: _.template($('#item-template').html()),
  104. render: function(){
  105. this.$el.html(this.template(this.model.toJSON()));
  106. this.input = this.$('.edit');
  107. return this; // enable chained calls
  108. },
  109. initialize: function(){
  110. this.model.on('change', this.render, this);
  111. this.model.on('destroy', this.remove, this); // remove: Convenience Backbone's function for removing the view from the DOM.
  112. },
  113. events: {
  114. 'dblclick label' : 'edit',
  115. 'keypress .edit' : 'updateOnEnter',
  116. 'blur .edit' : 'close',
  117. 'click .toggle': 'toggleCompleted',
  118. 'click .destroy': 'destroy'
  119. },
  120. edit: function(){
  121. this.$el.addClass('editing');
  122. this.input.focus();
  123. },
  124. close: function(){
  125. var value = this.input.val().trim();
  126. if(value) {
  127. this.model.save({title: value});
  128. }
  129. this.$el.removeClass('editing');
  130. },
  131. updateOnEnter: function(e){
  132. if(e.which == 13){
  133. this.close();
  134. }
  135. },
  136. toggleCompleted: function(){
  137. this.model.toggle();
  138. },
  139. destroy: function(){
  140. this.model.destroy();
  141. }
  142. });
  143. // renders the full list of todo items calling TodoView for each one.
  144. app.AppView = Backbone.View.extend({
  145. el: '#todoapp',
  146. initialize: function () {
  147. this.input = this.$('#new-todo');
  148. app.todoList.on('add', this.addAll, this);
  149. app.todoList.on('reset', this.addAll, this);
  150. app.todoList.fetch(); // Loads list from local storage
  151. },
  152. events: {
  153. 'keypress #new-todo': 'createTodoOnEnter'
  154. },
  155. createTodoOnEnter: function(e){
  156. if ( e.which !== 13 || !this.input.val().trim() ) { // ENTER_KEY = 13
  157. return;
  158. }
  159. app.todoList.create(this.newAttributes());
  160. this.input.val(''); // clean input box
  161. },
  162. addOne: function(todo){
  163. var view = new app.TodoView({model: todo});
  164. $('#todo-list').append(view.render().el);
  165. },
  166. addAll: function(){
  167. this.$('#todo-list').html(''); // clean the todo list
  168. // filter todo item list
  169. switch(window.filter){
  170. case 'pending':
  171. _.each(app.todoList.remaining(), this.addOne);
  172. break;
  173. case 'completed':
  174. _.each(app.todoList.completed(), this.addOne);
  175. break;
  176. default:
  177. app.todoList.each(this.addOne, this);
  178. break;
  179. }
  180. },
  181. newAttributes: function(){
  182. return {
  183. title: this.input.val().trim(),
  184. completed: false
  185. }
  186. }
  187. });
  188. //--------------
  189. // Routers
  190. //--------------
  191. app.Router = Backbone.Router.extend({
  192. routes: {
  193. '*filter' : 'setFilter'
  194. },
  195. setFilter: function(params) {
  196. console.log('app.router.params = ' + params);
  197. window.filter = params.trim() || '';
  198. app.todoList.trigger('reset');
  199. }
  200. });
  201. //--------------
  202. // Initializers
  203. //--------------
  204. app.router = new app.Router();
  205. Backbone.history.start();
  206. app.appView = new app.AppView();
  207. </script>
  208. </body>
  209. </html>