| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>todos.js</title> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 7 |
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> |
| 8 |
<link rel="stylesheet" media="all" href="../../docco.css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div id="container"> |
| 12 |
<div id="background"></div> |
| 13 |
|
| 14 |
<ul id="jump_to"> |
| 15 |
<li> |
| 16 |
<a class="large" href="javascript:void(0);">Jump To …</a> |
| 17 |
<a class="small" href="javascript:void(0);">+</a> |
| 18 |
<div id="jump_wrapper"> |
| 19 |
<div id="jump_page_wrapper"> |
| 20 |
<div id="jump_page"> |
| 21 |
|
| 22 |
|
| 23 |
<a class="source" href="../backbone.localStorage.html"> |
| 24 |
examples/backbone.localStorage.js |
| 25 |
</a> |
| 26 |
|
| 27 |
|
| 28 |
<a class="source" href="todos.html"> |
| 29 |
examples/todos/todos.js |
| 30 |
</a> |
| 31 |
|
| 32 |
</div> |
| 33 |
</div> |
| 34 |
</li> |
| 35 |
</ul> |
| 36 |
|
| 37 |
<ul class="sections"> |
| 38 |
|
| 39 |
<li id="title"> |
| 40 |
<div class="annotation"> |
| 41 |
<h1>todos.js</h1> |
| 42 |
</div> |
| 43 |
</li> |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
<li id="section-1"> |
| 48 |
<div class="annotation"> |
| 49 |
|
| 50 |
<div class="sswrap "> |
| 51 |
<a class="ss" href="#section-1">§</a> |
| 52 |
</div> |
| 53 |
<p>An example Backbone application contributed by |
| 54 |
<a href="http://jgn.me/">Jérôme Gravel-Niquet</a>. This demo uses a simple |
| 55 |
<a href="backbone.localStorage.html">LocalStorage adapter</a> |
| 56 |
to persist Backbone models within your browser.</p> |
| 57 |
|
| 58 |
</div> |
| 59 |
|
| 60 |
</li> |
| 61 |
|
| 62 |
|
| 63 |
<li id="section-2"> |
| 64 |
<div class="annotation"> |
| 65 |
|
| 66 |
<div class="sswrap "> |
| 67 |
<a class="ss" href="#section-2">§</a> |
| 68 |
</div> |
| 69 |
<p>Load the application once the DOM is ready, using <code>jQuery.ready</code>:</p> |
| 70 |
|
| 71 |
</div> |
| 72 |
|
| 73 |
<div class="content"><div class='highlight'><pre>$(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){</pre></div></div> |
| 74 |
|
| 75 |
</li> |
| 76 |
|
| 77 |
|
| 78 |
<li id="section-3"> |
| 79 |
<div class="annotation"> |
| 80 |
|
| 81 |
<div class="sswrap "> |
| 82 |
<a class="ss" href="#section-3">§</a> |
| 83 |
</div> |
| 84 |
<h2 id="todo-model">Todo Model</h2> |
| 85 |
|
| 86 |
</div> |
| 87 |
|
| 88 |
</li> |
| 89 |
|
| 90 |
|
| 91 |
<li id="section-4"> |
| 92 |
<div class="annotation"> |
| 93 |
|
| 94 |
<div class="sswrap "> |
| 95 |
<a class="ss" href="#section-4">§</a> |
| 96 |
</div> |
| 97 |
|
| 98 |
</div> |
| 99 |
|
| 100 |
</li> |
| 101 |
|
| 102 |
|
| 103 |
<li id="section-5"> |
| 104 |
<div class="annotation"> |
| 105 |
|
| 106 |
<div class="sswrap "> |
| 107 |
<a class="ss" href="#section-5">§</a> |
| 108 |
</div> |
| 109 |
<p>Our basic <strong>Todo</strong> model has <code>title</code>, <code>order</code>, and <code>done</code> attributes.</p> |
| 110 |
|
| 111 |
</div> |
| 112 |
|
| 113 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Todo</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Model</span>.<span class="hljs-title function_">extend</span>({</pre></div></div> |
| 114 |
|
| 115 |
</li> |
| 116 |
|
| 117 |
|
| 118 |
<li id="section-6"> |
| 119 |
<div class="annotation"> |
| 120 |
|
| 121 |
<div class="sswrap "> |
| 122 |
<a class="ss" href="#section-6">§</a> |
| 123 |
</div> |
| 124 |
<p>Default attributes for the todo item.</p> |
| 125 |
|
| 126 |
</div> |
| 127 |
|
| 128 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">defaults</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 129 |
<span class="hljs-keyword">return</span> { |
| 130 |
<span class="hljs-attr">title</span>: <span class="hljs-string">"empty todo..."</span>, |
| 131 |
<span class="hljs-attr">order</span>: <span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">nextOrder</span>(), |
| 132 |
<span class="hljs-attr">done</span>: <span class="hljs-literal">false</span> |
| 133 |
}; |
| 134 |
},</pre></div></div> |
| 135 |
|
| 136 |
</li> |
| 137 |
|
| 138 |
|
| 139 |
<li id="section-7"> |
| 140 |
<div class="annotation"> |
| 141 |
|
| 142 |
<div class="sswrap "> |
| 143 |
<a class="ss" href="#section-7">§</a> |
| 144 |
</div> |
| 145 |
<p>Toggle the <code>done</code> state of this todo item.</p> |
| 146 |
|
| 147 |
</div> |
| 148 |
|
| 149 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">toggle</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 150 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">save</span>({<span class="hljs-attr">done</span>: !<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">"done"</span>)}); |
| 151 |
} |
| 152 |
|
| 153 |
});</pre></div></div> |
| 154 |
|
| 155 |
</li> |
| 156 |
|
| 157 |
|
| 158 |
<li id="section-8"> |
| 159 |
<div class="annotation"> |
| 160 |
|
| 161 |
<div class="sswrap "> |
| 162 |
<a class="ss" href="#section-8">§</a> |
| 163 |
</div> |
| 164 |
<h2 id="todo-collection">Todo Collection</h2> |
| 165 |
|
| 166 |
</div> |
| 167 |
|
| 168 |
</li> |
| 169 |
|
| 170 |
|
| 171 |
<li id="section-9"> |
| 172 |
<div class="annotation"> |
| 173 |
|
| 174 |
<div class="sswrap "> |
| 175 |
<a class="ss" href="#section-9">§</a> |
| 176 |
</div> |
| 177 |
|
| 178 |
</div> |
| 179 |
|
| 180 |
</li> |
| 181 |
|
| 182 |
|
| 183 |
<li id="section-10"> |
| 184 |
<div class="annotation"> |
| 185 |
|
| 186 |
<div class="sswrap "> |
| 187 |
<a class="ss" href="#section-10">§</a> |
| 188 |
</div> |
| 189 |
<p>The collection of todos is backed by <em>localStorage</em> instead of a remote |
| 190 |
server.</p> |
| 191 |
|
| 192 |
</div> |
| 193 |
|
| 194 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">TodoList</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Collection</span>.<span class="hljs-title function_">extend</span>({</pre></div></div> |
| 195 |
|
| 196 |
</li> |
| 197 |
|
| 198 |
|
| 199 |
<li id="section-11"> |
| 200 |
<div class="annotation"> |
| 201 |
|
| 202 |
<div class="sswrap "> |
| 203 |
<a class="ss" href="#section-11">§</a> |
| 204 |
</div> |
| 205 |
<p>Reference to this collection’s model.</p> |
| 206 |
|
| 207 |
</div> |
| 208 |
|
| 209 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">model</span>: <span class="hljs-title class_">Todo</span>,</pre></div></div> |
| 210 |
|
| 211 |
</li> |
| 212 |
|
| 213 |
|
| 214 |
<li id="section-12"> |
| 215 |
<div class="annotation"> |
| 216 |
|
| 217 |
<div class="sswrap "> |
| 218 |
<a class="ss" href="#section-12">§</a> |
| 219 |
</div> |
| 220 |
<p>Save all of the todo items under the <code>"todos-backbone"</code> namespace.</p> |
| 221 |
|
| 222 |
</div> |
| 223 |
|
| 224 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">localStorage</span>: <span class="hljs-keyword">new</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-title class_">LocalStorage</span>(<span class="hljs-string">"todos-backbone"</span>),</pre></div></div> |
| 225 |
|
| 226 |
</li> |
| 227 |
|
| 228 |
|
| 229 |
<li id="section-13"> |
| 230 |
<div class="annotation"> |
| 231 |
|
| 232 |
<div class="sswrap "> |
| 233 |
<a class="ss" href="#section-13">§</a> |
| 234 |
</div> |
| 235 |
<p>Filter down the list of all todo items that are finished.</p> |
| 236 |
|
| 237 |
</div> |
| 238 |
|
| 239 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">done</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 240 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">where</span>({<span class="hljs-attr">done</span>: <span class="hljs-literal">true</span>}); |
| 241 |
},</pre></div></div> |
| 242 |
|
| 243 |
</li> |
| 244 |
|
| 245 |
|
| 246 |
<li id="section-14"> |
| 247 |
<div class="annotation"> |
| 248 |
|
| 249 |
<div class="sswrap "> |
| 250 |
<a class="ss" href="#section-14">§</a> |
| 251 |
</div> |
| 252 |
<p>Filter down the list to only todo items that are still not finished.</p> |
| 253 |
|
| 254 |
</div> |
| 255 |
|
| 256 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">remaining</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 257 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">where</span>({<span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>}); |
| 258 |
},</pre></div></div> |
| 259 |
|
| 260 |
</li> |
| 261 |
|
| 262 |
|
| 263 |
<li id="section-15"> |
| 264 |
<div class="annotation"> |
| 265 |
|
| 266 |
<div class="sswrap "> |
| 267 |
<a class="ss" href="#section-15">§</a> |
| 268 |
</div> |
| 269 |
<p>We keep the Todos in sequential order, despite being saved by unordered |
| 270 |
GUID in the database. This generates the next order number for new items.</p> |
| 271 |
|
| 272 |
</div> |
| 273 |
|
| 274 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">nextOrder</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 275 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>; |
| 276 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">last</span>().<span class="hljs-title function_">get</span>(<span class="hljs-string">'order'</span>) + <span class="hljs-number">1</span>; |
| 277 |
},</pre></div></div> |
| 278 |
|
| 279 |
</li> |
| 280 |
|
| 281 |
|
| 282 |
<li id="section-16"> |
| 283 |
<div class="annotation"> |
| 284 |
|
| 285 |
<div class="sswrap "> |
| 286 |
<a class="ss" href="#section-16">§</a> |
| 287 |
</div> |
| 288 |
<p>Todos are sorted by their original insertion order.</p> |
| 289 |
|
| 290 |
</div> |
| 291 |
|
| 292 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">comparator</span>: <span class="hljs-string">'order'</span> |
| 293 |
|
| 294 |
});</pre></div></div> |
| 295 |
|
| 296 |
</li> |
| 297 |
|
| 298 |
|
| 299 |
<li id="section-17"> |
| 300 |
<div class="annotation"> |
| 301 |
|
| 302 |
<div class="sswrap "> |
| 303 |
<a class="ss" href="#section-17">§</a> |
| 304 |
</div> |
| 305 |
<p>Create our global collection of <strong>Todos</strong>.</p> |
| 306 |
|
| 307 |
</div> |
| 308 |
|
| 309 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Todos</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TodoList</span>;</pre></div></div> |
| 310 |
|
| 311 |
</li> |
| 312 |
|
| 313 |
|
| 314 |
<li id="section-18"> |
| 315 |
<div class="annotation"> |
| 316 |
|
| 317 |
<div class="sswrap "> |
| 318 |
<a class="ss" href="#section-18">§</a> |
| 319 |
</div> |
| 320 |
<h2 id="todo-item-view">Todo Item View</h2> |
| 321 |
|
| 322 |
</div> |
| 323 |
|
| 324 |
</li> |
| 325 |
|
| 326 |
|
| 327 |
<li id="section-19"> |
| 328 |
<div class="annotation"> |
| 329 |
|
| 330 |
<div class="sswrap "> |
| 331 |
<a class="ss" href="#section-19">§</a> |
| 332 |
</div> |
| 333 |
|
| 334 |
</div> |
| 335 |
|
| 336 |
</li> |
| 337 |
|
| 338 |
|
| 339 |
<li id="section-20"> |
| 340 |
<div class="annotation"> |
| 341 |
|
| 342 |
<div class="sswrap "> |
| 343 |
<a class="ss" href="#section-20">§</a> |
| 344 |
</div> |
| 345 |
<p>The DOM element for a todo item…</p> |
| 346 |
|
| 347 |
</div> |
| 348 |
|
| 349 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">TodoView</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">View</span>.<span class="hljs-title function_">extend</span>({</pre></div></div> |
| 350 |
|
| 351 |
</li> |
| 352 |
|
| 353 |
|
| 354 |
<li id="section-21"> |
| 355 |
<div class="annotation"> |
| 356 |
|
| 357 |
<div class="sswrap "> |
| 358 |
<a class="ss" href="#section-21">§</a> |
| 359 |
</div> |
| 360 |
<p>… is a list tag.</p> |
| 361 |
|
| 362 |
</div> |
| 363 |
|
| 364 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">tagName</span>: <span class="hljs-string">"li"</span>,</pre></div></div> |
| 365 |
|
| 366 |
</li> |
| 367 |
|
| 368 |
|
| 369 |
<li id="section-22"> |
| 370 |
<div class="annotation"> |
| 371 |
|
| 372 |
<div class="sswrap "> |
| 373 |
<a class="ss" href="#section-22">§</a> |
| 374 |
</div> |
| 375 |
<p>Cache the template function for a single item.</p> |
| 376 |
|
| 377 |
</div> |
| 378 |
|
| 379 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">template</span>: _.<span class="hljs-title function_">template</span>($(<span class="hljs-string">'#item-template'</span>).<span class="hljs-title function_">html</span>()),</pre></div></div> |
| 380 |
|
| 381 |
</li> |
| 382 |
|
| 383 |
|
| 384 |
<li id="section-23"> |
| 385 |
<div class="annotation"> |
| 386 |
|
| 387 |
<div class="sswrap "> |
| 388 |
<a class="ss" href="#section-23">§</a> |
| 389 |
</div> |
| 390 |
<p>The DOM events specific to an item.</p> |
| 391 |
|
| 392 |
</div> |
| 393 |
|
| 394 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">events</span>: { |
| 395 |
<span class="hljs-string">"click .toggle"</span> : <span class="hljs-string">"toggleDone"</span>, |
| 396 |
<span class="hljs-string">"dblclick .view"</span> : <span class="hljs-string">"edit"</span>, |
| 397 |
<span class="hljs-string">"click a.destroy"</span> : <span class="hljs-string">"clear"</span>, |
| 398 |
<span class="hljs-string">"keypress .edit"</span> : <span class="hljs-string">"updateOnEnter"</span>, |
| 399 |
<span class="hljs-string">"blur .edit"</span> : <span class="hljs-string">"close"</span> |
| 400 |
},</pre></div></div> |
| 401 |
|
| 402 |
</li> |
| 403 |
|
| 404 |
|
| 405 |
<li id="section-24"> |
| 406 |
<div class="annotation"> |
| 407 |
|
| 408 |
<div class="sswrap "> |
| 409 |
<a class="ss" href="#section-24">§</a> |
| 410 |
</div> |
| 411 |
<p>The TodoView listens for changes to its model, re-rendering. Since there’s |
| 412 |
a one-to-one correspondence between a <strong>Todo</strong> and a <strong>TodoView</strong> in this |
| 413 |
app, we set a direct reference on the model for convenience.</p> |
| 414 |
|
| 415 |
</div> |
| 416 |
|
| 417 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">initialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 418 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listenTo</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>, <span class="hljs-string">'change'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">render</span>); |
| 419 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listenTo</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>, <span class="hljs-string">'destroy'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">remove</span>); |
| 420 |
},</pre></div></div> |
| 421 |
|
| 422 |
</li> |
| 423 |
|
| 424 |
|
| 425 |
<li id="section-25"> |
| 426 |
<div class="annotation"> |
| 427 |
|
| 428 |
<div class="sswrap "> |
| 429 |
<a class="ss" href="#section-25">§</a> |
| 430 |
</div> |
| 431 |
<p>Re-render the titles of the todo item.</p> |
| 432 |
|
| 433 |
</div> |
| 434 |
|
| 435 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">render</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 436 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">html</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">template</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-title function_">toJSON</span>())); |
| 437 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">toggleClass</span>(<span class="hljs-string">'done'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">'done'</span>)); |
| 438 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span> = <span class="hljs-variable language_">this</span>.$(<span class="hljs-string">'.edit'</span>); |
| 439 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 440 |
},</pre></div></div> |
| 441 |
|
| 442 |
</li> |
| 443 |
|
| 444 |
|
| 445 |
<li id="section-26"> |
| 446 |
<div class="annotation"> |
| 447 |
|
| 448 |
<div class="sswrap "> |
| 449 |
<a class="ss" href="#section-26">§</a> |
| 450 |
</div> |
| 451 |
<p>Toggle the <code>"done"</code> state of the model.</p> |
| 452 |
|
| 453 |
</div> |
| 454 |
|
| 455 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">toggleDone</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 456 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-title function_">toggle</span>(); |
| 457 |
},</pre></div></div> |
| 458 |
|
| 459 |
</li> |
| 460 |
|
| 461 |
|
| 462 |
<li id="section-27"> |
| 463 |
<div class="annotation"> |
| 464 |
|
| 465 |
<div class="sswrap "> |
| 466 |
<a class="ss" href="#section-27">§</a> |
| 467 |
</div> |
| 468 |
<p>Switch this view into <code>"editing"</code> mode, displaying the input field.</p> |
| 469 |
|
| 470 |
</div> |
| 471 |
|
| 472 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">edit</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 473 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">addClass</span>(<span class="hljs-string">"editing"</span>); |
| 474 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span>.<span class="hljs-title function_">focus</span>(); |
| 475 |
},</pre></div></div> |
| 476 |
|
| 477 |
</li> |
| 478 |
|
| 479 |
|
| 480 |
<li id="section-28"> |
| 481 |
<div class="annotation"> |
| 482 |
|
| 483 |
<div class="sswrap "> |
| 484 |
<a class="ss" href="#section-28">§</a> |
| 485 |
</div> |
| 486 |
<p>Close the <code>"editing"</code> mode, saving changes to the todo.</p> |
| 487 |
|
| 488 |
</div> |
| 489 |
|
| 490 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">close</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 491 |
<span class="hljs-keyword">var</span> value = <span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span>.<span class="hljs-title function_">val</span>(); |
| 492 |
<span class="hljs-keyword">if</span> (!value) { |
| 493 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clear</span>(); |
| 494 |
} <span class="hljs-keyword">else</span> { |
| 495 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-title function_">save</span>({<span class="hljs-attr">title</span>: value}); |
| 496 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">removeClass</span>(<span class="hljs-string">"editing"</span>); |
| 497 |
} |
| 498 |
},</pre></div></div> |
| 499 |
|
| 500 |
</li> |
| 501 |
|
| 502 |
|
| 503 |
<li id="section-29"> |
| 504 |
<div class="annotation"> |
| 505 |
|
| 506 |
<div class="sswrap "> |
| 507 |
<a class="ss" href="#section-29">§</a> |
| 508 |
</div> |
| 509 |
<p>If you hit <code>enter</code>, we’re through editing the item.</p> |
| 510 |
|
| 511 |
</div> |
| 512 |
|
| 513 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">updateOnEnter</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) { |
| 514 |
<span class="hljs-keyword">if</span> (e.<span class="hljs-property">keyCode</span> == <span class="hljs-number">13</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(); |
| 515 |
},</pre></div></div> |
| 516 |
|
| 517 |
</li> |
| 518 |
|
| 519 |
|
| 520 |
<li id="section-30"> |
| 521 |
<div class="annotation"> |
| 522 |
|
| 523 |
<div class="sswrap "> |
| 524 |
<a class="ss" href="#section-30">§</a> |
| 525 |
</div> |
| 526 |
<p>Remove the item, destroy the model.</p> |
| 527 |
|
| 528 |
</div> |
| 529 |
|
| 530 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">clear</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 531 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-title function_">destroy</span>(); |
| 532 |
} |
| 533 |
|
| 534 |
});</pre></div></div> |
| 535 |
|
| 536 |
</li> |
| 537 |
|
| 538 |
|
| 539 |
<li id="section-31"> |
| 540 |
<div class="annotation"> |
| 541 |
|
| 542 |
<div class="sswrap "> |
| 543 |
<a class="ss" href="#section-31">§</a> |
| 544 |
</div> |
| 545 |
<h2 id="the-application">The Application</h2> |
| 546 |
|
| 547 |
</div> |
| 548 |
|
| 549 |
</li> |
| 550 |
|
| 551 |
|
| 552 |
<li id="section-32"> |
| 553 |
<div class="annotation"> |
| 554 |
|
| 555 |
<div class="sswrap "> |
| 556 |
<a class="ss" href="#section-32">§</a> |
| 557 |
</div> |
| 558 |
|
| 559 |
</div> |
| 560 |
|
| 561 |
</li> |
| 562 |
|
| 563 |
|
| 564 |
<li id="section-33"> |
| 565 |
<div class="annotation"> |
| 566 |
|
| 567 |
<div class="sswrap "> |
| 568 |
<a class="ss" href="#section-33">§</a> |
| 569 |
</div> |
| 570 |
<p>Our overall <strong>AppView</strong> is the top-level piece of UI.</p> |
| 571 |
|
| 572 |
</div> |
| 573 |
|
| 574 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">AppView</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">View</span>.<span class="hljs-title function_">extend</span>({</pre></div></div> |
| 575 |
|
| 576 |
</li> |
| 577 |
|
| 578 |
|
| 579 |
<li id="section-34"> |
| 580 |
<div class="annotation"> |
| 581 |
|
| 582 |
<div class="sswrap "> |
| 583 |
<a class="ss" href="#section-34">§</a> |
| 584 |
</div> |
| 585 |
<p>Instead of generating a new element, bind to the existing skeleton of |
| 586 |
the App already present in the HTML.</p> |
| 587 |
|
| 588 |
</div> |
| 589 |
|
| 590 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">el</span>: $(<span class="hljs-string">"#todoapp"</span>),</pre></div></div> |
| 591 |
|
| 592 |
</li> |
| 593 |
|
| 594 |
|
| 595 |
<li id="section-35"> |
| 596 |
<div class="annotation"> |
| 597 |
|
| 598 |
<div class="sswrap "> |
| 599 |
<a class="ss" href="#section-35">§</a> |
| 600 |
</div> |
| 601 |
<p>Our template for the line of statistics at the bottom of the app.</p> |
| 602 |
|
| 603 |
</div> |
| 604 |
|
| 605 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">statsTemplate</span>: _.<span class="hljs-title function_">template</span>($(<span class="hljs-string">'#stats-template'</span>).<span class="hljs-title function_">html</span>()),</pre></div></div> |
| 606 |
|
| 607 |
</li> |
| 608 |
|
| 609 |
|
| 610 |
<li id="section-36"> |
| 611 |
<div class="annotation"> |
| 612 |
|
| 613 |
<div class="sswrap "> |
| 614 |
<a class="ss" href="#section-36">§</a> |
| 615 |
</div> |
| 616 |
<p>Delegated events for creating new items, and clearing completed ones.</p> |
| 617 |
|
| 618 |
</div> |
| 619 |
|
| 620 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">events</span>: { |
| 621 |
<span class="hljs-string">"keypress #new-todo"</span>: <span class="hljs-string">"createOnEnter"</span>, |
| 622 |
<span class="hljs-string">"click #clear-completed"</span>: <span class="hljs-string">"clearCompleted"</span>, |
| 623 |
<span class="hljs-string">"click #toggle-all"</span>: <span class="hljs-string">"toggleAllComplete"</span> |
| 624 |
},</pre></div></div> |
| 625 |
|
| 626 |
</li> |
| 627 |
|
| 628 |
|
| 629 |
<li id="section-37"> |
| 630 |
<div class="annotation"> |
| 631 |
|
| 632 |
<div class="sswrap "> |
| 633 |
<a class="ss" href="#section-37">§</a> |
| 634 |
</div> |
| 635 |
<p>At initialization we bind to the relevant events on the <code>Todos</code> |
| 636 |
collection, when items are added or changed. Kick things off by |
| 637 |
loading any preexisting todos that might be saved in <em>localStorage</em>.</p> |
| 638 |
|
| 639 |
</div> |
| 640 |
|
| 641 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">initialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 642 |
|
| 643 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span> = <span class="hljs-variable language_">this</span>.$(<span class="hljs-string">"#new-todo"</span>); |
| 644 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">allCheckbox</span> = <span class="hljs-variable language_">this</span>.$(<span class="hljs-string">"#toggle-all"</span>)[<span class="hljs-number">0</span>]; |
| 645 |
|
| 646 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listenTo</span>(<span class="hljs-title class_">Todos</span>, <span class="hljs-string">'add'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">addOne</span>); |
| 647 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listenTo</span>(<span class="hljs-title class_">Todos</span>, <span class="hljs-string">'reset'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">addAll</span>); |
| 648 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listenTo</span>(<span class="hljs-title class_">Todos</span>, <span class="hljs-string">'all'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">render</span>); |
| 649 |
|
| 650 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">footer</span> = <span class="hljs-variable language_">this</span>.$(<span class="hljs-string">'footer'</span>); |
| 651 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">main</span> = $(<span class="hljs-string">'#main'</span>); |
| 652 |
|
| 653 |
<span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">fetch</span>(); |
| 654 |
},</pre></div></div> |
| 655 |
|
| 656 |
</li> |
| 657 |
|
| 658 |
|
| 659 |
<li id="section-38"> |
| 660 |
<div class="annotation"> |
| 661 |
|
| 662 |
<div class="sswrap "> |
| 663 |
<a class="ss" href="#section-38">§</a> |
| 664 |
</div> |
| 665 |
<p>Re-rendering the App just means refreshing the statistics – the rest |
| 666 |
of the app doesn’t change.</p> |
| 667 |
|
| 668 |
</div> |
| 669 |
|
| 670 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">render</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 671 |
<span class="hljs-keyword">var</span> done = <span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">done</span>().<span class="hljs-property">length</span>; |
| 672 |
<span class="hljs-keyword">var</span> remaining = <span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">remaining</span>().<span class="hljs-property">length</span>; |
| 673 |
|
| 674 |
<span class="hljs-keyword">if</span> (<span class="hljs-title class_">Todos</span>.<span class="hljs-property">length</span>) { |
| 675 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">main</span>.<span class="hljs-title function_">show</span>(); |
| 676 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">footer</span>.<span class="hljs-title function_">show</span>(); |
| 677 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">footer</span>.<span class="hljs-title function_">html</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">statsTemplate</span>({<span class="hljs-attr">done</span>: done, <span class="hljs-attr">remaining</span>: remaining})); |
| 678 |
} <span class="hljs-keyword">else</span> { |
| 679 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">main</span>.<span class="hljs-title function_">hide</span>(); |
| 680 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">footer</span>.<span class="hljs-title function_">hide</span>(); |
| 681 |
} |
| 682 |
|
| 683 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">allCheckbox</span>.<span class="hljs-property">checked</span> = !remaining; |
| 684 |
},</pre></div></div> |
| 685 |
|
| 686 |
</li> |
| 687 |
|
| 688 |
|
| 689 |
<li id="section-39"> |
| 690 |
<div class="annotation"> |
| 691 |
|
| 692 |
<div class="sswrap "> |
| 693 |
<a class="ss" href="#section-39">§</a> |
| 694 |
</div> |
| 695 |
<p>Add a single todo item to the list by creating a view for it, and |
| 696 |
appending its element to the <code><ul></code>.</p> |
| 697 |
|
| 698 |
</div> |
| 699 |
|
| 700 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">addOne</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">todo</span>) { |
| 701 |
<span class="hljs-keyword">var</span> view = <span class="hljs-keyword">new</span> <span class="hljs-title class_">TodoView</span>({<span class="hljs-attr">model</span>: todo}); |
| 702 |
<span class="hljs-variable language_">this</span>.$(<span class="hljs-string">"#todo-list"</span>).<span class="hljs-title function_">append</span>(view.<span class="hljs-title function_">render</span>().<span class="hljs-property">el</span>); |
| 703 |
},</pre></div></div> |
| 704 |
|
| 705 |
</li> |
| 706 |
|
| 707 |
|
| 708 |
<li id="section-40"> |
| 709 |
<div class="annotation"> |
| 710 |
|
| 711 |
<div class="sswrap "> |
| 712 |
<a class="ss" href="#section-40">§</a> |
| 713 |
</div> |
| 714 |
<p>Add all items in the <strong>Todos</strong> collection at once.</p> |
| 715 |
|
| 716 |
</div> |
| 717 |
|
| 718 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">addAll</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 719 |
<span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">each</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">addOne</span>, <span class="hljs-variable language_">this</span>); |
| 720 |
},</pre></div></div> |
| 721 |
|
| 722 |
</li> |
| 723 |
|
| 724 |
|
| 725 |
<li id="section-41"> |
| 726 |
<div class="annotation"> |
| 727 |
|
| 728 |
<div class="sswrap "> |
| 729 |
<a class="ss" href="#section-41">§</a> |
| 730 |
</div> |
| 731 |
<p>If you hit return in the main input field, create new <strong>Todo</strong> model, |
| 732 |
persisting it to <em>localStorage</em>.</p> |
| 733 |
|
| 734 |
</div> |
| 735 |
|
| 736 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">createOnEnter</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) { |
| 737 |
<span class="hljs-keyword">if</span> (e.<span class="hljs-property">keyCode</span> != <span class="hljs-number">13</span>) <span class="hljs-keyword">return</span>; |
| 738 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span>.<span class="hljs-title function_">val</span>()) <span class="hljs-keyword">return</span>; |
| 739 |
|
| 740 |
<span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">create</span>({<span class="hljs-attr">title</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span>.<span class="hljs-title function_">val</span>()}); |
| 741 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">input</span>.<span class="hljs-title function_">val</span>(<span class="hljs-string">''</span>); |
| 742 |
},</pre></div></div> |
| 743 |
|
| 744 |
</li> |
| 745 |
|
| 746 |
|
| 747 |
<li id="section-42"> |
| 748 |
<div class="annotation"> |
| 749 |
|
| 750 |
<div class="sswrap "> |
| 751 |
<a class="ss" href="#section-42">§</a> |
| 752 |
</div> |
| 753 |
<p>Clear all done todo items, destroying their models.</p> |
| 754 |
|
| 755 |
</div> |
| 756 |
|
| 757 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">clearCompleted</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 758 |
_.<span class="hljs-title function_">invoke</span>(<span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">done</span>(), <span class="hljs-string">'destroy'</span>); |
| 759 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 760 |
}, |
| 761 |
|
| 762 |
<span class="hljs-attr">toggleAllComplete</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) { |
| 763 |
<span class="hljs-keyword">var</span> done = <span class="hljs-variable language_">this</span>.<span class="hljs-property">allCheckbox</span>.<span class="hljs-property">checked</span>; |
| 764 |
<span class="hljs-title class_">Todos</span>.<span class="hljs-title function_">each</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params">todo</span>) { todo.<span class="hljs-title function_">save</span>({<span class="hljs-string">'done'</span>: done}); }); |
| 765 |
} |
| 766 |
|
| 767 |
});</pre></div></div> |
| 768 |
|
| 769 |
</li> |
| 770 |
|
| 771 |
|
| 772 |
<li id="section-43"> |
| 773 |
<div class="annotation"> |
| 774 |
|
| 775 |
<div class="sswrap "> |
| 776 |
<a class="ss" href="#section-43">§</a> |
| 777 |
</div> |
| 778 |
<p>Finally, we kick things off by creating the <strong>App</strong>.</p> |
| 779 |
|
| 780 |
</div> |
| 781 |
|
| 782 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">App</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AppView</span>; |
| 783 |
|
| 784 |
});</pre></div></div> |
| 785 |
|
| 786 |
</li> |
| 787 |
|
| 788 |
</ul> |
| 789 |
</div> |
| 790 |
</body> |
| 791 |
</html> |