| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>backbone.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 class="sections"> |
| 15 |
|
| 16 |
<li id="title"> |
| 17 |
<div class="annotation"> |
| 18 |
<h1>backbone.js</h1> |
| 19 |
</div> |
| 20 |
</li> |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
<li id="section-1"> |
| 25 |
<div class="annotation"> |
| 26 |
|
| 27 |
<div class="sswrap "> |
| 28 |
<a class="ss" href="#section-1">§</a> |
| 29 |
</div> |
| 30 |
<pre><code><span class="hljs-title class_">Backbone</span>.<span class="hljs-property">js</span> <span class="hljs-number">1.4</span><span class="hljs-number">.1</span> |
| 31 |
</code></pre> |
| 32 |
|
| 33 |
</div> |
| 34 |
|
| 35 |
</li> |
| 36 |
|
| 37 |
|
| 38 |
<li id="section-2"> |
| 39 |
<div class="annotation"> |
| 40 |
|
| 41 |
<div class="sswrap "> |
| 42 |
<a class="ss" href="#section-2">§</a> |
| 43 |
</div> |
| 44 |
<pre><code>(c) <span class="hljs-number">2010</span>-<span class="hljs-number">2022</span> <span class="hljs-title class_">Jeremy</span> <span class="hljs-title class_">Ashkenas</span> and <span class="hljs-title class_">DocumentCloud</span> |
| 45 |
<span class="hljs-title class_">Backbone</span> may be freely distributed under the <span class="hljs-variable constant_">MIT</span> license. |
| 46 |
<span class="hljs-title class_">For</span> all details and <span class="hljs-attr">documentation</span>: |
| 47 |
<span class="hljs-attr">http</span>:<span class="hljs-comment">//backbonejs.org</span> |
| 48 |
</code></pre> |
| 49 |
|
| 50 |
</div> |
| 51 |
|
| 52 |
<div class="content"><div class='highlight'><pre> |
| 53 |
(<span class="hljs-keyword">function</span>(<span class="hljs-params">factory</span>) {</pre></div></div> |
| 54 |
|
| 55 |
</li> |
| 56 |
|
| 57 |
|
| 58 |
<li id="section-3"> |
| 59 |
<div class="annotation"> |
| 60 |
|
| 61 |
<div class="sswrap "> |
| 62 |
<a class="ss" href="#section-3">§</a> |
| 63 |
</div> |
| 64 |
<p>Establish the root object, <code>window</code> (<code>self</code>) in the browser, or <code>global</code> on the server. |
| 65 |
We use <code>self</code> instead of <code>window</code> for <code>WebWorker</code> support.</p> |
| 66 |
|
| 67 |
</div> |
| 68 |
|
| 69 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> root = <span class="hljs-keyword">typeof</span> self == <span class="hljs-string">'object'</span> && self.<span class="hljs-property">self</span> === self && self || |
| 70 |
<span class="hljs-keyword">typeof</span> <span class="hljs-variable language_">global</span> == <span class="hljs-string">'object'</span> && <span class="hljs-variable language_">global</span>.<span class="hljs-property">global</span> === <span class="hljs-variable language_">global</span> && <span class="hljs-variable language_">global</span>;</pre></div></div> |
| 71 |
|
| 72 |
</li> |
| 73 |
|
| 74 |
|
| 75 |
<li id="section-4"> |
| 76 |
<div class="annotation"> |
| 77 |
|
| 78 |
<div class="sswrap "> |
| 79 |
<a class="ss" href="#section-4">§</a> |
| 80 |
</div> |
| 81 |
<p>Set up Backbone appropriately for the environment. Start with AMD.</p> |
| 82 |
|
| 83 |
</div> |
| 84 |
|
| 85 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> define === <span class="hljs-string">'function'</span> && define.<span class="hljs-property">amd</span>) { |
| 86 |
<span class="hljs-title function_">define</span>([<span class="hljs-string">'underscore'</span>, <span class="hljs-string">'jquery'</span>, <span class="hljs-string">'exports'</span>], <span class="hljs-keyword">function</span>(<span class="hljs-params">_, $, <span class="hljs-built_in">exports</span></span>) {</pre></div></div> |
| 87 |
|
| 88 |
</li> |
| 89 |
|
| 90 |
|
| 91 |
<li id="section-5"> |
| 92 |
<div class="annotation"> |
| 93 |
|
| 94 |
<div class="sswrap "> |
| 95 |
<a class="ss" href="#section-5">§</a> |
| 96 |
</div> |
| 97 |
<p>Export global even in AMD case in case this script is loaded with |
| 98 |
others that may still expect a global Backbone.</p> |
| 99 |
|
| 100 |
</div> |
| 101 |
|
| 102 |
<div class="content"><div class='highlight'><pre> root.<span class="hljs-property">Backbone</span> = <span class="hljs-title function_">factory</span>(root, <span class="hljs-built_in">exports</span>, _, $); |
| 103 |
});</pre></div></div> |
| 104 |
|
| 105 |
</li> |
| 106 |
|
| 107 |
|
| 108 |
<li id="section-6"> |
| 109 |
<div class="annotation"> |
| 110 |
|
| 111 |
<div class="sswrap "> |
| 112 |
<a class="ss" href="#section-6">§</a> |
| 113 |
</div> |
| 114 |
<p>Next for Node.js or CommonJS. jQuery may not be needed as a module.</p> |
| 115 |
|
| 116 |
</div> |
| 117 |
|
| 118 |
<div class="content"><div class='highlight'><pre> } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> <span class="hljs-built_in">exports</span> !== <span class="hljs-string">'undefined'</span>) { |
| 119 |
<span class="hljs-keyword">var</span> _ = <span class="hljs-built_in">require</span>(<span class="hljs-string">'underscore'</span>), $; |
| 120 |
<span class="hljs-keyword">try</span> { $ = <span class="hljs-built_in">require</span>(<span class="hljs-string">'jquery'</span>); } <span class="hljs-keyword">catch</span> (e) {} |
| 121 |
<span class="hljs-title function_">factory</span>(root, <span class="hljs-built_in">exports</span>, _, $);</pre></div></div> |
| 122 |
|
| 123 |
</li> |
| 124 |
|
| 125 |
|
| 126 |
<li id="section-7"> |
| 127 |
<div class="annotation"> |
| 128 |
|
| 129 |
<div class="sswrap "> |
| 130 |
<a class="ss" href="#section-7">§</a> |
| 131 |
</div> |
| 132 |
<p>Finally, as a browser global.</p> |
| 133 |
|
| 134 |
</div> |
| 135 |
|
| 136 |
<div class="content"><div class='highlight'><pre> } <span class="hljs-keyword">else</span> { |
| 137 |
root.<span class="hljs-property">Backbone</span> = <span class="hljs-title function_">factory</span>(root, {}, root.<span class="hljs-property">_</span>, root.<span class="hljs-property">jQuery</span> || root.<span class="hljs-property">Zepto</span> || root.<span class="hljs-property">ender</span> || root.<span class="hljs-property">$</span>); |
| 138 |
} |
| 139 |
|
| 140 |
})(<span class="hljs-keyword">function</span>(<span class="hljs-params">root, Backbone, _, $</span>) {</pre></div></div> |
| 141 |
|
| 142 |
</li> |
| 143 |
|
| 144 |
|
| 145 |
<li id="section-8"> |
| 146 |
<div class="annotation"> |
| 147 |
|
| 148 |
<div class="sswrap "> |
| 149 |
<a class="ss" href="#section-8">§</a> |
| 150 |
</div> |
| 151 |
<h2 id="initial-setup">Initial Setup</h2> |
| 152 |
|
| 153 |
</div> |
| 154 |
|
| 155 |
</li> |
| 156 |
|
| 157 |
|
| 158 |
<li id="section-9"> |
| 159 |
<div class="annotation"> |
| 160 |
|
| 161 |
<div class="sswrap "> |
| 162 |
<a class="ss" href="#section-9">§</a> |
| 163 |
</div> |
| 164 |
|
| 165 |
</div> |
| 166 |
|
| 167 |
</li> |
| 168 |
|
| 169 |
|
| 170 |
<li id="section-10"> |
| 171 |
<div class="annotation"> |
| 172 |
|
| 173 |
<div class="sswrap "> |
| 174 |
<a class="ss" href="#section-10">§</a> |
| 175 |
</div> |
| 176 |
<p>Save the previous value of the <code>Backbone</code> variable, so that it can be |
| 177 |
restored later on, if <code>noConflict</code> is used.</p> |
| 178 |
|
| 179 |
</div> |
| 180 |
|
| 181 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> previousBackbone = root.<span class="hljs-property">Backbone</span>;</pre></div></div> |
| 182 |
|
| 183 |
</li> |
| 184 |
|
| 185 |
|
| 186 |
<li id="section-11"> |
| 187 |
<div class="annotation"> |
| 188 |
|
| 189 |
<div class="sswrap "> |
| 190 |
<a class="ss" href="#section-11">§</a> |
| 191 |
</div> |
| 192 |
<p>Create a local reference to a common array method we’ll want to use later.</p> |
| 193 |
|
| 194 |
</div> |
| 195 |
|
| 196 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> slice = <span class="hljs-title class_">Array</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">slice</span>;</pre></div></div> |
| 197 |
|
| 198 |
</li> |
| 199 |
|
| 200 |
|
| 201 |
<li id="section-12"> |
| 202 |
<div class="annotation"> |
| 203 |
|
| 204 |
<div class="sswrap "> |
| 205 |
<a class="ss" href="#section-12">§</a> |
| 206 |
</div> |
| 207 |
<p>Current version of the library. Keep in sync with <code>package.json</code>.</p> |
| 208 |
|
| 209 |
</div> |
| 210 |
|
| 211 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">VERSION</span> = <span class="hljs-string">'1.4.1'</span>;</pre></div></div> |
| 212 |
|
| 213 |
</li> |
| 214 |
|
| 215 |
|
| 216 |
<li id="section-13"> |
| 217 |
<div class="annotation"> |
| 218 |
|
| 219 |
<div class="sswrap "> |
| 220 |
<a class="ss" href="#section-13">§</a> |
| 221 |
</div> |
| 222 |
<p>For Backbone’s purposes, jQuery, Zepto, Ender, or My Library (kidding) owns |
| 223 |
the <code>$</code> variable.</p> |
| 224 |
|
| 225 |
</div> |
| 226 |
|
| 227 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">$</span> = $;</pre></div></div> |
| 228 |
|
| 229 |
</li> |
| 230 |
|
| 231 |
|
| 232 |
<li id="section-14"> |
| 233 |
<div class="annotation"> |
| 234 |
|
| 235 |
<div class="sswrap "> |
| 236 |
<a class="ss" href="#section-14">§</a> |
| 237 |
</div> |
| 238 |
<p>Runs Backbone.js in <em>noConflict</em> mode, returning the <code>Backbone</code> variable |
| 239 |
to its previous owner. Returns a reference to this Backbone object.</p> |
| 240 |
|
| 241 |
</div> |
| 242 |
|
| 243 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">noConflict</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 244 |
root.<span class="hljs-property">Backbone</span> = previousBackbone; |
| 245 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 246 |
};</pre></div></div> |
| 247 |
|
| 248 |
</li> |
| 249 |
|
| 250 |
|
| 251 |
<li id="section-15"> |
| 252 |
<div class="annotation"> |
| 253 |
|
| 254 |
<div class="sswrap "> |
| 255 |
<a class="ss" href="#section-15">§</a> |
| 256 |
</div> |
| 257 |
<p>Turn on <code>emulateHTTP</code> to support legacy HTTP servers. Setting this option |
| 258 |
will fake <code>"PATCH"</code>, <code>"PUT"</code> and <code>"DELETE"</code> requests via the <code>_method</code> parameter and |
| 259 |
set a <code>X-Http-Method-Override</code> header.</p> |
| 260 |
|
| 261 |
</div> |
| 262 |
|
| 263 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">emulateHTTP</span> = <span class="hljs-literal">false</span>;</pre></div></div> |
| 264 |
|
| 265 |
</li> |
| 266 |
|
| 267 |
|
| 268 |
<li id="section-16"> |
| 269 |
<div class="annotation"> |
| 270 |
|
| 271 |
<div class="sswrap "> |
| 272 |
<a class="ss" href="#section-16">§</a> |
| 273 |
</div> |
| 274 |
<p>Turn on <code>emulateJSON</code> to support legacy servers that can’t deal with direct |
| 275 |
<code>application/json</code> requests … this will encode the body as |
| 276 |
<code>application/x-www-form-urlencoded</code> instead and will send the model in a |
| 277 |
form param named <code>model</code>.</p> |
| 278 |
|
| 279 |
</div> |
| 280 |
|
| 281 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">emulateJSON</span> = <span class="hljs-literal">false</span>;</pre></div></div> |
| 282 |
|
| 283 |
</li> |
| 284 |
|
| 285 |
|
| 286 |
<li id="section-17"> |
| 287 |
<div class="annotation"> |
| 288 |
|
| 289 |
<div class="sswrap "> |
| 290 |
<a class="ss" href="#section-17">§</a> |
| 291 |
</div> |
| 292 |
<h2 id="backboneevents">Backbone.Events</h2> |
| 293 |
|
| 294 |
</div> |
| 295 |
|
| 296 |
</li> |
| 297 |
|
| 298 |
|
| 299 |
<li id="section-18"> |
| 300 |
<div class="annotation"> |
| 301 |
|
| 302 |
<div class="sswrap "> |
| 303 |
<a class="ss" href="#section-18">§</a> |
| 304 |
</div> |
| 305 |
|
| 306 |
</div> |
| 307 |
|
| 308 |
</li> |
| 309 |
|
| 310 |
|
| 311 |
<li id="section-19"> |
| 312 |
<div class="annotation"> |
| 313 |
|
| 314 |
<div class="sswrap "> |
| 315 |
<a class="ss" href="#section-19">§</a> |
| 316 |
</div> |
| 317 |
<p>A module that can be mixed in to <em>any object</em> in order to provide it with |
| 318 |
a custom event channel. You may bind a callback to an event with <code>on</code> or |
| 319 |
remove with <code>off</code>; <code>trigger</code>-ing an event fires all callbacks in |
| 320 |
succession.</p> |
| 321 |
<pre><code><span class="hljs-keyword">var</span> object = {}; |
| 322 |
_.<span class="hljs-title function_">extend</span>(object, <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Events</span>); |
| 323 |
object.<span class="hljs-title function_">on</span>(<span class="hljs-string">'expand'</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){ <span class="hljs-title function_">alert</span>(<span class="hljs-string">'expanded'</span>); }); |
| 324 |
object.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'expand'</span>); |
| 325 |
</code></pre> |
| 326 |
|
| 327 |
</div> |
| 328 |
|
| 329 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Events</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Events</span> = {};</pre></div></div> |
| 330 |
|
| 331 |
</li> |
| 332 |
|
| 333 |
|
| 334 |
<li id="section-20"> |
| 335 |
<div class="annotation"> |
| 336 |
|
| 337 |
<div class="sswrap "> |
| 338 |
<a class="ss" href="#section-20">§</a> |
| 339 |
</div> |
| 340 |
<p>Regular expression used to split event strings.</p> |
| 341 |
|
| 342 |
</div> |
| 343 |
|
| 344 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> eventSplitter = <span class="hljs-regexp">/\s+/</span>;</pre></div></div> |
| 345 |
|
| 346 |
</li> |
| 347 |
|
| 348 |
|
| 349 |
<li id="section-21"> |
| 350 |
<div class="annotation"> |
| 351 |
|
| 352 |
<div class="sswrap "> |
| 353 |
<a class="ss" href="#section-21">§</a> |
| 354 |
</div> |
| 355 |
<p>A private global variable to share between listeners and listenees.</p> |
| 356 |
|
| 357 |
</div> |
| 358 |
|
| 359 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> _listening;</pre></div></div> |
| 360 |
|
| 361 |
</li> |
| 362 |
|
| 363 |
|
| 364 |
<li id="section-22"> |
| 365 |
<div class="annotation"> |
| 366 |
|
| 367 |
<div class="sswrap "> |
| 368 |
<a class="ss" href="#section-22">§</a> |
| 369 |
</div> |
| 370 |
<p>Iterates over the standard <code>event, callback</code> (as well as the fancy multiple |
| 371 |
space-separated events <code>"change blur", callback</code> and jQuery-style event |
| 372 |
maps <code>{event: callback}</code>).</p> |
| 373 |
|
| 374 |
</div> |
| 375 |
|
| 376 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> eventsApi = <span class="hljs-keyword">function</span>(<span class="hljs-params">iteratee, events, name, callback, opts</span>) { |
| 377 |
<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, names; |
| 378 |
<span class="hljs-keyword">if</span> (name && <span class="hljs-keyword">typeof</span> name === <span class="hljs-string">'object'</span>) {</pre></div></div> |
| 379 |
|
| 380 |
</li> |
| 381 |
|
| 382 |
|
| 383 |
<li id="section-23"> |
| 384 |
<div class="annotation"> |
| 385 |
|
| 386 |
<div class="sswrap "> |
| 387 |
<a class="ss" href="#section-23">§</a> |
| 388 |
</div> |
| 389 |
<p>Handle event maps.</p> |
| 390 |
|
| 391 |
</div> |
| 392 |
|
| 393 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (callback !== <span class="hljs-keyword">void</span> <span class="hljs-number">0</span> && <span class="hljs-string">'context'</span> <span class="hljs-keyword">in</span> opts && opts.<span class="hljs-property">context</span> === <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>) opts.<span class="hljs-property">context</span> = callback; |
| 394 |
<span class="hljs-keyword">for</span> (names = _.<span class="hljs-title function_">keys</span>(name); i < names.<span class="hljs-property">length</span> ; i++) { |
| 395 |
events = <span class="hljs-title function_">eventsApi</span>(iteratee, events, names[i], name[names[i]], opts); |
| 396 |
} |
| 397 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (name && eventSplitter.<span class="hljs-title function_">test</span>(name)) {</pre></div></div> |
| 398 |
|
| 399 |
</li> |
| 400 |
|
| 401 |
|
| 402 |
<li id="section-24"> |
| 403 |
<div class="annotation"> |
| 404 |
|
| 405 |
<div class="sswrap "> |
| 406 |
<a class="ss" href="#section-24">§</a> |
| 407 |
</div> |
| 408 |
<p>Handle space-separated event names by delegating them individually.</p> |
| 409 |
|
| 410 |
</div> |
| 411 |
|
| 412 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> (names = name.<span class="hljs-title function_">split</span>(eventSplitter); i < names.<span class="hljs-property">length</span>; i++) { |
| 413 |
events = <span class="hljs-title function_">iteratee</span>(events, names[i], callback, opts); |
| 414 |
} |
| 415 |
} <span class="hljs-keyword">else</span> {</pre></div></div> |
| 416 |
|
| 417 |
</li> |
| 418 |
|
| 419 |
|
| 420 |
<li id="section-25"> |
| 421 |
<div class="annotation"> |
| 422 |
|
| 423 |
<div class="sswrap "> |
| 424 |
<a class="ss" href="#section-25">§</a> |
| 425 |
</div> |
| 426 |
<p>Finally, standard events.</p> |
| 427 |
|
| 428 |
</div> |
| 429 |
|
| 430 |
<div class="content"><div class='highlight'><pre> events = <span class="hljs-title function_">iteratee</span>(events, name, callback, opts); |
| 431 |
} |
| 432 |
<span class="hljs-keyword">return</span> events; |
| 433 |
};</pre></div></div> |
| 434 |
|
| 435 |
</li> |
| 436 |
|
| 437 |
|
| 438 |
<li id="section-26"> |
| 439 |
<div class="annotation"> |
| 440 |
|
| 441 |
<div class="sswrap "> |
| 442 |
<a class="ss" href="#section-26">§</a> |
| 443 |
</div> |
| 444 |
<p>Bind an event to a <code>callback</code> function. Passing <code>"all"</code> will bind |
| 445 |
the callback to all events fired.</p> |
| 446 |
|
| 447 |
</div> |
| 448 |
|
| 449 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">on</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">name, callback, context</span>) { |
| 450 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span> = <span class="hljs-title function_">eventsApi</span>(onApi, <span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span> || {}, name, callback, { |
| 451 |
<span class="hljs-attr">context</span>: context, |
| 452 |
<span class="hljs-attr">ctx</span>: <span class="hljs-variable language_">this</span>, |
| 453 |
<span class="hljs-attr">listening</span>: _listening |
| 454 |
}); |
| 455 |
|
| 456 |
<span class="hljs-keyword">if</span> (_listening) { |
| 457 |
<span class="hljs-keyword">var</span> listeners = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeners</span> || (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeners</span> = {}); |
| 458 |
listeners[_listening.<span class="hljs-property">id</span>] = _listening;</pre></div></div> |
| 459 |
|
| 460 |
</li> |
| 461 |
|
| 462 |
|
| 463 |
<li id="section-27"> |
| 464 |
<div class="annotation"> |
| 465 |
|
| 466 |
<div class="sswrap "> |
| 467 |
<a class="ss" href="#section-27">§</a> |
| 468 |
</div> |
| 469 |
<p>Allow the listening to use a counter, instead of tracking |
| 470 |
callbacks for library interop</p> |
| 471 |
|
| 472 |
</div> |
| 473 |
|
| 474 |
<div class="content"><div class='highlight'><pre> _listening.<span class="hljs-property">interop</span> = <span class="hljs-literal">false</span>; |
| 475 |
} |
| 476 |
|
| 477 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 478 |
};</pre></div></div> |
| 479 |
|
| 480 |
</li> |
| 481 |
|
| 482 |
|
| 483 |
<li id="section-28"> |
| 484 |
<div class="annotation"> |
| 485 |
|
| 486 |
<div class="sswrap "> |
| 487 |
<a class="ss" href="#section-28">§</a> |
| 488 |
</div> |
| 489 |
<p>Inversion-of-control versions of <code>on</code>. Tell <em>this</em> object to listen to |
| 490 |
an event in another object… keeping track of what it’s listening to |
| 491 |
for easier unbinding later.</p> |
| 492 |
|
| 493 |
</div> |
| 494 |
|
| 495 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">listenTo</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">obj, name, callback</span>) { |
| 496 |
<span class="hljs-keyword">if</span> (!obj) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 497 |
<span class="hljs-keyword">var</span> id = obj.<span class="hljs-property">_listenId</span> || (obj.<span class="hljs-property">_listenId</span> = _.<span class="hljs-title function_">uniqueId</span>(<span class="hljs-string">'l'</span>)); |
| 498 |
<span class="hljs-keyword">var</span> listeningTo = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeningTo</span> || (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeningTo</span> = {}); |
| 499 |
<span class="hljs-keyword">var</span> listening = _listening = listeningTo[id];</pre></div></div> |
| 500 |
|
| 501 |
</li> |
| 502 |
|
| 503 |
|
| 504 |
<li id="section-29"> |
| 505 |
<div class="annotation"> |
| 506 |
|
| 507 |
<div class="sswrap "> |
| 508 |
<a class="ss" href="#section-29">§</a> |
| 509 |
</div> |
| 510 |
<p>This object is not listening to any other events on <code>obj</code> yet. |
| 511 |
Setup the necessary references to track the listening callbacks.</p> |
| 512 |
|
| 513 |
</div> |
| 514 |
|
| 515 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!listening) { |
| 516 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_listenId</span> || (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_listenId</span> = _.<span class="hljs-title function_">uniqueId</span>(<span class="hljs-string">'l'</span>)); |
| 517 |
listening = _listening = listeningTo[id] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Listening</span>(<span class="hljs-variable language_">this</span>, obj); |
| 518 |
}</pre></div></div> |
| 519 |
|
| 520 |
</li> |
| 521 |
|
| 522 |
|
| 523 |
<li id="section-30"> |
| 524 |
<div class="annotation"> |
| 525 |
|
| 526 |
<div class="sswrap "> |
| 527 |
<a class="ss" href="#section-30">§</a> |
| 528 |
</div> |
| 529 |
<p>Bind callbacks on obj.</p> |
| 530 |
|
| 531 |
</div> |
| 532 |
|
| 533 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> error = <span class="hljs-title function_">tryCatchOn</span>(obj, name, callback, <span class="hljs-variable language_">this</span>); |
| 534 |
_listening = <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 535 |
|
| 536 |
<span class="hljs-keyword">if</span> (error) <span class="hljs-keyword">throw</span> error;</pre></div></div> |
| 537 |
|
| 538 |
</li> |
| 539 |
|
| 540 |
|
| 541 |
<li id="section-31"> |
| 542 |
<div class="annotation"> |
| 543 |
|
| 544 |
<div class="sswrap "> |
| 545 |
<a class="ss" href="#section-31">§</a> |
| 546 |
</div> |
| 547 |
<p>If the target obj is not Backbone.Events, track events manually.</p> |
| 548 |
|
| 549 |
</div> |
| 550 |
|
| 551 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (listening.<span class="hljs-property">interop</span>) listening.<span class="hljs-title function_">on</span>(name, callback); |
| 552 |
|
| 553 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 554 |
};</pre></div></div> |
| 555 |
|
| 556 |
</li> |
| 557 |
|
| 558 |
|
| 559 |
<li id="section-32"> |
| 560 |
<div class="annotation"> |
| 561 |
|
| 562 |
<div class="sswrap "> |
| 563 |
<a class="ss" href="#section-32">§</a> |
| 564 |
</div> |
| 565 |
<p>The reducing API that adds a callback to the <code>events</code> object.</p> |
| 566 |
|
| 567 |
</div> |
| 568 |
|
| 569 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> onApi = <span class="hljs-keyword">function</span>(<span class="hljs-params">events, name, callback, options</span>) { |
| 570 |
<span class="hljs-keyword">if</span> (callback) { |
| 571 |
<span class="hljs-keyword">var</span> handlers = events[name] || (events[name] = []); |
| 572 |
<span class="hljs-keyword">var</span> context = options.<span class="hljs-property">context</span>, ctx = options.<span class="hljs-property">ctx</span>, listening = options.<span class="hljs-property">listening</span>; |
| 573 |
<span class="hljs-keyword">if</span> (listening) listening.<span class="hljs-property">count</span>++; |
| 574 |
|
| 575 |
handlers.<span class="hljs-title function_">push</span>({<span class="hljs-attr">callback</span>: callback, <span class="hljs-attr">context</span>: context, <span class="hljs-attr">ctx</span>: context || ctx, <span class="hljs-attr">listening</span>: listening}); |
| 576 |
} |
| 577 |
<span class="hljs-keyword">return</span> events; |
| 578 |
};</pre></div></div> |
| 579 |
|
| 580 |
</li> |
| 581 |
|
| 582 |
|
| 583 |
<li id="section-33"> |
| 584 |
<div class="annotation"> |
| 585 |
|
| 586 |
<div class="sswrap "> |
| 587 |
<a class="ss" href="#section-33">§</a> |
| 588 |
</div> |
| 589 |
<p>An try-catch guarded #on function, to prevent poisoning the global |
| 590 |
<code>_listening</code> variable.</p> |
| 591 |
|
| 592 |
</div> |
| 593 |
|
| 594 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> tryCatchOn = <span class="hljs-keyword">function</span>(<span class="hljs-params">obj, name, callback, context</span>) { |
| 595 |
<span class="hljs-keyword">try</span> { |
| 596 |
obj.<span class="hljs-title function_">on</span>(name, callback, context); |
| 597 |
} <span class="hljs-keyword">catch</span> (e) { |
| 598 |
<span class="hljs-keyword">return</span> e; |
| 599 |
} |
| 600 |
};</pre></div></div> |
| 601 |
|
| 602 |
</li> |
| 603 |
|
| 604 |
|
| 605 |
<li id="section-34"> |
| 606 |
<div class="annotation"> |
| 607 |
|
| 608 |
<div class="sswrap "> |
| 609 |
<a class="ss" href="#section-34">§</a> |
| 610 |
</div> |
| 611 |
<p>Remove one or many callbacks. If <code>context</code> is null, removes all |
| 612 |
callbacks with that function. If <code>callback</code> is null, removes all |
| 613 |
callbacks for the event. If <code>name</code> is null, removes all bound |
| 614 |
callbacks for all events.</p> |
| 615 |
|
| 616 |
</div> |
| 617 |
|
| 618 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">off</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">name, callback, context</span>) { |
| 619 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span>) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 620 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span> = <span class="hljs-title function_">eventsApi</span>(offApi, <span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span>, name, callback, { |
| 621 |
<span class="hljs-attr">context</span>: context, |
| 622 |
<span class="hljs-attr">listeners</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeners</span> |
| 623 |
}); |
| 624 |
|
| 625 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 626 |
};</pre></div></div> |
| 627 |
|
| 628 |
</li> |
| 629 |
|
| 630 |
|
| 631 |
<li id="section-35"> |
| 632 |
<div class="annotation"> |
| 633 |
|
| 634 |
<div class="sswrap "> |
| 635 |
<a class="ss" href="#section-35">§</a> |
| 636 |
</div> |
| 637 |
<p>Tell this object to stop listening to either specific events … or |
| 638 |
to every object it’s currently listening to.</p> |
| 639 |
|
| 640 |
</div> |
| 641 |
|
| 642 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">stopListening</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">obj, name, callback</span>) { |
| 643 |
<span class="hljs-keyword">var</span> listeningTo = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeningTo</span>; |
| 644 |
<span class="hljs-keyword">if</span> (!listeningTo) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 645 |
|
| 646 |
<span class="hljs-keyword">var</span> ids = obj ? [obj.<span class="hljs-property">_listenId</span>] : _.<span class="hljs-title function_">keys</span>(listeningTo); |
| 647 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < ids.<span class="hljs-property">length</span>; i++) { |
| 648 |
<span class="hljs-keyword">var</span> listening = listeningTo[ids[i]];</pre></div></div> |
| 649 |
|
| 650 |
</li> |
| 651 |
|
| 652 |
|
| 653 |
<li id="section-36"> |
| 654 |
<div class="annotation"> |
| 655 |
|
| 656 |
<div class="sswrap "> |
| 657 |
<a class="ss" href="#section-36">§</a> |
| 658 |
</div> |
| 659 |
<p>If listening doesn’t exist, this object is not currently |
| 660 |
listening to obj. Break out early.</p> |
| 661 |
|
| 662 |
</div> |
| 663 |
|
| 664 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!listening) <span class="hljs-keyword">break</span>; |
| 665 |
|
| 666 |
listening.<span class="hljs-property">obj</span>.<span class="hljs-title function_">off</span>(name, callback, <span class="hljs-variable language_">this</span>); |
| 667 |
<span class="hljs-keyword">if</span> (listening.<span class="hljs-property">interop</span>) listening.<span class="hljs-title function_">off</span>(name, callback); |
| 668 |
} |
| 669 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isEmpty</span>(listeningTo)) <span class="hljs-variable language_">this</span>.<span class="hljs-property">_listeningTo</span> = <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 670 |
|
| 671 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 672 |
};</pre></div></div> |
| 673 |
|
| 674 |
</li> |
| 675 |
|
| 676 |
|
| 677 |
<li id="section-37"> |
| 678 |
<div class="annotation"> |
| 679 |
|
| 680 |
<div class="sswrap "> |
| 681 |
<a class="ss" href="#section-37">§</a> |
| 682 |
</div> |
| 683 |
<p>The reducing API that removes a callback from the <code>events</code> object.</p> |
| 684 |
|
| 685 |
</div> |
| 686 |
|
| 687 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> offApi = <span class="hljs-keyword">function</span>(<span class="hljs-params">events, name, callback, options</span>) { |
| 688 |
<span class="hljs-keyword">if</span> (!events) <span class="hljs-keyword">return</span>; |
| 689 |
|
| 690 |
<span class="hljs-keyword">var</span> context = options.<span class="hljs-property">context</span>, listeners = options.<span class="hljs-property">listeners</span>; |
| 691 |
<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>, names;</pre></div></div> |
| 692 |
|
| 693 |
</li> |
| 694 |
|
| 695 |
|
| 696 |
<li id="section-38"> |
| 697 |
<div class="annotation"> |
| 698 |
|
| 699 |
<div class="sswrap "> |
| 700 |
<a class="ss" href="#section-38">§</a> |
| 701 |
</div> |
| 702 |
<p>Delete all event listeners and “drop” events.</p> |
| 703 |
|
| 704 |
</div> |
| 705 |
|
| 706 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!name && !context && !callback) { |
| 707 |
<span class="hljs-keyword">for</span> (names = _.<span class="hljs-title function_">keys</span>(listeners); i < names.<span class="hljs-property">length</span>; i++) { |
| 708 |
listeners[names[i]].<span class="hljs-title function_">cleanup</span>(); |
| 709 |
} |
| 710 |
<span class="hljs-keyword">return</span>; |
| 711 |
} |
| 712 |
|
| 713 |
names = name ? [name] : _.<span class="hljs-title function_">keys</span>(events); |
| 714 |
<span class="hljs-keyword">for</span> (; i < names.<span class="hljs-property">length</span>; i++) { |
| 715 |
name = names[i]; |
| 716 |
<span class="hljs-keyword">var</span> handlers = events[name];</pre></div></div> |
| 717 |
|
| 718 |
</li> |
| 719 |
|
| 720 |
|
| 721 |
<li id="section-39"> |
| 722 |
<div class="annotation"> |
| 723 |
|
| 724 |
<div class="sswrap "> |
| 725 |
<a class="ss" href="#section-39">§</a> |
| 726 |
</div> |
| 727 |
<p>Bail out if there are no events stored.</p> |
| 728 |
|
| 729 |
</div> |
| 730 |
|
| 731 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!handlers) <span class="hljs-keyword">break</span>;</pre></div></div> |
| 732 |
|
| 733 |
</li> |
| 734 |
|
| 735 |
|
| 736 |
<li id="section-40"> |
| 737 |
<div class="annotation"> |
| 738 |
|
| 739 |
<div class="sswrap "> |
| 740 |
<a class="ss" href="#section-40">§</a> |
| 741 |
</div> |
| 742 |
<p>Find any remaining events.</p> |
| 743 |
|
| 744 |
</div> |
| 745 |
|
| 746 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> remaining = []; |
| 747 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> j = <span class="hljs-number">0</span>; j < handlers.<span class="hljs-property">length</span>; j++) { |
| 748 |
<span class="hljs-keyword">var</span> handler = handlers[j]; |
| 749 |
<span class="hljs-keyword">if</span> ( |
| 750 |
callback && callback !== handler.<span class="hljs-property">callback</span> && |
| 751 |
callback !== handler.<span class="hljs-property">callback</span>.<span class="hljs-property">_callback</span> || |
| 752 |
context && context !== handler.<span class="hljs-property">context</span> |
| 753 |
) { |
| 754 |
remaining.<span class="hljs-title function_">push</span>(handler); |
| 755 |
} <span class="hljs-keyword">else</span> { |
| 756 |
<span class="hljs-keyword">var</span> listening = handler.<span class="hljs-property">listening</span>; |
| 757 |
<span class="hljs-keyword">if</span> (listening) listening.<span class="hljs-title function_">off</span>(name, callback); |
| 758 |
} |
| 759 |
}</pre></div></div> |
| 760 |
|
| 761 |
</li> |
| 762 |
|
| 763 |
|
| 764 |
<li id="section-41"> |
| 765 |
<div class="annotation"> |
| 766 |
|
| 767 |
<div class="sswrap "> |
| 768 |
<a class="ss" href="#section-41">§</a> |
| 769 |
</div> |
| 770 |
<p>Replace events if there are any remaining. Otherwise, clean up.</p> |
| 771 |
|
| 772 |
</div> |
| 773 |
|
| 774 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (remaining.<span class="hljs-property">length</span>) { |
| 775 |
events[name] = remaining; |
| 776 |
} <span class="hljs-keyword">else</span> { |
| 777 |
<span class="hljs-keyword">delete</span> events[name]; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
<span class="hljs-keyword">return</span> events; |
| 782 |
};</pre></div></div> |
| 783 |
|
| 784 |
</li> |
| 785 |
|
| 786 |
|
| 787 |
<li id="section-42"> |
| 788 |
<div class="annotation"> |
| 789 |
|
| 790 |
<div class="sswrap "> |
| 791 |
<a class="ss" href="#section-42">§</a> |
| 792 |
</div> |
| 793 |
<p>Bind an event to only be triggered a single time. After the first time |
| 794 |
the callback is invoked, its listener will be removed. If multiple events |
| 795 |
are passed in using the space-separated syntax, the handler will fire |
| 796 |
once for each event, not once for a combination of all events.</p> |
| 797 |
|
| 798 |
</div> |
| 799 |
|
| 800 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">once</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">name, callback, context</span>) {</pre></div></div> |
| 801 |
|
| 802 |
</li> |
| 803 |
|
| 804 |
|
| 805 |
<li id="section-43"> |
| 806 |
<div class="annotation"> |
| 807 |
|
| 808 |
<div class="sswrap "> |
| 809 |
<a class="ss" href="#section-43">§</a> |
| 810 |
</div> |
| 811 |
<p>Map the event into a <code>{event: once}</code> object.</p> |
| 812 |
|
| 813 |
</div> |
| 814 |
|
| 815 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> events = <span class="hljs-title function_">eventsApi</span>(onceMap, {}, name, callback, <span class="hljs-variable language_">this</span>.<span class="hljs-property">off</span>.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>)); |
| 816 |
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> name === <span class="hljs-string">'string'</span> && context == <span class="hljs-literal">null</span>) callback = <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 817 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">on</span>(events, callback, context); |
| 818 |
};</pre></div></div> |
| 819 |
|
| 820 |
</li> |
| 821 |
|
| 822 |
|
| 823 |
<li id="section-44"> |
| 824 |
<div class="annotation"> |
| 825 |
|
| 826 |
<div class="sswrap "> |
| 827 |
<a class="ss" href="#section-44">§</a> |
| 828 |
</div> |
| 829 |
<p>Inversion-of-control versions of <code>once</code>.</p> |
| 830 |
|
| 831 |
</div> |
| 832 |
|
| 833 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">listenToOnce</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">obj, name, callback</span>) {</pre></div></div> |
| 834 |
|
| 835 |
</li> |
| 836 |
|
| 837 |
|
| 838 |
<li id="section-45"> |
| 839 |
<div class="annotation"> |
| 840 |
|
| 841 |
<div class="sswrap "> |
| 842 |
<a class="ss" href="#section-45">§</a> |
| 843 |
</div> |
| 844 |
<p>Map the event into a <code>{event: once}</code> object.</p> |
| 845 |
|
| 846 |
</div> |
| 847 |
|
| 848 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> events = <span class="hljs-title function_">eventsApi</span>(onceMap, {}, name, callback, <span class="hljs-variable language_">this</span>.<span class="hljs-property">stopListening</span>.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>, obj)); |
| 849 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listenTo</span>(obj, events); |
| 850 |
};</pre></div></div> |
| 851 |
|
| 852 |
</li> |
| 853 |
|
| 854 |
|
| 855 |
<li id="section-46"> |
| 856 |
<div class="annotation"> |
| 857 |
|
| 858 |
<div class="sswrap "> |
| 859 |
<a class="ss" href="#section-46">§</a> |
| 860 |
</div> |
| 861 |
<p>Reduces the event callbacks into a map of <code>{event: onceWrapper}</code>. |
| 862 |
<code>offer</code> unbinds the <code>onceWrapper</code> after it has been called.</p> |
| 863 |
|
| 864 |
</div> |
| 865 |
|
| 866 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> onceMap = <span class="hljs-keyword">function</span>(<span class="hljs-params">map, name, callback, offer</span>) { |
| 867 |
<span class="hljs-keyword">if</span> (callback) { |
| 868 |
<span class="hljs-keyword">var</span> once = map[name] = _.<span class="hljs-title function_">once</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 869 |
<span class="hljs-title function_">offer</span>(name, once); |
| 870 |
callback.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 871 |
}); |
| 872 |
once.<span class="hljs-property">_callback</span> = callback; |
| 873 |
} |
| 874 |
<span class="hljs-keyword">return</span> map; |
| 875 |
};</pre></div></div> |
| 876 |
|
| 877 |
</li> |
| 878 |
|
| 879 |
|
| 880 |
<li id="section-47"> |
| 881 |
<div class="annotation"> |
| 882 |
|
| 883 |
<div class="sswrap "> |
| 884 |
<a class="ss" href="#section-47">§</a> |
| 885 |
</div> |
| 886 |
<p>Trigger one or many events, firing all bound callbacks. Callbacks are |
| 887 |
passed the same arguments as <code>trigger</code> is, apart from the event name |
| 888 |
(unless you’re listening on <code>"all"</code>, which will cause your callback to |
| 889 |
receive the true name of the event as the first argument).</p> |
| 890 |
|
| 891 |
</div> |
| 892 |
|
| 893 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">trigger</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">name</span>) { |
| 894 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span>) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 895 |
|
| 896 |
<span class="hljs-keyword">var</span> length = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">max</span>(<span class="hljs-number">0</span>, <span class="hljs-variable language_">arguments</span>.<span class="hljs-property">length</span> - <span class="hljs-number">1</span>); |
| 897 |
<span class="hljs-keyword">var</span> args = <span class="hljs-title class_">Array</span>(length); |
| 898 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < length; i++) args[i] = <span class="hljs-variable language_">arguments</span>[i + <span class="hljs-number">1</span>]; |
| 899 |
|
| 900 |
<span class="hljs-title function_">eventsApi</span>(triggerApi, <span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span>, name, <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>, args); |
| 901 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 902 |
};</pre></div></div> |
| 903 |
|
| 904 |
</li> |
| 905 |
|
| 906 |
|
| 907 |
<li id="section-48"> |
| 908 |
<div class="annotation"> |
| 909 |
|
| 910 |
<div class="sswrap "> |
| 911 |
<a class="ss" href="#section-48">§</a> |
| 912 |
</div> |
| 913 |
<p>Handles triggering the appropriate event callbacks.</p> |
| 914 |
|
| 915 |
</div> |
| 916 |
|
| 917 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> triggerApi = <span class="hljs-keyword">function</span>(<span class="hljs-params">objEvents, name, callback, args</span>) { |
| 918 |
<span class="hljs-keyword">if</span> (objEvents) { |
| 919 |
<span class="hljs-keyword">var</span> events = objEvents[name]; |
| 920 |
<span class="hljs-keyword">var</span> allEvents = objEvents.<span class="hljs-property">all</span>; |
| 921 |
<span class="hljs-keyword">if</span> (events && allEvents) allEvents = allEvents.<span class="hljs-title function_">slice</span>(); |
| 922 |
<span class="hljs-keyword">if</span> (events) <span class="hljs-title function_">triggerEvents</span>(events, args); |
| 923 |
<span class="hljs-keyword">if</span> (allEvents) <span class="hljs-title function_">triggerEvents</span>(allEvents, [name].<span class="hljs-title function_">concat</span>(args)); |
| 924 |
} |
| 925 |
<span class="hljs-keyword">return</span> objEvents; |
| 926 |
};</pre></div></div> |
| 927 |
|
| 928 |
</li> |
| 929 |
|
| 930 |
|
| 931 |
<li id="section-49"> |
| 932 |
<div class="annotation"> |
| 933 |
|
| 934 |
<div class="sswrap "> |
| 935 |
<a class="ss" href="#section-49">§</a> |
| 936 |
</div> |
| 937 |
<p>A difficult-to-believe, but optimized internal dispatch function for |
| 938 |
triggering events. Tries to keep the usual cases speedy (most internal |
| 939 |
Backbone events have 3 arguments).</p> |
| 940 |
|
| 941 |
</div> |
| 942 |
|
| 943 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> triggerEvents = <span class="hljs-keyword">function</span>(<span class="hljs-params">events, args</span>) { |
| 944 |
<span class="hljs-keyword">var</span> ev, i = -<span class="hljs-number">1</span>, l = events.<span class="hljs-property">length</span>, a1 = args[<span class="hljs-number">0</span>], a2 = args[<span class="hljs-number">1</span>], a3 = args[<span class="hljs-number">2</span>]; |
| 945 |
<span class="hljs-keyword">switch</span> (args.<span class="hljs-property">length</span>) { |
| 946 |
<span class="hljs-keyword">case</span> <span class="hljs-number">0</span>: <span class="hljs-keyword">while</span> (++i < l) (ev = events[i]).<span class="hljs-property">callback</span>.<span class="hljs-title function_">call</span>(ev.<span class="hljs-property">ctx</span>); <span class="hljs-keyword">return</span>; |
| 947 |
<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-keyword">while</span> (++i < l) (ev = events[i]).<span class="hljs-property">callback</span>.<span class="hljs-title function_">call</span>(ev.<span class="hljs-property">ctx</span>, a1); <span class="hljs-keyword">return</span>; |
| 948 |
<span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-keyword">while</span> (++i < l) (ev = events[i]).<span class="hljs-property">callback</span>.<span class="hljs-title function_">call</span>(ev.<span class="hljs-property">ctx</span>, a1, a2); <span class="hljs-keyword">return</span>; |
| 949 |
<span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: <span class="hljs-keyword">while</span> (++i < l) (ev = events[i]).<span class="hljs-property">callback</span>.<span class="hljs-title function_">call</span>(ev.<span class="hljs-property">ctx</span>, a1, a2, a3); <span class="hljs-keyword">return</span>; |
| 950 |
<span class="hljs-attr">default</span>: <span class="hljs-keyword">while</span> (++i < l) (ev = events[i]).<span class="hljs-property">callback</span>.<span class="hljs-title function_">apply</span>(ev.<span class="hljs-property">ctx</span>, args); <span class="hljs-keyword">return</span>; |
| 951 |
} |
| 952 |
};</pre></div></div> |
| 953 |
|
| 954 |
</li> |
| 955 |
|
| 956 |
|
| 957 |
<li id="section-50"> |
| 958 |
<div class="annotation"> |
| 959 |
|
| 960 |
<div class="sswrap "> |
| 961 |
<a class="ss" href="#section-50">§</a> |
| 962 |
</div> |
| 963 |
<p>A listening class that tracks and cleans up memory bindings |
| 964 |
when all callbacks have been offed.</p> |
| 965 |
|
| 966 |
</div> |
| 967 |
|
| 968 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Listening</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">listener, obj</span>) { |
| 969 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">id</span> = listener.<span class="hljs-property">_listenId</span>; |
| 970 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">listener</span> = listener; |
| 971 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">obj</span> = obj; |
| 972 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">interop</span> = <span class="hljs-literal">true</span>; |
| 973 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">count</span> = <span class="hljs-number">0</span>; |
| 974 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span> = <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 975 |
}; |
| 976 |
|
| 977 |
<span class="hljs-title class_">Listening</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">on</span> = <span class="hljs-title class_">Events</span>.<span class="hljs-property">on</span>;</pre></div></div> |
| 978 |
|
| 979 |
</li> |
| 980 |
|
| 981 |
|
| 982 |
<li id="section-51"> |
| 983 |
<div class="annotation"> |
| 984 |
|
| 985 |
<div class="sswrap "> |
| 986 |
<a class="ss" href="#section-51">§</a> |
| 987 |
</div> |
| 988 |
<p>Offs a callback (or several). |
| 989 |
Uses an optimized counter if the listenee uses Backbone.Events. |
| 990 |
Otherwise, falls back to manual tracking to support events |
| 991 |
library interop.</p> |
| 992 |
|
| 993 |
</div> |
| 994 |
|
| 995 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Listening</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">off</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">name, callback</span>) { |
| 996 |
<span class="hljs-keyword">var</span> cleanup; |
| 997 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">interop</span>) { |
| 998 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span> = <span class="hljs-title function_">eventsApi</span>(offApi, <span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span>, name, callback, { |
| 999 |
<span class="hljs-attr">context</span>: <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>, |
| 1000 |
<span class="hljs-attr">listeners</span>: <span class="hljs-keyword">void</span> <span class="hljs-number">0</span> |
| 1001 |
}); |
| 1002 |
cleanup = !<span class="hljs-variable language_">this</span>.<span class="hljs-property">_events</span>; |
| 1003 |
} <span class="hljs-keyword">else</span> { |
| 1004 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">count</span>--; |
| 1005 |
cleanup = <span class="hljs-variable language_">this</span>.<span class="hljs-property">count</span> === <span class="hljs-number">0</span>; |
| 1006 |
} |
| 1007 |
<span class="hljs-keyword">if</span> (cleanup) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">cleanup</span>(); |
| 1008 |
};</pre></div></div> |
| 1009 |
|
| 1010 |
</li> |
| 1011 |
|
| 1012 |
|
| 1013 |
<li id="section-52"> |
| 1014 |
<div class="annotation"> |
| 1015 |
|
| 1016 |
<div class="sswrap "> |
| 1017 |
<a class="ss" href="#section-52">§</a> |
| 1018 |
</div> |
| 1019 |
<p>Cleans up memory bindings between the listener and the listenee.</p> |
| 1020 |
|
| 1021 |
</div> |
| 1022 |
|
| 1023 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Listening</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">cleanup</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1024 |
<span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">listener</span>.<span class="hljs-property">_listeningTo</span>[<span class="hljs-variable language_">this</span>.<span class="hljs-property">obj</span>.<span class="hljs-property">_listenId</span>]; |
| 1025 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">interop</span>) <span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">obj</span>.<span class="hljs-property">_listeners</span>[<span class="hljs-variable language_">this</span>.<span class="hljs-property">id</span>]; |
| 1026 |
};</pre></div></div> |
| 1027 |
|
| 1028 |
</li> |
| 1029 |
|
| 1030 |
|
| 1031 |
<li id="section-53"> |
| 1032 |
<div class="annotation"> |
| 1033 |
|
| 1034 |
<div class="sswrap "> |
| 1035 |
<a class="ss" href="#section-53">§</a> |
| 1036 |
</div> |
| 1037 |
<p>Aliases for backwards compatibility.</p> |
| 1038 |
|
| 1039 |
</div> |
| 1040 |
|
| 1041 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Events</span>.<span class="hljs-property">bind</span> = <span class="hljs-title class_">Events</span>.<span class="hljs-property">on</span>; |
| 1042 |
<span class="hljs-title class_">Events</span>.<span class="hljs-property">unbind</span> = <span class="hljs-title class_">Events</span>.<span class="hljs-property">off</span>;</pre></div></div> |
| 1043 |
|
| 1044 |
</li> |
| 1045 |
|
| 1046 |
|
| 1047 |
<li id="section-54"> |
| 1048 |
<div class="annotation"> |
| 1049 |
|
| 1050 |
<div class="sswrap "> |
| 1051 |
<a class="ss" href="#section-54">§</a> |
| 1052 |
</div> |
| 1053 |
<p>Allow the <code>Backbone</code> object to serve as a global event bus, for folks who |
| 1054 |
want global “pubsub” in a convenient place.</p> |
| 1055 |
|
| 1056 |
</div> |
| 1057 |
|
| 1058 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">Backbone</span>, <span class="hljs-title class_">Events</span>);</pre></div></div> |
| 1059 |
|
| 1060 |
</li> |
| 1061 |
|
| 1062 |
|
| 1063 |
<li id="section-55"> |
| 1064 |
<div class="annotation"> |
| 1065 |
|
| 1066 |
<div class="sswrap "> |
| 1067 |
<a class="ss" href="#section-55">§</a> |
| 1068 |
</div> |
| 1069 |
<h2 id="backbonemodel">Backbone.Model</h2> |
| 1070 |
|
| 1071 |
</div> |
| 1072 |
|
| 1073 |
</li> |
| 1074 |
|
| 1075 |
|
| 1076 |
<li id="section-56"> |
| 1077 |
<div class="annotation"> |
| 1078 |
|
| 1079 |
<div class="sswrap "> |
| 1080 |
<a class="ss" href="#section-56">§</a> |
| 1081 |
</div> |
| 1082 |
|
| 1083 |
</div> |
| 1084 |
|
| 1085 |
</li> |
| 1086 |
|
| 1087 |
|
| 1088 |
<li id="section-57"> |
| 1089 |
<div class="annotation"> |
| 1090 |
|
| 1091 |
<div class="sswrap "> |
| 1092 |
<a class="ss" href="#section-57">§</a> |
| 1093 |
</div> |
| 1094 |
<p>Backbone <strong>Models</strong> are the basic data object in the framework – |
| 1095 |
frequently representing a row in a table in a database on your server. |
| 1096 |
A discrete chunk of data and a bunch of useful, related methods for |
| 1097 |
performing computations and transformations on that data.</p> |
| 1098 |
|
| 1099 |
</div> |
| 1100 |
|
| 1101 |
</li> |
| 1102 |
|
| 1103 |
|
| 1104 |
<li id="section-58"> |
| 1105 |
<div class="annotation"> |
| 1106 |
|
| 1107 |
<div class="sswrap "> |
| 1108 |
<a class="ss" href="#section-58">§</a> |
| 1109 |
</div> |
| 1110 |
<p>Create a new model with the specified attributes. A client id (<code>cid</code>) |
| 1111 |
is automatically generated and assigned for you.</p> |
| 1112 |
|
| 1113 |
</div> |
| 1114 |
|
| 1115 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Model</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Model</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">attributes, options</span>) { |
| 1116 |
<span class="hljs-keyword">var</span> attrs = attributes || {}; |
| 1117 |
options || (options = {}); |
| 1118 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">preinitialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 1119 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">cid</span> = _.<span class="hljs-title function_">uniqueId</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">cidPrefix</span>); |
| 1120 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span> = {}; |
| 1121 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">collection</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">collection</span> = options.<span class="hljs-property">collection</span>; |
| 1122 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">parse</span>) attrs = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">parse</span>(attrs, options) || {}; |
| 1123 |
<span class="hljs-keyword">var</span> defaults = _.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'defaults'</span>); |
| 1124 |
attrs = _.<span class="hljs-title function_">defaults</span>(_.<span class="hljs-title function_">extend</span>({}, defaults, attrs), defaults); |
| 1125 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(attrs, options); |
| 1126 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">changed</span> = {}; |
| 1127 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">initialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 1128 |
};</pre></div></div> |
| 1129 |
|
| 1130 |
</li> |
| 1131 |
|
| 1132 |
|
| 1133 |
<li id="section-59"> |
| 1134 |
<div class="annotation"> |
| 1135 |
|
| 1136 |
<div class="sswrap "> |
| 1137 |
<a class="ss" href="#section-59">§</a> |
| 1138 |
</div> |
| 1139 |
<p>Attach all inheritable methods to the Model prototype.</p> |
| 1140 |
|
| 1141 |
</div> |
| 1142 |
|
| 1143 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">Model</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, <span class="hljs-title class_">Events</span>, {</pre></div></div> |
| 1144 |
|
| 1145 |
</li> |
| 1146 |
|
| 1147 |
|
| 1148 |
<li id="section-60"> |
| 1149 |
<div class="annotation"> |
| 1150 |
|
| 1151 |
<div class="sswrap "> |
| 1152 |
<a class="ss" href="#section-60">§</a> |
| 1153 |
</div> |
| 1154 |
<p>A hash of attributes whose current and previous value differ.</p> |
| 1155 |
|
| 1156 |
</div> |
| 1157 |
|
| 1158 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">changed</span>: <span class="hljs-literal">null</span>,</pre></div></div> |
| 1159 |
|
| 1160 |
</li> |
| 1161 |
|
| 1162 |
|
| 1163 |
<li id="section-61"> |
| 1164 |
<div class="annotation"> |
| 1165 |
|
| 1166 |
<div class="sswrap "> |
| 1167 |
<a class="ss" href="#section-61">§</a> |
| 1168 |
</div> |
| 1169 |
<p>The value returned during the last failed validation.</p> |
| 1170 |
|
| 1171 |
</div> |
| 1172 |
|
| 1173 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">validationError</span>: <span class="hljs-literal">null</span>,</pre></div></div> |
| 1174 |
|
| 1175 |
</li> |
| 1176 |
|
| 1177 |
|
| 1178 |
<li id="section-62"> |
| 1179 |
<div class="annotation"> |
| 1180 |
|
| 1181 |
<div class="sswrap "> |
| 1182 |
<a class="ss" href="#section-62">§</a> |
| 1183 |
</div> |
| 1184 |
<p>The default name for the JSON <code>id</code> attribute is <code>"id"</code>. MongoDB and |
| 1185 |
CouchDB users may want to set this to <code>"_id"</code>.</p> |
| 1186 |
|
| 1187 |
</div> |
| 1188 |
|
| 1189 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">idAttribute</span>: <span class="hljs-string">'id'</span>,</pre></div></div> |
| 1190 |
|
| 1191 |
</li> |
| 1192 |
|
| 1193 |
|
| 1194 |
<li id="section-63"> |
| 1195 |
<div class="annotation"> |
| 1196 |
|
| 1197 |
<div class="sswrap "> |
| 1198 |
<a class="ss" href="#section-63">§</a> |
| 1199 |
</div> |
| 1200 |
<p>The prefix is used to create the client id which is used to identify models locally. |
| 1201 |
You may want to override this if you’re experiencing name clashes with model ids.</p> |
| 1202 |
|
| 1203 |
</div> |
| 1204 |
|
| 1205 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">cidPrefix</span>: <span class="hljs-string">'c'</span>,</pre></div></div> |
| 1206 |
|
| 1207 |
</li> |
| 1208 |
|
| 1209 |
|
| 1210 |
<li id="section-64"> |
| 1211 |
<div class="annotation"> |
| 1212 |
|
| 1213 |
<div class="sswrap "> |
| 1214 |
<a class="ss" href="#section-64">§</a> |
| 1215 |
</div> |
| 1216 |
<p>preinitialize is an empty function by default. You can override it with a function |
| 1217 |
or object. preinitialize will run before any instantiation logic is run in the Model.</p> |
| 1218 |
|
| 1219 |
</div> |
| 1220 |
|
| 1221 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">preinitialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 1222 |
|
| 1223 |
</li> |
| 1224 |
|
| 1225 |
|
| 1226 |
<li id="section-65"> |
| 1227 |
<div class="annotation"> |
| 1228 |
|
| 1229 |
<div class="sswrap "> |
| 1230 |
<a class="ss" href="#section-65">§</a> |
| 1231 |
</div> |
| 1232 |
<p>Initialize is an empty function by default. Override it with your own |
| 1233 |
initialization logic.</p> |
| 1234 |
|
| 1235 |
</div> |
| 1236 |
|
| 1237 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">initialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 1238 |
|
| 1239 |
</li> |
| 1240 |
|
| 1241 |
|
| 1242 |
<li id="section-66"> |
| 1243 |
<div class="annotation"> |
| 1244 |
|
| 1245 |
<div class="sswrap "> |
| 1246 |
<a class="ss" href="#section-66">§</a> |
| 1247 |
</div> |
| 1248 |
<p>Return a copy of the model’s <code>attributes</code> object.</p> |
| 1249 |
|
| 1250 |
</div> |
| 1251 |
|
| 1252 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">toJSON</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 1253 |
<span class="hljs-keyword">return</span> _.<span class="hljs-title function_">clone</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>); |
| 1254 |
},</pre></div></div> |
| 1255 |
|
| 1256 |
</li> |
| 1257 |
|
| 1258 |
|
| 1259 |
<li id="section-67"> |
| 1260 |
<div class="annotation"> |
| 1261 |
|
| 1262 |
<div class="sswrap "> |
| 1263 |
<a class="ss" href="#section-67">§</a> |
| 1264 |
</div> |
| 1265 |
<p>Proxy <code>Backbone.sync</code> by default – but override this if you need |
| 1266 |
custom syncing semantics for <em>this</em> particular model.</p> |
| 1267 |
|
| 1268 |
</div> |
| 1269 |
|
| 1270 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">sync</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1271 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">sync</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 1272 |
},</pre></div></div> |
| 1273 |
|
| 1274 |
</li> |
| 1275 |
|
| 1276 |
|
| 1277 |
<li id="section-68"> |
| 1278 |
<div class="annotation"> |
| 1279 |
|
| 1280 |
<div class="sswrap "> |
| 1281 |
<a class="ss" href="#section-68">§</a> |
| 1282 |
</div> |
| 1283 |
<p>Get the value of an attribute.</p> |
| 1284 |
|
| 1285 |
</div> |
| 1286 |
|
| 1287 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">get</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr</span>) { |
| 1288 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>[attr]; |
| 1289 |
},</pre></div></div> |
| 1290 |
|
| 1291 |
</li> |
| 1292 |
|
| 1293 |
|
| 1294 |
<li id="section-69"> |
| 1295 |
<div class="annotation"> |
| 1296 |
|
| 1297 |
<div class="sswrap "> |
| 1298 |
<a class="ss" href="#section-69">§</a> |
| 1299 |
</div> |
| 1300 |
<p>Get the HTML-escaped value of an attribute.</p> |
| 1301 |
|
| 1302 |
</div> |
| 1303 |
|
| 1304 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">escape</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr</span>) { |
| 1305 |
<span class="hljs-keyword">return</span> _.<span class="hljs-built_in">escape</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(attr)); |
| 1306 |
},</pre></div></div> |
| 1307 |
|
| 1308 |
</li> |
| 1309 |
|
| 1310 |
|
| 1311 |
<li id="section-70"> |
| 1312 |
<div class="annotation"> |
| 1313 |
|
| 1314 |
<div class="sswrap "> |
| 1315 |
<a class="ss" href="#section-70">§</a> |
| 1316 |
</div> |
| 1317 |
<p>Returns <code>true</code> if the attribute contains a value that is not null |
| 1318 |
or undefined.</p> |
| 1319 |
|
| 1320 |
</div> |
| 1321 |
|
| 1322 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">has</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr</span>) { |
| 1323 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(attr) != <span class="hljs-literal">null</span>; |
| 1324 |
},</pre></div></div> |
| 1325 |
|
| 1326 |
</li> |
| 1327 |
|
| 1328 |
|
| 1329 |
<li id="section-71"> |
| 1330 |
<div class="annotation"> |
| 1331 |
|
| 1332 |
<div class="sswrap "> |
| 1333 |
<a class="ss" href="#section-71">§</a> |
| 1334 |
</div> |
| 1335 |
<p>Special-cased proxy to underscore’s <code>_.matches</code> method.</p> |
| 1336 |
|
| 1337 |
</div> |
| 1338 |
|
| 1339 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">matches</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs</span>) { |
| 1340 |
<span class="hljs-keyword">return</span> !!_.<span class="hljs-title function_">iteratee</span>(attrs, <span class="hljs-variable language_">this</span>)(<span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>); |
| 1341 |
},</pre></div></div> |
| 1342 |
|
| 1343 |
</li> |
| 1344 |
|
| 1345 |
|
| 1346 |
<li id="section-72"> |
| 1347 |
<div class="annotation"> |
| 1348 |
|
| 1349 |
<div class="sswrap "> |
| 1350 |
<a class="ss" href="#section-72">§</a> |
| 1351 |
</div> |
| 1352 |
<p>Set a hash of model attributes on the object, firing <code>"change"</code>. This is |
| 1353 |
the core primitive operation of a model, updating the data and notifying |
| 1354 |
anyone who needs to know about the change in state. The heart of the beast.</p> |
| 1355 |
|
| 1356 |
</div> |
| 1357 |
|
| 1358 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">set</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">key, val, options</span>) { |
| 1359 |
<span class="hljs-keyword">if</span> (key == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>;</pre></div></div> |
| 1360 |
|
| 1361 |
</li> |
| 1362 |
|
| 1363 |
|
| 1364 |
<li id="section-73"> |
| 1365 |
<div class="annotation"> |
| 1366 |
|
| 1367 |
<div class="sswrap "> |
| 1368 |
<a class="ss" href="#section-73">§</a> |
| 1369 |
</div> |
| 1370 |
<p>Handle both <code>"key", value</code> and <code>{key: value}</code> -style arguments.</p> |
| 1371 |
|
| 1372 |
</div> |
| 1373 |
|
| 1374 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> attrs; |
| 1375 |
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> key === <span class="hljs-string">'object'</span>) { |
| 1376 |
attrs = key; |
| 1377 |
options = val; |
| 1378 |
} <span class="hljs-keyword">else</span> { |
| 1379 |
(attrs = {})[key] = val; |
| 1380 |
} |
| 1381 |
|
| 1382 |
options || (options = {});</pre></div></div> |
| 1383 |
|
| 1384 |
</li> |
| 1385 |
|
| 1386 |
|
| 1387 |
<li id="section-74"> |
| 1388 |
<div class="annotation"> |
| 1389 |
|
| 1390 |
<div class="sswrap "> |
| 1391 |
<a class="ss" href="#section-74">§</a> |
| 1392 |
</div> |
| 1393 |
<p>Run validation.</p> |
| 1394 |
|
| 1395 |
</div> |
| 1396 |
|
| 1397 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_validate</span>(attrs, options)) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;</pre></div></div> |
| 1398 |
|
| 1399 |
</li> |
| 1400 |
|
| 1401 |
|
| 1402 |
<li id="section-75"> |
| 1403 |
<div class="annotation"> |
| 1404 |
|
| 1405 |
<div class="sswrap "> |
| 1406 |
<a class="ss" href="#section-75">§</a> |
| 1407 |
</div> |
| 1408 |
<p>Extract attributes and options.</p> |
| 1409 |
|
| 1410 |
</div> |
| 1411 |
|
| 1412 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> unset = options.<span class="hljs-property">unset</span>; |
| 1413 |
<span class="hljs-keyword">var</span> silent = options.<span class="hljs-property">silent</span>; |
| 1414 |
<span class="hljs-keyword">var</span> changes = []; |
| 1415 |
<span class="hljs-keyword">var</span> changing = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_changing</span>; |
| 1416 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_changing</span> = <span class="hljs-literal">true</span>; |
| 1417 |
|
| 1418 |
<span class="hljs-keyword">if</span> (!changing) { |
| 1419 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_previousAttributes</span> = _.<span class="hljs-title function_">clone</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>); |
| 1420 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">changed</span> = {}; |
| 1421 |
} |
| 1422 |
|
| 1423 |
<span class="hljs-keyword">var</span> current = <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>; |
| 1424 |
<span class="hljs-keyword">var</span> changed = <span class="hljs-variable language_">this</span>.<span class="hljs-property">changed</span>; |
| 1425 |
<span class="hljs-keyword">var</span> prev = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_previousAttributes</span>;</pre></div></div> |
| 1426 |
|
| 1427 |
</li> |
| 1428 |
|
| 1429 |
|
| 1430 |
<li id="section-76"> |
| 1431 |
<div class="annotation"> |
| 1432 |
|
| 1433 |
<div class="sswrap "> |
| 1434 |
<a class="ss" href="#section-76">§</a> |
| 1435 |
</div> |
| 1436 |
<p>For each <code>set</code> attribute, update or delete the current value.</p> |
| 1437 |
|
| 1438 |
</div> |
| 1439 |
|
| 1440 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> attr <span class="hljs-keyword">in</span> attrs) { |
| 1441 |
val = attrs[attr]; |
| 1442 |
<span class="hljs-keyword">if</span> (!_.<span class="hljs-title function_">isEqual</span>(current[attr], val)) changes.<span class="hljs-title function_">push</span>(attr); |
| 1443 |
<span class="hljs-keyword">if</span> (!_.<span class="hljs-title function_">isEqual</span>(prev[attr], val)) { |
| 1444 |
changed[attr] = val; |
| 1445 |
} <span class="hljs-keyword">else</span> { |
| 1446 |
<span class="hljs-keyword">delete</span> changed[attr]; |
| 1447 |
} |
| 1448 |
unset ? <span class="hljs-keyword">delete</span> current[attr] : current[attr] = val; |
| 1449 |
}</pre></div></div> |
| 1450 |
|
| 1451 |
</li> |
| 1452 |
|
| 1453 |
|
| 1454 |
<li id="section-77"> |
| 1455 |
<div class="annotation"> |
| 1456 |
|
| 1457 |
<div class="sswrap "> |
| 1458 |
<a class="ss" href="#section-77">§</a> |
| 1459 |
</div> |
| 1460 |
<p>Update the <code>id</code>.</p> |
| 1461 |
|
| 1462 |
</div> |
| 1463 |
|
| 1464 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">idAttribute</span> <span class="hljs-keyword">in</span> attrs) { |
| 1465 |
<span class="hljs-keyword">var</span> prevId = <span class="hljs-variable language_">this</span>.<span class="hljs-property">id</span>; |
| 1466 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">id</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">idAttribute</span>); |
| 1467 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'changeId'</span>, <span class="hljs-variable language_">this</span>, prevId, options); |
| 1468 |
}</pre></div></div> |
| 1469 |
|
| 1470 |
</li> |
| 1471 |
|
| 1472 |
|
| 1473 |
<li id="section-78"> |
| 1474 |
<div class="annotation"> |
| 1475 |
|
| 1476 |
<div class="sswrap "> |
| 1477 |
<a class="ss" href="#section-78">§</a> |
| 1478 |
</div> |
| 1479 |
<p>Trigger all relevant attribute changes.</p> |
| 1480 |
|
| 1481 |
</div> |
| 1482 |
|
| 1483 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!silent) { |
| 1484 |
<span class="hljs-keyword">if</span> (changes.<span class="hljs-property">length</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">_pending</span> = options; |
| 1485 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < changes.<span class="hljs-property">length</span>; i++) { |
| 1486 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'change:'</span> + changes[i], <span class="hljs-variable language_">this</span>, current[changes[i]], options); |
| 1487 |
} |
| 1488 |
}</pre></div></div> |
| 1489 |
|
| 1490 |
</li> |
| 1491 |
|
| 1492 |
|
| 1493 |
<li id="section-79"> |
| 1494 |
<div class="annotation"> |
| 1495 |
|
| 1496 |
<div class="sswrap "> |
| 1497 |
<a class="ss" href="#section-79">§</a> |
| 1498 |
</div> |
| 1499 |
<p>You might be wondering why there’s a <code>while</code> loop here. Changes can |
| 1500 |
be recursively nested within <code>"change"</code> events.</p> |
| 1501 |
|
| 1502 |
</div> |
| 1503 |
|
| 1504 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (changing) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 1505 |
<span class="hljs-keyword">if</span> (!silent) { |
| 1506 |
<span class="hljs-keyword">while</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_pending</span>) { |
| 1507 |
options = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_pending</span>; |
| 1508 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_pending</span> = <span class="hljs-literal">false</span>; |
| 1509 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'change'</span>, <span class="hljs-variable language_">this</span>, options); |
| 1510 |
} |
| 1511 |
} |
| 1512 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_pending</span> = <span class="hljs-literal">false</span>; |
| 1513 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_changing</span> = <span class="hljs-literal">false</span>; |
| 1514 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 1515 |
},</pre></div></div> |
| 1516 |
|
| 1517 |
</li> |
| 1518 |
|
| 1519 |
|
| 1520 |
<li id="section-80"> |
| 1521 |
<div class="annotation"> |
| 1522 |
|
| 1523 |
<div class="sswrap "> |
| 1524 |
<a class="ss" href="#section-80">§</a> |
| 1525 |
</div> |
| 1526 |
<p>Remove an attribute from the model, firing <code>"change"</code>. <code>unset</code> is a noop |
| 1527 |
if the attribute doesn’t exist.</p> |
| 1528 |
|
| 1529 |
</div> |
| 1530 |
|
| 1531 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">unset</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr, options</span>) { |
| 1532 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(attr, <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>, _.<span class="hljs-title function_">extend</span>({}, options, {<span class="hljs-attr">unset</span>: <span class="hljs-literal">true</span>})); |
| 1533 |
},</pre></div></div> |
| 1534 |
|
| 1535 |
</li> |
| 1536 |
|
| 1537 |
|
| 1538 |
<li id="section-81"> |
| 1539 |
<div class="annotation"> |
| 1540 |
|
| 1541 |
<div class="sswrap "> |
| 1542 |
<a class="ss" href="#section-81">§</a> |
| 1543 |
</div> |
| 1544 |
<p>Clear all attributes on the model, firing <code>"change"</code>.</p> |
| 1545 |
|
| 1546 |
</div> |
| 1547 |
|
| 1548 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">clear</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 1549 |
<span class="hljs-keyword">var</span> attrs = {}; |
| 1550 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> key <span class="hljs-keyword">in</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>) attrs[key] = <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 1551 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(attrs, _.<span class="hljs-title function_">extend</span>({}, options, {<span class="hljs-attr">unset</span>: <span class="hljs-literal">true</span>})); |
| 1552 |
},</pre></div></div> |
| 1553 |
|
| 1554 |
</li> |
| 1555 |
|
| 1556 |
|
| 1557 |
<li id="section-82"> |
| 1558 |
<div class="annotation"> |
| 1559 |
|
| 1560 |
<div class="sswrap "> |
| 1561 |
<a class="ss" href="#section-82">§</a> |
| 1562 |
</div> |
| 1563 |
<p>Determine if the model has changed since the last <code>"change"</code> event. |
| 1564 |
If you specify an attribute name, determine if that attribute has changed.</p> |
| 1565 |
|
| 1566 |
</div> |
| 1567 |
|
| 1568 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">hasChanged</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr</span>) { |
| 1569 |
<span class="hljs-keyword">if</span> (attr == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> !_.<span class="hljs-title function_">isEmpty</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">changed</span>); |
| 1570 |
<span class="hljs-keyword">return</span> _.<span class="hljs-title function_">has</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">changed</span>, attr); |
| 1571 |
},</pre></div></div> |
| 1572 |
|
| 1573 |
</li> |
| 1574 |
|
| 1575 |
|
| 1576 |
<li id="section-83"> |
| 1577 |
<div class="annotation"> |
| 1578 |
|
| 1579 |
<div class="sswrap "> |
| 1580 |
<a class="ss" href="#section-83">§</a> |
| 1581 |
</div> |
| 1582 |
<p>Return an object containing all the attributes that have changed, or |
| 1583 |
false if there are no changed attributes. Useful for determining what |
| 1584 |
parts of a view need to be updated and/or what attributes need to be |
| 1585 |
persisted to the server. Unset attributes will be set to undefined. |
| 1586 |
You can also pass an attributes object to diff against the model, |
| 1587 |
determining if there <em>would be</em> a change.</p> |
| 1588 |
|
| 1589 |
</div> |
| 1590 |
|
| 1591 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">changedAttributes</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">diff</span>) { |
| 1592 |
<span class="hljs-keyword">if</span> (!diff) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">hasChanged</span>() ? _.<span class="hljs-title function_">clone</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">changed</span>) : <span class="hljs-literal">false</span>; |
| 1593 |
<span class="hljs-keyword">var</span> old = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_changing</span> ? <span class="hljs-variable language_">this</span>.<span class="hljs-property">_previousAttributes</span> : <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>; |
| 1594 |
<span class="hljs-keyword">var</span> changed = {}; |
| 1595 |
<span class="hljs-keyword">var</span> hasChanged; |
| 1596 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> attr <span class="hljs-keyword">in</span> diff) { |
| 1597 |
<span class="hljs-keyword">var</span> val = diff[attr]; |
| 1598 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isEqual</span>(old[attr], val)) <span class="hljs-keyword">continue</span>; |
| 1599 |
changed[attr] = val; |
| 1600 |
hasChanged = <span class="hljs-literal">true</span>; |
| 1601 |
} |
| 1602 |
<span class="hljs-keyword">return</span> hasChanged ? changed : <span class="hljs-literal">false</span>; |
| 1603 |
},</pre></div></div> |
| 1604 |
|
| 1605 |
</li> |
| 1606 |
|
| 1607 |
|
| 1608 |
<li id="section-84"> |
| 1609 |
<div class="annotation"> |
| 1610 |
|
| 1611 |
<div class="sswrap "> |
| 1612 |
<a class="ss" href="#section-84">§</a> |
| 1613 |
</div> |
| 1614 |
<p>Get the previous value of an attribute, recorded at the time the last |
| 1615 |
<code>"change"</code> event was fired.</p> |
| 1616 |
|
| 1617 |
</div> |
| 1618 |
|
| 1619 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">previous</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr</span>) { |
| 1620 |
<span class="hljs-keyword">if</span> (attr == <span class="hljs-literal">null</span> || !<span class="hljs-variable language_">this</span>.<span class="hljs-property">_previousAttributes</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>; |
| 1621 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_previousAttributes</span>[attr]; |
| 1622 |
},</pre></div></div> |
| 1623 |
|
| 1624 |
</li> |
| 1625 |
|
| 1626 |
|
| 1627 |
<li id="section-85"> |
| 1628 |
<div class="annotation"> |
| 1629 |
|
| 1630 |
<div class="sswrap "> |
| 1631 |
<a class="ss" href="#section-85">§</a> |
| 1632 |
</div> |
| 1633 |
<p>Get all of the attributes of the model at the time of the previous |
| 1634 |
<code>"change"</code> event.</p> |
| 1635 |
|
| 1636 |
</div> |
| 1637 |
|
| 1638 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">previousAttributes</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1639 |
<span class="hljs-keyword">return</span> _.<span class="hljs-title function_">clone</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">_previousAttributes</span>); |
| 1640 |
},</pre></div></div> |
| 1641 |
|
| 1642 |
</li> |
| 1643 |
|
| 1644 |
|
| 1645 |
<li id="section-86"> |
| 1646 |
<div class="annotation"> |
| 1647 |
|
| 1648 |
<div class="sswrap "> |
| 1649 |
<a class="ss" href="#section-86">§</a> |
| 1650 |
</div> |
| 1651 |
<p>Fetch the model from the server, merging the response with the model’s |
| 1652 |
local attributes. Any changed attributes will trigger a “change” event.</p> |
| 1653 |
|
| 1654 |
</div> |
| 1655 |
|
| 1656 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">fetch</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 1657 |
options = _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">parse</span>: <span class="hljs-literal">true</span>}, options); |
| 1658 |
<span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>; |
| 1659 |
<span class="hljs-keyword">var</span> success = options.<span class="hljs-property">success</span>; |
| 1660 |
options.<span class="hljs-property">success</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) { |
| 1661 |
<span class="hljs-keyword">var</span> serverAttrs = options.<span class="hljs-property">parse</span> ? model.<span class="hljs-title function_">parse</span>(resp, options) : resp; |
| 1662 |
<span class="hljs-keyword">if</span> (!model.<span class="hljs-title function_">set</span>(serverAttrs, options)) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 1663 |
<span class="hljs-keyword">if</span> (success) success.<span class="hljs-title function_">call</span>(options.<span class="hljs-property">context</span>, model, resp, options); |
| 1664 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'sync'</span>, model, resp, options); |
| 1665 |
}; |
| 1666 |
<span class="hljs-title function_">wrapError</span>(<span class="hljs-variable language_">this</span>, options); |
| 1667 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">sync</span>(<span class="hljs-string">'read'</span>, <span class="hljs-variable language_">this</span>, options); |
| 1668 |
},</pre></div></div> |
| 1669 |
|
| 1670 |
</li> |
| 1671 |
|
| 1672 |
|
| 1673 |
<li id="section-87"> |
| 1674 |
<div class="annotation"> |
| 1675 |
|
| 1676 |
<div class="sswrap "> |
| 1677 |
<a class="ss" href="#section-87">§</a> |
| 1678 |
</div> |
| 1679 |
<p>Set a hash of model attributes, and sync the model to the server. |
| 1680 |
If the server returns an attributes hash that differs, the model’s |
| 1681 |
state will be <code>set</code> again.</p> |
| 1682 |
|
| 1683 |
</div> |
| 1684 |
|
| 1685 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">save</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">key, val, options</span>) {</pre></div></div> |
| 1686 |
|
| 1687 |
</li> |
| 1688 |
|
| 1689 |
|
| 1690 |
<li id="section-88"> |
| 1691 |
<div class="annotation"> |
| 1692 |
|
| 1693 |
<div class="sswrap "> |
| 1694 |
<a class="ss" href="#section-88">§</a> |
| 1695 |
</div> |
| 1696 |
<p>Handle both <code>"key", value</code> and <code>{key: value}</code> -style arguments.</p> |
| 1697 |
|
| 1698 |
</div> |
| 1699 |
|
| 1700 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> attrs; |
| 1701 |
<span class="hljs-keyword">if</span> (key == <span class="hljs-literal">null</span> || <span class="hljs-keyword">typeof</span> key === <span class="hljs-string">'object'</span>) { |
| 1702 |
attrs = key; |
| 1703 |
options = val; |
| 1704 |
} <span class="hljs-keyword">else</span> { |
| 1705 |
(attrs = {})[key] = val; |
| 1706 |
} |
| 1707 |
|
| 1708 |
options = _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">validate</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">parse</span>: <span class="hljs-literal">true</span>}, options); |
| 1709 |
<span class="hljs-keyword">var</span> wait = options.<span class="hljs-property">wait</span>;</pre></div></div> |
| 1710 |
|
| 1711 |
</li> |
| 1712 |
|
| 1713 |
|
| 1714 |
<li id="section-89"> |
| 1715 |
<div class="annotation"> |
| 1716 |
|
| 1717 |
<div class="sswrap "> |
| 1718 |
<a class="ss" href="#section-89">§</a> |
| 1719 |
</div> |
| 1720 |
<p>If we’re not waiting and attributes exist, save acts as |
| 1721 |
<code>set(attr).save(null, opts)</code> with validation. Otherwise, check if |
| 1722 |
the model will be valid when the attributes, if any, are set.</p> |
| 1723 |
|
| 1724 |
</div> |
| 1725 |
|
| 1726 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (attrs && !wait) { |
| 1727 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(attrs, options)) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 1728 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_validate</span>(attrs, options)) { |
| 1729 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 1730 |
}</pre></div></div> |
| 1731 |
|
| 1732 |
</li> |
| 1733 |
|
| 1734 |
|
| 1735 |
<li id="section-90"> |
| 1736 |
<div class="annotation"> |
| 1737 |
|
| 1738 |
<div class="sswrap "> |
| 1739 |
<a class="ss" href="#section-90">§</a> |
| 1740 |
</div> |
| 1741 |
<p>After a successful server-side save, the client is (optionally) |
| 1742 |
updated with the server-side state.</p> |
| 1743 |
|
| 1744 |
</div> |
| 1745 |
|
| 1746 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>; |
| 1747 |
<span class="hljs-keyword">var</span> success = options.<span class="hljs-property">success</span>; |
| 1748 |
<span class="hljs-keyword">var</span> attributes = <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>; |
| 1749 |
options.<span class="hljs-property">success</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) {</pre></div></div> |
| 1750 |
|
| 1751 |
</li> |
| 1752 |
|
| 1753 |
|
| 1754 |
<li id="section-91"> |
| 1755 |
<div class="annotation"> |
| 1756 |
|
| 1757 |
<div class="sswrap "> |
| 1758 |
<a class="ss" href="#section-91">§</a> |
| 1759 |
</div> |
| 1760 |
<p>Ensure attributes are restored during synchronous saves.</p> |
| 1761 |
|
| 1762 |
</div> |
| 1763 |
|
| 1764 |
<div class="content"><div class='highlight'><pre> model.<span class="hljs-property">attributes</span> = attributes; |
| 1765 |
<span class="hljs-keyword">var</span> serverAttrs = options.<span class="hljs-property">parse</span> ? model.<span class="hljs-title function_">parse</span>(resp, options) : resp; |
| 1766 |
<span class="hljs-keyword">if</span> (wait) serverAttrs = _.<span class="hljs-title function_">extend</span>({}, attrs, serverAttrs); |
| 1767 |
<span class="hljs-keyword">if</span> (serverAttrs && !model.<span class="hljs-title function_">set</span>(serverAttrs, options)) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 1768 |
<span class="hljs-keyword">if</span> (success) success.<span class="hljs-title function_">call</span>(options.<span class="hljs-property">context</span>, model, resp, options); |
| 1769 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'sync'</span>, model, resp, options); |
| 1770 |
}; |
| 1771 |
<span class="hljs-title function_">wrapError</span>(<span class="hljs-variable language_">this</span>, options);</pre></div></div> |
| 1772 |
|
| 1773 |
</li> |
| 1774 |
|
| 1775 |
|
| 1776 |
<li id="section-92"> |
| 1777 |
<div class="annotation"> |
| 1778 |
|
| 1779 |
<div class="sswrap "> |
| 1780 |
<a class="ss" href="#section-92">§</a> |
| 1781 |
</div> |
| 1782 |
<p>Set temporary attributes if <code>{wait: true}</code> to properly find new ids.</p> |
| 1783 |
|
| 1784 |
</div> |
| 1785 |
|
| 1786 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (attrs && wait) <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span> = _.<span class="hljs-title function_">extend</span>({}, attributes, attrs); |
| 1787 |
|
| 1788 |
<span class="hljs-keyword">var</span> method = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">isNew</span>() ? <span class="hljs-string">'create'</span> : options.<span class="hljs-property">patch</span> ? <span class="hljs-string">'patch'</span> : <span class="hljs-string">'update'</span>; |
| 1789 |
<span class="hljs-keyword">if</span> (method === <span class="hljs-string">'patch'</span> && !options.<span class="hljs-property">attrs</span>) options.<span class="hljs-property">attrs</span> = attrs; |
| 1790 |
<span class="hljs-keyword">var</span> xhr = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">sync</span>(method, <span class="hljs-variable language_">this</span>, options);</pre></div></div> |
| 1791 |
|
| 1792 |
</li> |
| 1793 |
|
| 1794 |
|
| 1795 |
<li id="section-93"> |
| 1796 |
<div class="annotation"> |
| 1797 |
|
| 1798 |
<div class="sswrap "> |
| 1799 |
<a class="ss" href="#section-93">§</a> |
| 1800 |
</div> |
| 1801 |
<p>Restore attributes.</p> |
| 1802 |
|
| 1803 |
</div> |
| 1804 |
|
| 1805 |
<div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span> = attributes; |
| 1806 |
|
| 1807 |
<span class="hljs-keyword">return</span> xhr; |
| 1808 |
},</pre></div></div> |
| 1809 |
|
| 1810 |
</li> |
| 1811 |
|
| 1812 |
|
| 1813 |
<li id="section-94"> |
| 1814 |
<div class="annotation"> |
| 1815 |
|
| 1816 |
<div class="sswrap "> |
| 1817 |
<a class="ss" href="#section-94">§</a> |
| 1818 |
</div> |
| 1819 |
<p>Destroy this model on the server if it was already persisted. |
| 1820 |
Optimistically removes the model from its collection, if it has one. |
| 1821 |
If <code>wait: true</code> is passed, waits for the server to respond before removal.</p> |
| 1822 |
|
| 1823 |
</div> |
| 1824 |
|
| 1825 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">destroy</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 1826 |
options = options ? _.<span class="hljs-title function_">clone</span>(options) : {}; |
| 1827 |
<span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>; |
| 1828 |
<span class="hljs-keyword">var</span> success = options.<span class="hljs-property">success</span>; |
| 1829 |
<span class="hljs-keyword">var</span> wait = options.<span class="hljs-property">wait</span>; |
| 1830 |
|
| 1831 |
<span class="hljs-keyword">var</span> destroy = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1832 |
model.<span class="hljs-title function_">stopListening</span>(); |
| 1833 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'destroy'</span>, model, model.<span class="hljs-property">collection</span>, options); |
| 1834 |
}; |
| 1835 |
|
| 1836 |
options.<span class="hljs-property">success</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) { |
| 1837 |
<span class="hljs-keyword">if</span> (wait) <span class="hljs-title function_">destroy</span>(); |
| 1838 |
<span class="hljs-keyword">if</span> (success) success.<span class="hljs-title function_">call</span>(options.<span class="hljs-property">context</span>, model, resp, options); |
| 1839 |
<span class="hljs-keyword">if</span> (!model.<span class="hljs-title function_">isNew</span>()) model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'sync'</span>, model, resp, options); |
| 1840 |
}; |
| 1841 |
|
| 1842 |
<span class="hljs-keyword">var</span> xhr = <span class="hljs-literal">false</span>; |
| 1843 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">isNew</span>()) { |
| 1844 |
_.<span class="hljs-title function_">defer</span>(options.<span class="hljs-property">success</span>); |
| 1845 |
} <span class="hljs-keyword">else</span> { |
| 1846 |
<span class="hljs-title function_">wrapError</span>(<span class="hljs-variable language_">this</span>, options); |
| 1847 |
xhr = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">sync</span>(<span class="hljs-string">'delete'</span>, <span class="hljs-variable language_">this</span>, options); |
| 1848 |
} |
| 1849 |
<span class="hljs-keyword">if</span> (!wait) <span class="hljs-title function_">destroy</span>(); |
| 1850 |
<span class="hljs-keyword">return</span> xhr; |
| 1851 |
},</pre></div></div> |
| 1852 |
|
| 1853 |
</li> |
| 1854 |
|
| 1855 |
|
| 1856 |
<li id="section-95"> |
| 1857 |
<div class="annotation"> |
| 1858 |
|
| 1859 |
<div class="sswrap "> |
| 1860 |
<a class="ss" href="#section-95">§</a> |
| 1861 |
</div> |
| 1862 |
<p>Default URL for the model’s representation on the server – if you’re |
| 1863 |
using Backbone’s restful methods, override this to change the endpoint |
| 1864 |
that will be called.</p> |
| 1865 |
|
| 1866 |
</div> |
| 1867 |
|
| 1868 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">url</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1869 |
<span class="hljs-keyword">var</span> base = |
| 1870 |
_.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'urlRoot'</span>) || |
| 1871 |
_.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">collection</span>, <span class="hljs-string">'url'</span>) || |
| 1872 |
<span class="hljs-title function_">urlError</span>(); |
| 1873 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">isNew</span>()) <span class="hljs-keyword">return</span> base; |
| 1874 |
<span class="hljs-keyword">var</span> id = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">idAttribute</span>); |
| 1875 |
<span class="hljs-keyword">return</span> base.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/[^\/]$/</span>, <span class="hljs-string">'$&/'</span>) + <span class="hljs-built_in">encodeURIComponent</span>(id); |
| 1876 |
},</pre></div></div> |
| 1877 |
|
| 1878 |
</li> |
| 1879 |
|
| 1880 |
|
| 1881 |
<li id="section-96"> |
| 1882 |
<div class="annotation"> |
| 1883 |
|
| 1884 |
<div class="sswrap "> |
| 1885 |
<a class="ss" href="#section-96">§</a> |
| 1886 |
</div> |
| 1887 |
<p><strong>parse</strong> converts a response into the hash of attributes to be <code>set</code> on |
| 1888 |
the model. The default implementation is just to pass the response along.</p> |
| 1889 |
|
| 1890 |
</div> |
| 1891 |
|
| 1892 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">parse</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">resp, options</span>) { |
| 1893 |
<span class="hljs-keyword">return</span> resp; |
| 1894 |
},</pre></div></div> |
| 1895 |
|
| 1896 |
</li> |
| 1897 |
|
| 1898 |
|
| 1899 |
<li id="section-97"> |
| 1900 |
<div class="annotation"> |
| 1901 |
|
| 1902 |
<div class="sswrap "> |
| 1903 |
<a class="ss" href="#section-97">§</a> |
| 1904 |
</div> |
| 1905 |
<p>Create a new model with identical attributes to this one.</p> |
| 1906 |
|
| 1907 |
</div> |
| 1908 |
|
| 1909 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">clone</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1910 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">constructor</span>(<span class="hljs-params"><span class="hljs-variable language_">this</span>.attributes</span>); |
| 1911 |
},</pre></div></div> |
| 1912 |
|
| 1913 |
</li> |
| 1914 |
|
| 1915 |
|
| 1916 |
<li id="section-98"> |
| 1917 |
<div class="annotation"> |
| 1918 |
|
| 1919 |
<div class="sswrap "> |
| 1920 |
<a class="ss" href="#section-98">§</a> |
| 1921 |
</div> |
| 1922 |
<p>A model is new if it has never been saved to the server, and lacks an id.</p> |
| 1923 |
|
| 1924 |
</div> |
| 1925 |
|
| 1926 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">isNew</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 1927 |
<span class="hljs-keyword">return</span> !<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">has</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">idAttribute</span>); |
| 1928 |
},</pre></div></div> |
| 1929 |
|
| 1930 |
</li> |
| 1931 |
|
| 1932 |
|
| 1933 |
<li id="section-99"> |
| 1934 |
<div class="annotation"> |
| 1935 |
|
| 1936 |
<div class="sswrap "> |
| 1937 |
<a class="ss" href="#section-99">§</a> |
| 1938 |
</div> |
| 1939 |
<p>Check if the model is currently in a valid state.</p> |
| 1940 |
|
| 1941 |
</div> |
| 1942 |
|
| 1943 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">isValid</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 1944 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_validate</span>({}, _.<span class="hljs-title function_">extend</span>({}, options, {<span class="hljs-attr">validate</span>: <span class="hljs-literal">true</span>})); |
| 1945 |
},</pre></div></div> |
| 1946 |
|
| 1947 |
</li> |
| 1948 |
|
| 1949 |
|
| 1950 |
<li id="section-100"> |
| 1951 |
<div class="annotation"> |
| 1952 |
|
| 1953 |
<div class="sswrap "> |
| 1954 |
<a class="ss" href="#section-100">§</a> |
| 1955 |
</div> |
| 1956 |
<p>Run validation against the next complete set of model attributes, |
| 1957 |
returning <code>true</code> if all is well. Otherwise, fire an <code>"invalid"</code> event.</p> |
| 1958 |
|
| 1959 |
</div> |
| 1960 |
|
| 1961 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_validate</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs, options</span>) { |
| 1962 |
<span class="hljs-keyword">if</span> (!options.<span class="hljs-property">validate</span> || !<span class="hljs-variable language_">this</span>.<span class="hljs-property">validate</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; |
| 1963 |
attrs = _.<span class="hljs-title function_">extend</span>({}, <span class="hljs-variable language_">this</span>.<span class="hljs-property">attributes</span>, attrs); |
| 1964 |
<span class="hljs-keyword">var</span> error = <span class="hljs-variable language_">this</span>.<span class="hljs-property">validationError</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">validate</span>(attrs, options) || <span class="hljs-literal">null</span>; |
| 1965 |
<span class="hljs-keyword">if</span> (!error) <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; |
| 1966 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'invalid'</span>, <span class="hljs-variable language_">this</span>, error, _.<span class="hljs-title function_">extend</span>(options, {<span class="hljs-attr">validationError</span>: error})); |
| 1967 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 1968 |
} |
| 1969 |
|
| 1970 |
});</pre></div></div> |
| 1971 |
|
| 1972 |
</li> |
| 1973 |
|
| 1974 |
|
| 1975 |
<li id="section-101"> |
| 1976 |
<div class="annotation"> |
| 1977 |
|
| 1978 |
<div class="sswrap "> |
| 1979 |
<a class="ss" href="#section-101">§</a> |
| 1980 |
</div> |
| 1981 |
<h2 id="backbonecollection">Backbone.Collection</h2> |
| 1982 |
|
| 1983 |
</div> |
| 1984 |
|
| 1985 |
</li> |
| 1986 |
|
| 1987 |
|
| 1988 |
<li id="section-102"> |
| 1989 |
<div class="annotation"> |
| 1990 |
|
| 1991 |
<div class="sswrap "> |
| 1992 |
<a class="ss" href="#section-102">§</a> |
| 1993 |
</div> |
| 1994 |
|
| 1995 |
</div> |
| 1996 |
|
| 1997 |
</li> |
| 1998 |
|
| 1999 |
|
| 2000 |
<li id="section-103"> |
| 2001 |
<div class="annotation"> |
| 2002 |
|
| 2003 |
<div class="sswrap "> |
| 2004 |
<a class="ss" href="#section-103">§</a> |
| 2005 |
</div> |
| 2006 |
<p>If models tend to represent a single row of data, a Backbone Collection is |
| 2007 |
more analogous to a table full of data … or a small slice or page of that |
| 2008 |
table, or a collection of rows that belong together for a particular reason |
| 2009 |
– all of the messages in this particular folder, all of the documents |
| 2010 |
belonging to this particular author, and so on. Collections maintain |
| 2011 |
indexes of their models, both in order, and for lookup by <code>id</code>.</p> |
| 2012 |
|
| 2013 |
</div> |
| 2014 |
|
| 2015 |
</li> |
| 2016 |
|
| 2017 |
|
| 2018 |
<li id="section-104"> |
| 2019 |
<div class="annotation"> |
| 2020 |
|
| 2021 |
<div class="sswrap "> |
| 2022 |
<a class="ss" href="#section-104">§</a> |
| 2023 |
</div> |
| 2024 |
<p>Create a new <strong>Collection</strong>, perhaps to contain a specific type of <code>model</code>. |
| 2025 |
If a <code>comparator</code> is specified, the Collection will maintain |
| 2026 |
its models in sort order, as they’re added and removed.</p> |
| 2027 |
|
| 2028 |
</div> |
| 2029 |
|
| 2030 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Collection</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Collection</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">models, options</span>) { |
| 2031 |
options || (options = {}); |
| 2032 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">preinitialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 2033 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">model</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span> = options.<span class="hljs-property">model</span>; |
| 2034 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">comparator</span> !== <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">comparator</span> = options.<span class="hljs-property">comparator</span>; |
| 2035 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_reset</span>(); |
| 2036 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">initialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 2037 |
<span class="hljs-keyword">if</span> (models) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">reset</span>(models, _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>}, options)); |
| 2038 |
};</pre></div></div> |
| 2039 |
|
| 2040 |
</li> |
| 2041 |
|
| 2042 |
|
| 2043 |
<li id="section-105"> |
| 2044 |
<div class="annotation"> |
| 2045 |
|
| 2046 |
<div class="sswrap "> |
| 2047 |
<a class="ss" href="#section-105">§</a> |
| 2048 |
</div> |
| 2049 |
<p>Default options for <code>Collection#set</code>.</p> |
| 2050 |
|
| 2051 |
</div> |
| 2052 |
|
| 2053 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> setOptions = {<span class="hljs-attr">add</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">remove</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">merge</span>: <span class="hljs-literal">true</span>}; |
| 2054 |
<span class="hljs-keyword">var</span> addOptions = {<span class="hljs-attr">add</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">remove</span>: <span class="hljs-literal">false</span>};</pre></div></div> |
| 2055 |
|
| 2056 |
</li> |
| 2057 |
|
| 2058 |
|
| 2059 |
<li id="section-106"> |
| 2060 |
<div class="annotation"> |
| 2061 |
|
| 2062 |
<div class="sswrap "> |
| 2063 |
<a class="ss" href="#section-106">§</a> |
| 2064 |
</div> |
| 2065 |
<p>Splices <code>insert</code> into <code>array</code> at index <code>at</code>.</p> |
| 2066 |
|
| 2067 |
</div> |
| 2068 |
|
| 2069 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> splice = <span class="hljs-keyword">function</span>(<span class="hljs-params">array, insert, at</span>) { |
| 2070 |
at = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">min</span>(<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">max</span>(at, <span class="hljs-number">0</span>), array.<span class="hljs-property">length</span>); |
| 2071 |
<span class="hljs-keyword">var</span> tail = <span class="hljs-title class_">Array</span>(array.<span class="hljs-property">length</span> - at); |
| 2072 |
<span class="hljs-keyword">var</span> length = insert.<span class="hljs-property">length</span>; |
| 2073 |
<span class="hljs-keyword">var</span> i; |
| 2074 |
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < tail.<span class="hljs-property">length</span>; i++) tail[i] = array[i + at]; |
| 2075 |
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < length; i++) array[i + at] = insert[i]; |
| 2076 |
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < tail.<span class="hljs-property">length</span>; i++) array[i + length + at] = tail[i]; |
| 2077 |
};</pre></div></div> |
| 2078 |
|
| 2079 |
</li> |
| 2080 |
|
| 2081 |
|
| 2082 |
<li id="section-107"> |
| 2083 |
<div class="annotation"> |
| 2084 |
|
| 2085 |
<div class="sswrap "> |
| 2086 |
<a class="ss" href="#section-107">§</a> |
| 2087 |
</div> |
| 2088 |
<p>Define the Collection’s inheritable methods.</p> |
| 2089 |
|
| 2090 |
</div> |
| 2091 |
|
| 2092 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">Collection</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, <span class="hljs-title class_">Events</span>, {</pre></div></div> |
| 2093 |
|
| 2094 |
</li> |
| 2095 |
|
| 2096 |
|
| 2097 |
<li id="section-108"> |
| 2098 |
<div class="annotation"> |
| 2099 |
|
| 2100 |
<div class="sswrap "> |
| 2101 |
<a class="ss" href="#section-108">§</a> |
| 2102 |
</div> |
| 2103 |
<p>The default model for a collection is just a <strong>Backbone.Model</strong>. |
| 2104 |
This should be overridden in most cases.</p> |
| 2105 |
|
| 2106 |
</div> |
| 2107 |
|
| 2108 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">model</span>: <span class="hljs-title class_">Model</span>,</pre></div></div> |
| 2109 |
|
| 2110 |
</li> |
| 2111 |
|
| 2112 |
|
| 2113 |
<li id="section-109"> |
| 2114 |
<div class="annotation"> |
| 2115 |
|
| 2116 |
<div class="sswrap "> |
| 2117 |
<a class="ss" href="#section-109">§</a> |
| 2118 |
</div> |
| 2119 |
<p>preinitialize is an empty function by default. You can override it with a function |
| 2120 |
or object. preinitialize will run before any instantiation logic is run in the Collection.</p> |
| 2121 |
|
| 2122 |
</div> |
| 2123 |
|
| 2124 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">preinitialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 2125 |
|
| 2126 |
</li> |
| 2127 |
|
| 2128 |
|
| 2129 |
<li id="section-110"> |
| 2130 |
<div class="annotation"> |
| 2131 |
|
| 2132 |
<div class="sswrap "> |
| 2133 |
<a class="ss" href="#section-110">§</a> |
| 2134 |
</div> |
| 2135 |
<p>Initialize is an empty function by default. Override it with your own |
| 2136 |
initialization logic.</p> |
| 2137 |
|
| 2138 |
</div> |
| 2139 |
|
| 2140 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">initialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 2141 |
|
| 2142 |
</li> |
| 2143 |
|
| 2144 |
|
| 2145 |
<li id="section-111"> |
| 2146 |
<div class="annotation"> |
| 2147 |
|
| 2148 |
<div class="sswrap "> |
| 2149 |
<a class="ss" href="#section-111">§</a> |
| 2150 |
</div> |
| 2151 |
<p>The JSON representation of a Collection is an array of the |
| 2152 |
models’ attributes.</p> |
| 2153 |
|
| 2154 |
</div> |
| 2155 |
|
| 2156 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">toJSON</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 2157 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">map</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { <span class="hljs-keyword">return</span> model.<span class="hljs-title function_">toJSON</span>(options); }); |
| 2158 |
},</pre></div></div> |
| 2159 |
|
| 2160 |
</li> |
| 2161 |
|
| 2162 |
|
| 2163 |
<li id="section-112"> |
| 2164 |
<div class="annotation"> |
| 2165 |
|
| 2166 |
<div class="sswrap "> |
| 2167 |
<a class="ss" href="#section-112">§</a> |
| 2168 |
</div> |
| 2169 |
<p>Proxy <code>Backbone.sync</code> by default.</p> |
| 2170 |
|
| 2171 |
</div> |
| 2172 |
|
| 2173 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">sync</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2174 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">sync</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 2175 |
},</pre></div></div> |
| 2176 |
|
| 2177 |
</li> |
| 2178 |
|
| 2179 |
|
| 2180 |
<li id="section-113"> |
| 2181 |
<div class="annotation"> |
| 2182 |
|
| 2183 |
<div class="sswrap "> |
| 2184 |
<a class="ss" href="#section-113">§</a> |
| 2185 |
</div> |
| 2186 |
<p>Add a model, or list of models to the set. <code>models</code> may be Backbone |
| 2187 |
Models or raw JavaScript objects to be converted to Models, or any |
| 2188 |
combination of the two.</p> |
| 2189 |
|
| 2190 |
</div> |
| 2191 |
|
| 2192 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">add</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">models, options</span>) { |
| 2193 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">set</span>(models, _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">merge</span>: <span class="hljs-literal">false</span>}, options, addOptions)); |
| 2194 |
},</pre></div></div> |
| 2195 |
|
| 2196 |
</li> |
| 2197 |
|
| 2198 |
|
| 2199 |
<li id="section-114"> |
| 2200 |
<div class="annotation"> |
| 2201 |
|
| 2202 |
<div class="sswrap "> |
| 2203 |
<a class="ss" href="#section-114">§</a> |
| 2204 |
</div> |
| 2205 |
<p>Remove a model, or a list of models from the set.</p> |
| 2206 |
|
| 2207 |
</div> |
| 2208 |
|
| 2209 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">remove</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">models, options</span>) { |
| 2210 |
options = _.<span class="hljs-title function_">extend</span>({}, options); |
| 2211 |
<span class="hljs-keyword">var</span> singular = !_.<span class="hljs-title function_">isArray</span>(models); |
| 2212 |
models = singular ? [models] : models.<span class="hljs-title function_">slice</span>(); |
| 2213 |
<span class="hljs-keyword">var</span> removed = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_removeModels</span>(models, options); |
| 2214 |
<span class="hljs-keyword">if</span> (!options.<span class="hljs-property">silent</span> && removed.<span class="hljs-property">length</span>) { |
| 2215 |
options.<span class="hljs-property">changes</span> = {<span class="hljs-attr">added</span>: [], <span class="hljs-attr">merged</span>: [], <span class="hljs-attr">removed</span>: removed}; |
| 2216 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'update'</span>, <span class="hljs-variable language_">this</span>, options); |
| 2217 |
} |
| 2218 |
<span class="hljs-keyword">return</span> singular ? removed[<span class="hljs-number">0</span>] : removed; |
| 2219 |
},</pre></div></div> |
| 2220 |
|
| 2221 |
</li> |
| 2222 |
|
| 2223 |
|
| 2224 |
<li id="section-115"> |
| 2225 |
<div class="annotation"> |
| 2226 |
|
| 2227 |
<div class="sswrap "> |
| 2228 |
<a class="ss" href="#section-115">§</a> |
| 2229 |
</div> |
| 2230 |
<p>Update a collection by <code>set</code>-ing a new list of models, adding new ones, |
| 2231 |
removing models that are no longer present, and merging models that |
| 2232 |
already exist in the collection, as necessary. Similar to <strong>Model#set</strong>, |
| 2233 |
the core operation for updating the data contained by the collection.</p> |
| 2234 |
|
| 2235 |
</div> |
| 2236 |
|
| 2237 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">set</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">models, options</span>) { |
| 2238 |
<span class="hljs-keyword">if</span> (models == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span>; |
| 2239 |
|
| 2240 |
options = _.<span class="hljs-title function_">extend</span>({}, setOptions, options); |
| 2241 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">parse</span> && !<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_isModel</span>(models)) { |
| 2242 |
models = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">parse</span>(models, options) || []; |
| 2243 |
} |
| 2244 |
|
| 2245 |
<span class="hljs-keyword">var</span> singular = !_.<span class="hljs-title function_">isArray</span>(models); |
| 2246 |
models = singular ? [models] : models.<span class="hljs-title function_">slice</span>(); |
| 2247 |
|
| 2248 |
<span class="hljs-keyword">var</span> at = options.<span class="hljs-property">at</span>; |
| 2249 |
<span class="hljs-keyword">if</span> (at != <span class="hljs-literal">null</span>) at = +at; |
| 2250 |
<span class="hljs-keyword">if</span> (at > <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>) at = <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>; |
| 2251 |
<span class="hljs-keyword">if</span> (at < <span class="hljs-number">0</span>) at += <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> + <span class="hljs-number">1</span>; |
| 2252 |
|
| 2253 |
<span class="hljs-keyword">var</span> set = []; |
| 2254 |
<span class="hljs-keyword">var</span> toAdd = []; |
| 2255 |
<span class="hljs-keyword">var</span> toMerge = []; |
| 2256 |
<span class="hljs-keyword">var</span> toRemove = []; |
| 2257 |
<span class="hljs-keyword">var</span> modelMap = {}; |
| 2258 |
|
| 2259 |
<span class="hljs-keyword">var</span> add = options.<span class="hljs-property">add</span>; |
| 2260 |
<span class="hljs-keyword">var</span> merge = options.<span class="hljs-property">merge</span>; |
| 2261 |
<span class="hljs-keyword">var</span> remove = options.<span class="hljs-property">remove</span>; |
| 2262 |
|
| 2263 |
<span class="hljs-keyword">var</span> sort = <span class="hljs-literal">false</span>; |
| 2264 |
<span class="hljs-keyword">var</span> sortable = <span class="hljs-variable language_">this</span>.<span class="hljs-property">comparator</span> && at == <span class="hljs-literal">null</span> && options.<span class="hljs-property">sort</span> !== <span class="hljs-literal">false</span>; |
| 2265 |
<span class="hljs-keyword">var</span> sortAttr = _.<span class="hljs-title function_">isString</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">comparator</span>) ? <span class="hljs-variable language_">this</span>.<span class="hljs-property">comparator</span> : <span class="hljs-literal">null</span>;</pre></div></div> |
| 2266 |
|
| 2267 |
</li> |
| 2268 |
|
| 2269 |
|
| 2270 |
<li id="section-116"> |
| 2271 |
<div class="annotation"> |
| 2272 |
|
| 2273 |
<div class="sswrap "> |
| 2274 |
<a class="ss" href="#section-116">§</a> |
| 2275 |
</div> |
| 2276 |
<p>Turn bare objects into model references, and prevent invalid models |
| 2277 |
from being added.</p> |
| 2278 |
|
| 2279 |
</div> |
| 2280 |
|
| 2281 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> model, i; |
| 2282 |
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < models.<span class="hljs-property">length</span>; i++) { |
| 2283 |
model = models[i];</pre></div></div> |
| 2284 |
|
| 2285 |
</li> |
| 2286 |
|
| 2287 |
|
| 2288 |
<li id="section-117"> |
| 2289 |
<div class="annotation"> |
| 2290 |
|
| 2291 |
<div class="sswrap "> |
| 2292 |
<a class="ss" href="#section-117">§</a> |
| 2293 |
</div> |
| 2294 |
<p>If a duplicate is found, prevent it from being added and |
| 2295 |
optionally merge it into the existing model.</p> |
| 2296 |
|
| 2297 |
</div> |
| 2298 |
|
| 2299 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> existing = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(model); |
| 2300 |
<span class="hljs-keyword">if</span> (existing) { |
| 2301 |
<span class="hljs-keyword">if</span> (merge && model !== existing) { |
| 2302 |
<span class="hljs-keyword">var</span> attrs = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_isModel</span>(model) ? model.<span class="hljs-property">attributes</span> : model; |
| 2303 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">parse</span>) attrs = existing.<span class="hljs-title function_">parse</span>(attrs, options); |
| 2304 |
existing.<span class="hljs-title function_">set</span>(attrs, options); |
| 2305 |
toMerge.<span class="hljs-title function_">push</span>(existing); |
| 2306 |
<span class="hljs-keyword">if</span> (sortable && !sort) sort = existing.<span class="hljs-title function_">hasChanged</span>(sortAttr); |
| 2307 |
} |
| 2308 |
<span class="hljs-keyword">if</span> (!modelMap[existing.<span class="hljs-property">cid</span>]) { |
| 2309 |
modelMap[existing.<span class="hljs-property">cid</span>] = <span class="hljs-literal">true</span>; |
| 2310 |
set.<span class="hljs-title function_">push</span>(existing); |
| 2311 |
} |
| 2312 |
models[i] = existing;</pre></div></div> |
| 2313 |
|
| 2314 |
</li> |
| 2315 |
|
| 2316 |
|
| 2317 |
<li id="section-118"> |
| 2318 |
<div class="annotation"> |
| 2319 |
|
| 2320 |
<div class="sswrap "> |
| 2321 |
<a class="ss" href="#section-118">§</a> |
| 2322 |
</div> |
| 2323 |
<p>If this is a new, valid model, push it to the <code>toAdd</code> list.</p> |
| 2324 |
|
| 2325 |
</div> |
| 2326 |
|
| 2327 |
<div class="content"><div class='highlight'><pre> } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (add) { |
| 2328 |
model = models[i] = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_prepareModel</span>(model, options); |
| 2329 |
<span class="hljs-keyword">if</span> (model) { |
| 2330 |
toAdd.<span class="hljs-title function_">push</span>(model); |
| 2331 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_addReference</span>(model, options); |
| 2332 |
modelMap[model.<span class="hljs-property">cid</span>] = <span class="hljs-literal">true</span>; |
| 2333 |
set.<span class="hljs-title function_">push</span>(model); |
| 2334 |
} |
| 2335 |
} |
| 2336 |
}</pre></div></div> |
| 2337 |
|
| 2338 |
</li> |
| 2339 |
|
| 2340 |
|
| 2341 |
<li id="section-119"> |
| 2342 |
<div class="annotation"> |
| 2343 |
|
| 2344 |
<div class="sswrap "> |
| 2345 |
<a class="ss" href="#section-119">§</a> |
| 2346 |
</div> |
| 2347 |
<p>Remove stale models.</p> |
| 2348 |
|
| 2349 |
</div> |
| 2350 |
|
| 2351 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (remove) { |
| 2352 |
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>; i++) { |
| 2353 |
model = <span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>[i]; |
| 2354 |
<span class="hljs-keyword">if</span> (!modelMap[model.<span class="hljs-property">cid</span>]) toRemove.<span class="hljs-title function_">push</span>(model); |
| 2355 |
} |
| 2356 |
<span class="hljs-keyword">if</span> (toRemove.<span class="hljs-property">length</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_removeModels</span>(toRemove, options); |
| 2357 |
}</pre></div></div> |
| 2358 |
|
| 2359 |
</li> |
| 2360 |
|
| 2361 |
|
| 2362 |
<li id="section-120"> |
| 2363 |
<div class="annotation"> |
| 2364 |
|
| 2365 |
<div class="sswrap "> |
| 2366 |
<a class="ss" href="#section-120">§</a> |
| 2367 |
</div> |
| 2368 |
<p>See if sorting is needed, update <code>length</code> and splice in new models.</p> |
| 2369 |
|
| 2370 |
</div> |
| 2371 |
|
| 2372 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> orderChanged = <span class="hljs-literal">false</span>; |
| 2373 |
<span class="hljs-keyword">var</span> replace = !sortable && add && remove; |
| 2374 |
<span class="hljs-keyword">if</span> (set.<span class="hljs-property">length</span> && replace) { |
| 2375 |
orderChanged = <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> !== set.<span class="hljs-property">length</span> || _.<span class="hljs-title function_">some</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">m, index</span>) { |
| 2376 |
<span class="hljs-keyword">return</span> m !== set[index]; |
| 2377 |
}); |
| 2378 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>.<span class="hljs-property">length</span> = <span class="hljs-number">0</span>; |
| 2379 |
<span class="hljs-title function_">splice</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>, set, <span class="hljs-number">0</span>); |
| 2380 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>.<span class="hljs-property">length</span>; |
| 2381 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (toAdd.<span class="hljs-property">length</span>) { |
| 2382 |
<span class="hljs-keyword">if</span> (sortable) sort = <span class="hljs-literal">true</span>; |
| 2383 |
<span class="hljs-title function_">splice</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>, toAdd, at == <span class="hljs-literal">null</span> ? <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> : at); |
| 2384 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>.<span class="hljs-property">length</span>; |
| 2385 |
}</pre></div></div> |
| 2386 |
|
| 2387 |
</li> |
| 2388 |
|
| 2389 |
|
| 2390 |
<li id="section-121"> |
| 2391 |
<div class="annotation"> |
| 2392 |
|
| 2393 |
<div class="sswrap "> |
| 2394 |
<a class="ss" href="#section-121">§</a> |
| 2395 |
</div> |
| 2396 |
<p>Silently sort the collection if appropriate.</p> |
| 2397 |
|
| 2398 |
</div> |
| 2399 |
|
| 2400 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (sort) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">sort</span>({<span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>});</pre></div></div> |
| 2401 |
|
| 2402 |
</li> |
| 2403 |
|
| 2404 |
|
| 2405 |
<li id="section-122"> |
| 2406 |
<div class="annotation"> |
| 2407 |
|
| 2408 |
<div class="sswrap "> |
| 2409 |
<a class="ss" href="#section-122">§</a> |
| 2410 |
</div> |
| 2411 |
<p>Unless silenced, it’s time to fire all appropriate add/sort/update events.</p> |
| 2412 |
|
| 2413 |
</div> |
| 2414 |
|
| 2415 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!options.<span class="hljs-property">silent</span>) { |
| 2416 |
<span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i < toAdd.<span class="hljs-property">length</span>; i++) { |
| 2417 |
<span class="hljs-keyword">if</span> (at != <span class="hljs-literal">null</span>) options.<span class="hljs-property">index</span> = at + i; |
| 2418 |
model = toAdd[i]; |
| 2419 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'add'</span>, model, <span class="hljs-variable language_">this</span>, options); |
| 2420 |
} |
| 2421 |
<span class="hljs-keyword">if</span> (sort || orderChanged) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'sort'</span>, <span class="hljs-variable language_">this</span>, options); |
| 2422 |
<span class="hljs-keyword">if</span> (toAdd.<span class="hljs-property">length</span> || toRemove.<span class="hljs-property">length</span> || toMerge.<span class="hljs-property">length</span>) { |
| 2423 |
options.<span class="hljs-property">changes</span> = { |
| 2424 |
<span class="hljs-attr">added</span>: toAdd, |
| 2425 |
<span class="hljs-attr">removed</span>: toRemove, |
| 2426 |
<span class="hljs-attr">merged</span>: toMerge |
| 2427 |
}; |
| 2428 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'update'</span>, <span class="hljs-variable language_">this</span>, options); |
| 2429 |
} |
| 2430 |
}</pre></div></div> |
| 2431 |
|
| 2432 |
</li> |
| 2433 |
|
| 2434 |
|
| 2435 |
<li id="section-123"> |
| 2436 |
<div class="annotation"> |
| 2437 |
|
| 2438 |
<div class="sswrap "> |
| 2439 |
<a class="ss" href="#section-123">§</a> |
| 2440 |
</div> |
| 2441 |
<p>Return the added (or merged) model (or models).</p> |
| 2442 |
|
| 2443 |
</div> |
| 2444 |
|
| 2445 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> singular ? models[<span class="hljs-number">0</span>] : models; |
| 2446 |
},</pre></div></div> |
| 2447 |
|
| 2448 |
</li> |
| 2449 |
|
| 2450 |
|
| 2451 |
<li id="section-124"> |
| 2452 |
<div class="annotation"> |
| 2453 |
|
| 2454 |
<div class="sswrap "> |
| 2455 |
<a class="ss" href="#section-124">§</a> |
| 2456 |
</div> |
| 2457 |
<p>When you have more items than you want to add or remove individually, |
| 2458 |
you can reset the entire set with a new list of models, without firing |
| 2459 |
any granular <code>add</code> or <code>remove</code> events. Fires <code>reset</code> when finished. |
| 2460 |
Useful for bulk operations and optimizations.</p> |
| 2461 |
|
| 2462 |
</div> |
| 2463 |
|
| 2464 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">reset</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">models, options</span>) { |
| 2465 |
options = options ? _.<span class="hljs-title function_">clone</span>(options) : {}; |
| 2466 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>.<span class="hljs-property">length</span>; i++) { |
| 2467 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_removeReference</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>[i], options); |
| 2468 |
} |
| 2469 |
options.<span class="hljs-property">previousModels</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>; |
| 2470 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_reset</span>(); |
| 2471 |
models = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">add</span>(models, _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">silent</span>: <span class="hljs-literal">true</span>}, options)); |
| 2472 |
<span class="hljs-keyword">if</span> (!options.<span class="hljs-property">silent</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'reset'</span>, <span class="hljs-variable language_">this</span>, options); |
| 2473 |
<span class="hljs-keyword">return</span> models; |
| 2474 |
},</pre></div></div> |
| 2475 |
|
| 2476 |
</li> |
| 2477 |
|
| 2478 |
|
| 2479 |
<li id="section-125"> |
| 2480 |
<div class="annotation"> |
| 2481 |
|
| 2482 |
<div class="sswrap "> |
| 2483 |
<a class="ss" href="#section-125">§</a> |
| 2484 |
</div> |
| 2485 |
<p>Add a model to the end of the collection.</p> |
| 2486 |
|
| 2487 |
</div> |
| 2488 |
|
| 2489 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">push</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model, options</span>) { |
| 2490 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">add</span>(model, _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">at</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>}, options)); |
| 2491 |
},</pre></div></div> |
| 2492 |
|
| 2493 |
</li> |
| 2494 |
|
| 2495 |
|
| 2496 |
<li id="section-126"> |
| 2497 |
<div class="annotation"> |
| 2498 |
|
| 2499 |
<div class="sswrap "> |
| 2500 |
<a class="ss" href="#section-126">§</a> |
| 2501 |
</div> |
| 2502 |
<p>Remove a model from the end of the collection.</p> |
| 2503 |
|
| 2504 |
</div> |
| 2505 |
|
| 2506 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">pop</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 2507 |
<span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">at</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> - <span class="hljs-number">1</span>); |
| 2508 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">remove</span>(model, options); |
| 2509 |
},</pre></div></div> |
| 2510 |
|
| 2511 |
</li> |
| 2512 |
|
| 2513 |
|
| 2514 |
<li id="section-127"> |
| 2515 |
<div class="annotation"> |
| 2516 |
|
| 2517 |
<div class="sswrap "> |
| 2518 |
<a class="ss" href="#section-127">§</a> |
| 2519 |
</div> |
| 2520 |
<p>Add a model to the beginning of the collection.</p> |
| 2521 |
|
| 2522 |
</div> |
| 2523 |
|
| 2524 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">unshift</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model, options</span>) { |
| 2525 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">add</span>(model, _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">at</span>: <span class="hljs-number">0</span>}, options)); |
| 2526 |
},</pre></div></div> |
| 2527 |
|
| 2528 |
</li> |
| 2529 |
|
| 2530 |
|
| 2531 |
<li id="section-128"> |
| 2532 |
<div class="annotation"> |
| 2533 |
|
| 2534 |
<div class="sswrap "> |
| 2535 |
<a class="ss" href="#section-128">§</a> |
| 2536 |
</div> |
| 2537 |
<p>Remove a model from the beginning of the collection.</p> |
| 2538 |
|
| 2539 |
</div> |
| 2540 |
|
| 2541 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">shift</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 2542 |
<span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">at</span>(<span class="hljs-number">0</span>); |
| 2543 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">remove</span>(model, options); |
| 2544 |
},</pre></div></div> |
| 2545 |
|
| 2546 |
</li> |
| 2547 |
|
| 2548 |
|
| 2549 |
<li id="section-129"> |
| 2550 |
<div class="annotation"> |
| 2551 |
|
| 2552 |
<div class="sswrap "> |
| 2553 |
<a class="ss" href="#section-129">§</a> |
| 2554 |
</div> |
| 2555 |
<p>Slice out a sub-array of models from the collection.</p> |
| 2556 |
|
| 2557 |
</div> |
| 2558 |
|
| 2559 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">slice</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2560 |
<span class="hljs-keyword">return</span> slice.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>, <span class="hljs-variable language_">arguments</span>); |
| 2561 |
},</pre></div></div> |
| 2562 |
|
| 2563 |
</li> |
| 2564 |
|
| 2565 |
|
| 2566 |
<li id="section-130"> |
| 2567 |
<div class="annotation"> |
| 2568 |
|
| 2569 |
<div class="sswrap "> |
| 2570 |
<a class="ss" href="#section-130">§</a> |
| 2571 |
</div> |
| 2572 |
<p>Get a model from the set by id, cid, model object with id or cid |
| 2573 |
properties, or an attributes object that is transformed through modelId.</p> |
| 2574 |
|
| 2575 |
</div> |
| 2576 |
|
| 2577 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">get</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>) { |
| 2578 |
<span class="hljs-keyword">if</span> (obj == <span class="hljs-literal">null</span>) <span class="hljs-keyword">return</span> <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 2579 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[obj] || |
| 2580 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">modelId</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_isModel</span>(obj) ? obj.<span class="hljs-property">attributes</span> : obj, obj.<span class="hljs-property">idAttribute</span>)] || |
| 2581 |
obj.<span class="hljs-property">cid</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[obj.<span class="hljs-property">cid</span>]; |
| 2582 |
},</pre></div></div> |
| 2583 |
|
| 2584 |
</li> |
| 2585 |
|
| 2586 |
|
| 2587 |
<li id="section-131"> |
| 2588 |
<div class="annotation"> |
| 2589 |
|
| 2590 |
<div class="sswrap "> |
| 2591 |
<a class="ss" href="#section-131">§</a> |
| 2592 |
</div> |
| 2593 |
<p>Returns <code>true</code> if the model is in the collection.</p> |
| 2594 |
|
| 2595 |
</div> |
| 2596 |
|
| 2597 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">has</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>) { |
| 2598 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(obj) != <span class="hljs-literal">null</span>; |
| 2599 |
},</pre></div></div> |
| 2600 |
|
| 2601 |
</li> |
| 2602 |
|
| 2603 |
|
| 2604 |
<li id="section-132"> |
| 2605 |
<div class="annotation"> |
| 2606 |
|
| 2607 |
<div class="sswrap "> |
| 2608 |
<a class="ss" href="#section-132">§</a> |
| 2609 |
</div> |
| 2610 |
<p>Get the model at the given index.</p> |
| 2611 |
|
| 2612 |
</div> |
| 2613 |
|
| 2614 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">at</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">index</span>) { |
| 2615 |
<span class="hljs-keyword">if</span> (index < <span class="hljs-number">0</span>) index += <span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>; |
| 2616 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>[index]; |
| 2617 |
},</pre></div></div> |
| 2618 |
|
| 2619 |
</li> |
| 2620 |
|
| 2621 |
|
| 2622 |
<li id="section-133"> |
| 2623 |
<div class="annotation"> |
| 2624 |
|
| 2625 |
<div class="sswrap "> |
| 2626 |
<a class="ss" href="#section-133">§</a> |
| 2627 |
</div> |
| 2628 |
<p>Return models with matching attributes. Useful for simple cases of |
| 2629 |
<code>filter</code>.</p> |
| 2630 |
|
| 2631 |
</div> |
| 2632 |
|
| 2633 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">where</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs, first</span>) { |
| 2634 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>[first ? <span class="hljs-string">'find'</span> : <span class="hljs-string">'filter'</span>](attrs); |
| 2635 |
},</pre></div></div> |
| 2636 |
|
| 2637 |
</li> |
| 2638 |
|
| 2639 |
|
| 2640 |
<li id="section-134"> |
| 2641 |
<div class="annotation"> |
| 2642 |
|
| 2643 |
<div class="sswrap "> |
| 2644 |
<a class="ss" href="#section-134">§</a> |
| 2645 |
</div> |
| 2646 |
<p>Return the first model with matching attributes. Useful for simple cases |
| 2647 |
of <code>find</code>.</p> |
| 2648 |
|
| 2649 |
</div> |
| 2650 |
|
| 2651 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">findWhere</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs</span>) { |
| 2652 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">where</span>(attrs, <span class="hljs-literal">true</span>); |
| 2653 |
},</pre></div></div> |
| 2654 |
|
| 2655 |
</li> |
| 2656 |
|
| 2657 |
|
| 2658 |
<li id="section-135"> |
| 2659 |
<div class="annotation"> |
| 2660 |
|
| 2661 |
<div class="sswrap "> |
| 2662 |
<a class="ss" href="#section-135">§</a> |
| 2663 |
</div> |
| 2664 |
<p>Force the collection to re-sort itself. You don’t need to call this under |
| 2665 |
normal circumstances, as the set will maintain sort order as each item |
| 2666 |
is added.</p> |
| 2667 |
|
| 2668 |
</div> |
| 2669 |
|
| 2670 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">sort</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 2671 |
<span class="hljs-keyword">var</span> comparator = <span class="hljs-variable language_">this</span>.<span class="hljs-property">comparator</span>; |
| 2672 |
<span class="hljs-keyword">if</span> (!comparator) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Cannot sort a set without a comparator'</span>); |
| 2673 |
options || (options = {}); |
| 2674 |
|
| 2675 |
<span class="hljs-keyword">var</span> length = comparator.<span class="hljs-property">length</span>; |
| 2676 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isFunction</span>(comparator)) comparator = comparator.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>);</pre></div></div> |
| 2677 |
|
| 2678 |
</li> |
| 2679 |
|
| 2680 |
|
| 2681 |
<li id="section-136"> |
| 2682 |
<div class="annotation"> |
| 2683 |
|
| 2684 |
<div class="sswrap "> |
| 2685 |
<a class="ss" href="#section-136">§</a> |
| 2686 |
</div> |
| 2687 |
<p>Run sort based on type of <code>comparator</code>.</p> |
| 2688 |
|
| 2689 |
</div> |
| 2690 |
|
| 2691 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (length === <span class="hljs-number">1</span> || _.<span class="hljs-title function_">isString</span>(comparator)) { |
| 2692 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">sortBy</span>(comparator); |
| 2693 |
} <span class="hljs-keyword">else</span> { |
| 2694 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>.<span class="hljs-title function_">sort</span>(comparator); |
| 2695 |
} |
| 2696 |
<span class="hljs-keyword">if</span> (!options.<span class="hljs-property">silent</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'sort'</span>, <span class="hljs-variable language_">this</span>, options); |
| 2697 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 2698 |
},</pre></div></div> |
| 2699 |
|
| 2700 |
</li> |
| 2701 |
|
| 2702 |
|
| 2703 |
<li id="section-137"> |
| 2704 |
<div class="annotation"> |
| 2705 |
|
| 2706 |
<div class="sswrap "> |
| 2707 |
<a class="ss" href="#section-137">§</a> |
| 2708 |
</div> |
| 2709 |
<p>Pluck an attribute from each model in the collection.</p> |
| 2710 |
|
| 2711 |
</div> |
| 2712 |
|
| 2713 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">pluck</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attr</span>) { |
| 2714 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">map</span>(attr + <span class="hljs-string">''</span>); |
| 2715 |
},</pre></div></div> |
| 2716 |
|
| 2717 |
</li> |
| 2718 |
|
| 2719 |
|
| 2720 |
<li id="section-138"> |
| 2721 |
<div class="annotation"> |
| 2722 |
|
| 2723 |
<div class="sswrap "> |
| 2724 |
<a class="ss" href="#section-138">§</a> |
| 2725 |
</div> |
| 2726 |
<p>Fetch the default set of models for this collection, resetting the |
| 2727 |
collection when they arrive. If <code>reset: true</code> is passed, the response |
| 2728 |
data will be passed through the <code>reset</code> method instead of <code>set</code>.</p> |
| 2729 |
|
| 2730 |
</div> |
| 2731 |
|
| 2732 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">fetch</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 2733 |
options = _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">parse</span>: <span class="hljs-literal">true</span>}, options); |
| 2734 |
<span class="hljs-keyword">var</span> success = options.<span class="hljs-property">success</span>; |
| 2735 |
<span class="hljs-keyword">var</span> collection = <span class="hljs-variable language_">this</span>; |
| 2736 |
options.<span class="hljs-property">success</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) { |
| 2737 |
<span class="hljs-keyword">var</span> method = options.<span class="hljs-property">reset</span> ? <span class="hljs-string">'reset'</span> : <span class="hljs-string">'set'</span>; |
| 2738 |
collection[method](resp, options); |
| 2739 |
<span class="hljs-keyword">if</span> (success) success.<span class="hljs-title function_">call</span>(options.<span class="hljs-property">context</span>, collection, resp, options); |
| 2740 |
collection.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'sync'</span>, collection, resp, options); |
| 2741 |
}; |
| 2742 |
<span class="hljs-title function_">wrapError</span>(<span class="hljs-variable language_">this</span>, options); |
| 2743 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">sync</span>(<span class="hljs-string">'read'</span>, <span class="hljs-variable language_">this</span>, options); |
| 2744 |
},</pre></div></div> |
| 2745 |
|
| 2746 |
</li> |
| 2747 |
|
| 2748 |
|
| 2749 |
<li id="section-139"> |
| 2750 |
<div class="annotation"> |
| 2751 |
|
| 2752 |
<div class="sswrap "> |
| 2753 |
<a class="ss" href="#section-139">§</a> |
| 2754 |
</div> |
| 2755 |
<p>Create a new instance of a model in this collection. Add the model to the |
| 2756 |
collection immediately, unless <code>wait: true</code> is passed, in which case we |
| 2757 |
wait for the server to agree.</p> |
| 2758 |
|
| 2759 |
</div> |
| 2760 |
|
| 2761 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">create</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model, options</span>) { |
| 2762 |
options = options ? _.<span class="hljs-title function_">clone</span>(options) : {}; |
| 2763 |
<span class="hljs-keyword">var</span> wait = options.<span class="hljs-property">wait</span>; |
| 2764 |
model = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_prepareModel</span>(model, options); |
| 2765 |
<span class="hljs-keyword">if</span> (!model) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 2766 |
<span class="hljs-keyword">if</span> (!wait) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">add</span>(model, options); |
| 2767 |
<span class="hljs-keyword">var</span> collection = <span class="hljs-variable language_">this</span>; |
| 2768 |
<span class="hljs-keyword">var</span> success = options.<span class="hljs-property">success</span>; |
| 2769 |
options.<span class="hljs-property">success</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">m, resp, callbackOpts</span>) { |
| 2770 |
<span class="hljs-keyword">if</span> (wait) collection.<span class="hljs-title function_">add</span>(m, callbackOpts); |
| 2771 |
<span class="hljs-keyword">if</span> (success) success.<span class="hljs-title function_">call</span>(callbackOpts.<span class="hljs-property">context</span>, m, resp, callbackOpts); |
| 2772 |
}; |
| 2773 |
model.<span class="hljs-title function_">save</span>(<span class="hljs-literal">null</span>, options); |
| 2774 |
<span class="hljs-keyword">return</span> model; |
| 2775 |
},</pre></div></div> |
| 2776 |
|
| 2777 |
</li> |
| 2778 |
|
| 2779 |
|
| 2780 |
<li id="section-140"> |
| 2781 |
<div class="annotation"> |
| 2782 |
|
| 2783 |
<div class="sswrap "> |
| 2784 |
<a class="ss" href="#section-140">§</a> |
| 2785 |
</div> |
| 2786 |
<p><strong>parse</strong> converts a response into a list of models to be added to the |
| 2787 |
collection. The default implementation is just to pass it through.</p> |
| 2788 |
|
| 2789 |
</div> |
| 2790 |
|
| 2791 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">parse</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">resp, options</span>) { |
| 2792 |
<span class="hljs-keyword">return</span> resp; |
| 2793 |
},</pre></div></div> |
| 2794 |
|
| 2795 |
</li> |
| 2796 |
|
| 2797 |
|
| 2798 |
<li id="section-141"> |
| 2799 |
<div class="annotation"> |
| 2800 |
|
| 2801 |
<div class="sswrap "> |
| 2802 |
<a class="ss" href="#section-141">§</a> |
| 2803 |
</div> |
| 2804 |
<p>Create a new collection with an identical list of models as this one.</p> |
| 2805 |
|
| 2806 |
</div> |
| 2807 |
|
| 2808 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">clone</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2809 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">constructor</span>(<span class="hljs-params"><span class="hljs-variable language_">this</span>.models, { |
| 2810 |
model: <span class="hljs-variable language_">this</span>.model, |
| 2811 |
comparator: <span class="hljs-variable language_">this</span>.comparator |
| 2812 |
}</span>); |
| 2813 |
},</pre></div></div> |
| 2814 |
|
| 2815 |
</li> |
| 2816 |
|
| 2817 |
|
| 2818 |
<li id="section-142"> |
| 2819 |
<div class="annotation"> |
| 2820 |
|
| 2821 |
<div class="sswrap "> |
| 2822 |
<a class="ss" href="#section-142">§</a> |
| 2823 |
</div> |
| 2824 |
<p>Define how to uniquely identify models in the collection.</p> |
| 2825 |
|
| 2826 |
</div> |
| 2827 |
|
| 2828 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">modelId</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs, idAttribute</span>) { |
| 2829 |
<span class="hljs-keyword">return</span> attrs[idAttribute || <span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">idAttribute</span> || <span class="hljs-string">'id'</span>]; |
| 2830 |
},</pre></div></div> |
| 2831 |
|
| 2832 |
</li> |
| 2833 |
|
| 2834 |
|
| 2835 |
<li id="section-143"> |
| 2836 |
<div class="annotation"> |
| 2837 |
|
| 2838 |
<div class="sswrap "> |
| 2839 |
<a class="ss" href="#section-143">§</a> |
| 2840 |
</div> |
| 2841 |
<p>Get an iterator of all models in this collection.</p> |
| 2842 |
|
| 2843 |
</div> |
| 2844 |
|
| 2845 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">values</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2846 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CollectionIterator</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable constant_">ITERATOR_VALUES</span>); |
| 2847 |
},</pre></div></div> |
| 2848 |
|
| 2849 |
</li> |
| 2850 |
|
| 2851 |
|
| 2852 |
<li id="section-144"> |
| 2853 |
<div class="annotation"> |
| 2854 |
|
| 2855 |
<div class="sswrap "> |
| 2856 |
<a class="ss" href="#section-144">§</a> |
| 2857 |
</div> |
| 2858 |
<p>Get an iterator of all model IDs in this collection.</p> |
| 2859 |
|
| 2860 |
</div> |
| 2861 |
|
| 2862 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">keys</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2863 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CollectionIterator</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable constant_">ITERATOR_KEYS</span>); |
| 2864 |
},</pre></div></div> |
| 2865 |
|
| 2866 |
</li> |
| 2867 |
|
| 2868 |
|
| 2869 |
<li id="section-145"> |
| 2870 |
<div class="annotation"> |
| 2871 |
|
| 2872 |
<div class="sswrap "> |
| 2873 |
<a class="ss" href="#section-145">§</a> |
| 2874 |
</div> |
| 2875 |
<p>Get an iterator of all [ID, model] tuples in this collection.</p> |
| 2876 |
|
| 2877 |
</div> |
| 2878 |
|
| 2879 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">entries</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2880 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">CollectionIterator</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable constant_">ITERATOR_KEYSVALUES</span>); |
| 2881 |
},</pre></div></div> |
| 2882 |
|
| 2883 |
</li> |
| 2884 |
|
| 2885 |
|
| 2886 |
<li id="section-146"> |
| 2887 |
<div class="annotation"> |
| 2888 |
|
| 2889 |
<div class="sswrap "> |
| 2890 |
<a class="ss" href="#section-146">§</a> |
| 2891 |
</div> |
| 2892 |
<p>Private method to reset all internal state. Called when the collection |
| 2893 |
is first initialized or reset.</p> |
| 2894 |
|
| 2895 |
</div> |
| 2896 |
|
| 2897 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_reset</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 2898 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span> = <span class="hljs-number">0</span>; |
| 2899 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span> = []; |
| 2900 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span> = {}; |
| 2901 |
},</pre></div></div> |
| 2902 |
|
| 2903 |
</li> |
| 2904 |
|
| 2905 |
|
| 2906 |
<li id="section-147"> |
| 2907 |
<div class="annotation"> |
| 2908 |
|
| 2909 |
<div class="sswrap "> |
| 2910 |
<a class="ss" href="#section-147">§</a> |
| 2911 |
</div> |
| 2912 |
<p>Prepare a hash of attributes (or other model) to be added to this |
| 2913 |
collection.</p> |
| 2914 |
|
| 2915 |
</div> |
| 2916 |
|
| 2917 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_prepareModel</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs, options</span>) { |
| 2918 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_isModel</span>(attrs)) { |
| 2919 |
<span class="hljs-keyword">if</span> (!attrs.<span class="hljs-property">collection</span>) attrs.<span class="hljs-property">collection</span> = <span class="hljs-variable language_">this</span>; |
| 2920 |
<span class="hljs-keyword">return</span> attrs; |
| 2921 |
} |
| 2922 |
options = options ? _.<span class="hljs-title function_">clone</span>(options) : {}; |
| 2923 |
options.<span class="hljs-property">collection</span> = <span class="hljs-variable language_">this</span>; |
| 2924 |
|
| 2925 |
<span class="hljs-keyword">var</span> model; |
| 2926 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">model</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>) { |
| 2927 |
model = <span class="hljs-keyword">new</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">model</span>(attrs, options); |
| 2928 |
} <span class="hljs-keyword">else</span> {</pre></div></div> |
| 2929 |
|
| 2930 |
</li> |
| 2931 |
|
| 2932 |
|
| 2933 |
<li id="section-148"> |
| 2934 |
<div class="annotation"> |
| 2935 |
|
| 2936 |
<div class="sswrap "> |
| 2937 |
<a class="ss" href="#section-148">§</a> |
| 2938 |
</div> |
| 2939 |
<p>ES class methods didn’t have prototype</p> |
| 2940 |
|
| 2941 |
</div> |
| 2942 |
|
| 2943 |
<div class="content"><div class='highlight'><pre> model = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">model</span>(attrs, options); |
| 2944 |
} |
| 2945 |
|
| 2946 |
<span class="hljs-keyword">if</span> (!model.<span class="hljs-property">validationError</span>) <span class="hljs-keyword">return</span> model; |
| 2947 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'invalid'</span>, <span class="hljs-variable language_">this</span>, model.<span class="hljs-property">validationError</span>, options); |
| 2948 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 2949 |
},</pre></div></div> |
| 2950 |
|
| 2951 |
</li> |
| 2952 |
|
| 2953 |
|
| 2954 |
<li id="section-149"> |
| 2955 |
<div class="annotation"> |
| 2956 |
|
| 2957 |
<div class="sswrap "> |
| 2958 |
<a class="ss" href="#section-149">§</a> |
| 2959 |
</div> |
| 2960 |
<p>Internal method called by both remove and set.</p> |
| 2961 |
|
| 2962 |
</div> |
| 2963 |
|
| 2964 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_removeModels</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">models, options</span>) { |
| 2965 |
<span class="hljs-keyword">var</span> removed = []; |
| 2966 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < models.<span class="hljs-property">length</span>; i++) { |
| 2967 |
<span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">get</span>(models[i]); |
| 2968 |
<span class="hljs-keyword">if</span> (!model) <span class="hljs-keyword">continue</span>; |
| 2969 |
|
| 2970 |
<span class="hljs-keyword">var</span> index = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">indexOf</span>(model); |
| 2971 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">models</span>.<span class="hljs-title function_">splice</span>(index, <span class="hljs-number">1</span>); |
| 2972 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">length</span>--;</pre></div></div> |
| 2973 |
|
| 2974 |
</li> |
| 2975 |
|
| 2976 |
|
| 2977 |
<li id="section-150"> |
| 2978 |
<div class="annotation"> |
| 2979 |
|
| 2980 |
<div class="sswrap "> |
| 2981 |
<a class="ss" href="#section-150">§</a> |
| 2982 |
</div> |
| 2983 |
<p>Remove references before triggering ‘remove’ event to prevent an |
| 2984 |
infinite loop. #3693</p> |
| 2985 |
|
| 2986 |
</div> |
| 2987 |
|
| 2988 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[model.<span class="hljs-property">cid</span>]; |
| 2989 |
<span class="hljs-keyword">var</span> id = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">modelId</span>(model.<span class="hljs-property">attributes</span>, model.<span class="hljs-property">idAttribute</span>); |
| 2990 |
<span class="hljs-keyword">if</span> (id != <span class="hljs-literal">null</span>) <span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[id]; |
| 2991 |
|
| 2992 |
<span class="hljs-keyword">if</span> (!options.<span class="hljs-property">silent</span>) { |
| 2993 |
options.<span class="hljs-property">index</span> = index; |
| 2994 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'remove'</span>, model, <span class="hljs-variable language_">this</span>, options); |
| 2995 |
} |
| 2996 |
|
| 2997 |
removed.<span class="hljs-title function_">push</span>(model); |
| 2998 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_removeReference</span>(model, options); |
| 2999 |
} |
| 3000 |
<span class="hljs-keyword">return</span> removed; |
| 3001 |
},</pre></div></div> |
| 3002 |
|
| 3003 |
</li> |
| 3004 |
|
| 3005 |
|
| 3006 |
<li id="section-151"> |
| 3007 |
<div class="annotation"> |
| 3008 |
|
| 3009 |
<div class="sswrap "> |
| 3010 |
<a class="ss" href="#section-151">§</a> |
| 3011 |
</div> |
| 3012 |
<p>Method for checking whether an object should be considered a model for |
| 3013 |
the purposes of adding to the collection.</p> |
| 3014 |
|
| 3015 |
</div> |
| 3016 |
|
| 3017 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_isModel</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 3018 |
<span class="hljs-keyword">return</span> model <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Model</span>; |
| 3019 |
},</pre></div></div> |
| 3020 |
|
| 3021 |
</li> |
| 3022 |
|
| 3023 |
|
| 3024 |
<li id="section-152"> |
| 3025 |
<div class="annotation"> |
| 3026 |
|
| 3027 |
<div class="sswrap "> |
| 3028 |
<a class="ss" href="#section-152">§</a> |
| 3029 |
</div> |
| 3030 |
<p>Internal method to create a model’s ties to a collection.</p> |
| 3031 |
|
| 3032 |
</div> |
| 3033 |
|
| 3034 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_addReference</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model, options</span>) { |
| 3035 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[model.<span class="hljs-property">cid</span>] = model; |
| 3036 |
<span class="hljs-keyword">var</span> id = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">modelId</span>(model.<span class="hljs-property">attributes</span>, model.<span class="hljs-property">idAttribute</span>); |
| 3037 |
<span class="hljs-keyword">if</span> (id != <span class="hljs-literal">null</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[id] = model; |
| 3038 |
model.<span class="hljs-title function_">on</span>(<span class="hljs-string">'all'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">_onModelEvent</span>, <span class="hljs-variable language_">this</span>); |
| 3039 |
},</pre></div></div> |
| 3040 |
|
| 3041 |
</li> |
| 3042 |
|
| 3043 |
|
| 3044 |
<li id="section-153"> |
| 3045 |
<div class="annotation"> |
| 3046 |
|
| 3047 |
<div class="sswrap "> |
| 3048 |
<a class="ss" href="#section-153">§</a> |
| 3049 |
</div> |
| 3050 |
<p>Internal method to sever a model’s ties to a collection.</p> |
| 3051 |
|
| 3052 |
</div> |
| 3053 |
|
| 3054 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_removeReference</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model, options</span>) { |
| 3055 |
<span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[model.<span class="hljs-property">cid</span>]; |
| 3056 |
<span class="hljs-keyword">var</span> id = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">modelId</span>(model.<span class="hljs-property">attributes</span>, model.<span class="hljs-property">idAttribute</span>); |
| 3057 |
<span class="hljs-keyword">if</span> (id != <span class="hljs-literal">null</span>) <span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[id]; |
| 3058 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span> === model.<span class="hljs-property">collection</span>) <span class="hljs-keyword">delete</span> model.<span class="hljs-property">collection</span>; |
| 3059 |
model.<span class="hljs-title function_">off</span>(<span class="hljs-string">'all'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">_onModelEvent</span>, <span class="hljs-variable language_">this</span>); |
| 3060 |
},</pre></div></div> |
| 3061 |
|
| 3062 |
</li> |
| 3063 |
|
| 3064 |
|
| 3065 |
<li id="section-154"> |
| 3066 |
<div class="annotation"> |
| 3067 |
|
| 3068 |
<div class="sswrap "> |
| 3069 |
<a class="ss" href="#section-154">§</a> |
| 3070 |
</div> |
| 3071 |
<p>Internal method called every time a model in the set fires an event. |
| 3072 |
Sets need to update their indexes when models change ids. All other |
| 3073 |
events simply proxy through. “add” and “remove” events that originate |
| 3074 |
in other collections are ignored.</p> |
| 3075 |
|
| 3076 |
</div> |
| 3077 |
|
| 3078 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_onModelEvent</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">event, model, collection, options</span>) { |
| 3079 |
<span class="hljs-keyword">if</span> (model) { |
| 3080 |
<span class="hljs-keyword">if</span> ((event === <span class="hljs-string">'add'</span> || event === <span class="hljs-string">'remove'</span>) && collection !== <span class="hljs-variable language_">this</span>) <span class="hljs-keyword">return</span>; |
| 3081 |
<span class="hljs-keyword">if</span> (event === <span class="hljs-string">'destroy'</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">remove</span>(model, options); |
| 3082 |
<span class="hljs-keyword">if</span> (event === <span class="hljs-string">'changeId'</span>) { |
| 3083 |
<span class="hljs-keyword">var</span> prevId = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">modelId</span>(model.<span class="hljs-title function_">previousAttributes</span>(), model.<span class="hljs-property">idAttribute</span>); |
| 3084 |
<span class="hljs-keyword">var</span> id = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">modelId</span>(model.<span class="hljs-property">attributes</span>, model.<span class="hljs-property">idAttribute</span>); |
| 3085 |
<span class="hljs-keyword">if</span> (prevId != <span class="hljs-literal">null</span>) <span class="hljs-keyword">delete</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[prevId]; |
| 3086 |
<span class="hljs-keyword">if</span> (id != <span class="hljs-literal">null</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">_byId</span>[id] = model; |
| 3087 |
} |
| 3088 |
} |
| 3089 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">trigger</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 3090 |
} |
| 3091 |
|
| 3092 |
});</pre></div></div> |
| 3093 |
|
| 3094 |
</li> |
| 3095 |
|
| 3096 |
|
| 3097 |
<li id="section-155"> |
| 3098 |
<div class="annotation"> |
| 3099 |
|
| 3100 |
<div class="sswrap "> |
| 3101 |
<a class="ss" href="#section-155">§</a> |
| 3102 |
</div> |
| 3103 |
<p>Defining an @@iterator method implements JavaScript’s Iterable protocol. |
| 3104 |
In modern ES2015 browsers, this value is found at Symbol.iterator.</p> |
| 3105 |
|
| 3106 |
</div> |
| 3107 |
|
| 3108 |
<div class="content"><div class='highlight'><pre> <span class="hljs-comment">/* global Symbol */</span> |
| 3109 |
<span class="hljs-keyword">var</span> $$iterator = <span class="hljs-keyword">typeof</span> <span class="hljs-title class_">Symbol</span> === <span class="hljs-string">'function'</span> && <span class="hljs-title class_">Symbol</span>.<span class="hljs-property">iterator</span>; |
| 3110 |
<span class="hljs-keyword">if</span> ($$iterator) { |
| 3111 |
<span class="hljs-title class_">Collection</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>[$$iterator] = <span class="hljs-title class_">Collection</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">values</span>; |
| 3112 |
}</pre></div></div> |
| 3113 |
|
| 3114 |
</li> |
| 3115 |
|
| 3116 |
|
| 3117 |
<li id="section-156"> |
| 3118 |
<div class="annotation"> |
| 3119 |
|
| 3120 |
<div class="sswrap "> |
| 3121 |
<a class="ss" href="#section-156">§</a> |
| 3122 |
</div> |
| 3123 |
<h2 id="collectioniterator">CollectionIterator</h2> |
| 3124 |
|
| 3125 |
</div> |
| 3126 |
|
| 3127 |
</li> |
| 3128 |
|
| 3129 |
|
| 3130 |
<li id="section-157"> |
| 3131 |
<div class="annotation"> |
| 3132 |
|
| 3133 |
<div class="sswrap "> |
| 3134 |
<a class="ss" href="#section-157">§</a> |
| 3135 |
</div> |
| 3136 |
|
| 3137 |
</div> |
| 3138 |
|
| 3139 |
</li> |
| 3140 |
|
| 3141 |
|
| 3142 |
<li id="section-158"> |
| 3143 |
<div class="annotation"> |
| 3144 |
|
| 3145 |
<div class="sswrap "> |
| 3146 |
<a class="ss" href="#section-158">§</a> |
| 3147 |
</div> |
| 3148 |
<p>A CollectionIterator implements JavaScript’s Iterator protocol, allowing the |
| 3149 |
use of <code>for of</code> loops in modern browsers and interoperation between |
| 3150 |
Backbone.Collection and other JavaScript functions and third-party libraries |
| 3151 |
which can operate on Iterables.</p> |
| 3152 |
|
| 3153 |
</div> |
| 3154 |
|
| 3155 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">CollectionIterator</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">collection, kind</span>) { |
| 3156 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_collection</span> = collection; |
| 3157 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_kind</span> = kind; |
| 3158 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span> = <span class="hljs-number">0</span>; |
| 3159 |
};</pre></div></div> |
| 3160 |
|
| 3161 |
</li> |
| 3162 |
|
| 3163 |
|
| 3164 |
<li id="section-159"> |
| 3165 |
<div class="annotation"> |
| 3166 |
|
| 3167 |
<div class="sswrap "> |
| 3168 |
<a class="ss" href="#section-159">§</a> |
| 3169 |
</div> |
| 3170 |
<p>This “enum” defines the three possible kinds of values which can be emitted |
| 3171 |
by a CollectionIterator that correspond to the values(), keys() and entries() |
| 3172 |
methods on Collection, respectively.</p> |
| 3173 |
|
| 3174 |
</div> |
| 3175 |
|
| 3176 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-variable constant_">ITERATOR_VALUES</span> = <span class="hljs-number">1</span>; |
| 3177 |
<span class="hljs-keyword">var</span> <span class="hljs-variable constant_">ITERATOR_KEYS</span> = <span class="hljs-number">2</span>; |
| 3178 |
<span class="hljs-keyword">var</span> <span class="hljs-variable constant_">ITERATOR_KEYSVALUES</span> = <span class="hljs-number">3</span>;</pre></div></div> |
| 3179 |
|
| 3180 |
</li> |
| 3181 |
|
| 3182 |
|
| 3183 |
<li id="section-160"> |
| 3184 |
<div class="annotation"> |
| 3185 |
|
| 3186 |
<div class="sswrap "> |
| 3187 |
<a class="ss" href="#section-160">§</a> |
| 3188 |
</div> |
| 3189 |
<p>All Iterators should themselves be Iterable.</p> |
| 3190 |
|
| 3191 |
</div> |
| 3192 |
|
| 3193 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> ($$iterator) { |
| 3194 |
<span class="hljs-title class_">CollectionIterator</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>[$$iterator] = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3195 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3196 |
}; |
| 3197 |
} |
| 3198 |
|
| 3199 |
<span class="hljs-title class_">CollectionIterator</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">next</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3200 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_collection</span>) {</pre></div></div> |
| 3201 |
|
| 3202 |
</li> |
| 3203 |
|
| 3204 |
|
| 3205 |
<li id="section-161"> |
| 3206 |
<div class="annotation"> |
| 3207 |
|
| 3208 |
<div class="sswrap "> |
| 3209 |
<a class="ss" href="#section-161">§</a> |
| 3210 |
</div> |
| 3211 |
<p>Only continue iterating if the iterated collection is long enough.</p> |
| 3212 |
|
| 3213 |
</div> |
| 3214 |
|
| 3215 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span> < <span class="hljs-variable language_">this</span>.<span class="hljs-property">_collection</span>.<span class="hljs-property">length</span>) { |
| 3216 |
<span class="hljs-keyword">var</span> model = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_collection</span>.<span class="hljs-title function_">at</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span>); |
| 3217 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_index</span>++;</pre></div></div> |
| 3218 |
|
| 3219 |
</li> |
| 3220 |
|
| 3221 |
|
| 3222 |
<li id="section-162"> |
| 3223 |
<div class="annotation"> |
| 3224 |
|
| 3225 |
<div class="sswrap "> |
| 3226 |
<a class="ss" href="#section-162">§</a> |
| 3227 |
</div> |
| 3228 |
<p>Construct a value depending on what kind of values should be iterated.</p> |
| 3229 |
|
| 3230 |
</div> |
| 3231 |
|
| 3232 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> value; |
| 3233 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_kind</span> === <span class="hljs-variable constant_">ITERATOR_VALUES</span>) { |
| 3234 |
value = model; |
| 3235 |
} <span class="hljs-keyword">else</span> { |
| 3236 |
<span class="hljs-keyword">var</span> id = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_collection</span>.<span class="hljs-title function_">modelId</span>(model.<span class="hljs-property">attributes</span>, model.<span class="hljs-property">idAttribute</span>); |
| 3237 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_kind</span> === <span class="hljs-variable constant_">ITERATOR_KEYS</span>) { |
| 3238 |
value = id; |
| 3239 |
} <span class="hljs-keyword">else</span> { <span class="hljs-comment">// ITERATOR_KEYSVALUES</span> |
| 3240 |
value = [id, model]; |
| 3241 |
} |
| 3242 |
} |
| 3243 |
<span class="hljs-keyword">return</span> {<span class="hljs-attr">value</span>: value, <span class="hljs-attr">done</span>: <span class="hljs-literal">false</span>}; |
| 3244 |
}</pre></div></div> |
| 3245 |
|
| 3246 |
</li> |
| 3247 |
|
| 3248 |
|
| 3249 |
<li id="section-163"> |
| 3250 |
<div class="annotation"> |
| 3251 |
|
| 3252 |
<div class="sswrap "> |
| 3253 |
<a class="ss" href="#section-163">§</a> |
| 3254 |
</div> |
| 3255 |
<p>Once exhausted, remove the reference to the collection so future |
| 3256 |
calls to the next method always return done.</p> |
| 3257 |
|
| 3258 |
</div> |
| 3259 |
|
| 3260 |
<div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">_collection</span> = <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>; |
| 3261 |
} |
| 3262 |
|
| 3263 |
<span class="hljs-keyword">return</span> {<span class="hljs-attr">value</span>: <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>, <span class="hljs-attr">done</span>: <span class="hljs-literal">true</span>}; |
| 3264 |
};</pre></div></div> |
| 3265 |
|
| 3266 |
</li> |
| 3267 |
|
| 3268 |
|
| 3269 |
<li id="section-164"> |
| 3270 |
<div class="annotation"> |
| 3271 |
|
| 3272 |
<div class="sswrap "> |
| 3273 |
<a class="ss" href="#section-164">§</a> |
| 3274 |
</div> |
| 3275 |
<h2 id="backboneview">Backbone.View</h2> |
| 3276 |
|
| 3277 |
</div> |
| 3278 |
|
| 3279 |
</li> |
| 3280 |
|
| 3281 |
|
| 3282 |
<li id="section-165"> |
| 3283 |
<div class="annotation"> |
| 3284 |
|
| 3285 |
<div class="sswrap "> |
| 3286 |
<a class="ss" href="#section-165">§</a> |
| 3287 |
</div> |
| 3288 |
|
| 3289 |
</div> |
| 3290 |
|
| 3291 |
</li> |
| 3292 |
|
| 3293 |
|
| 3294 |
<li id="section-166"> |
| 3295 |
<div class="annotation"> |
| 3296 |
|
| 3297 |
<div class="sswrap "> |
| 3298 |
<a class="ss" href="#section-166">§</a> |
| 3299 |
</div> |
| 3300 |
<p>Backbone Views are almost more convention than they are actual code. A View |
| 3301 |
is simply a JavaScript object that represents a logical chunk of UI in the |
| 3302 |
DOM. This might be a single item, an entire list, a sidebar or panel, or |
| 3303 |
even the surrounding frame which wraps your whole app. Defining a chunk of |
| 3304 |
UI as a <strong>View</strong> allows you to define your DOM events declaratively, without |
| 3305 |
having to worry about render order … and makes it easy for the view to |
| 3306 |
react to specific changes in the state of your models.</p> |
| 3307 |
|
| 3308 |
</div> |
| 3309 |
|
| 3310 |
</li> |
| 3311 |
|
| 3312 |
|
| 3313 |
<li id="section-167"> |
| 3314 |
<div class="annotation"> |
| 3315 |
|
| 3316 |
<div class="sswrap "> |
| 3317 |
<a class="ss" href="#section-167">§</a> |
| 3318 |
</div> |
| 3319 |
<p>Creating a Backbone.View creates its initial element outside of the DOM, |
| 3320 |
if an existing element is not provided…</p> |
| 3321 |
|
| 3322 |
</div> |
| 3323 |
|
| 3324 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">View</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">View</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 3325 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">cid</span> = _.<span class="hljs-title function_">uniqueId</span>(<span class="hljs-string">'view'</span>); |
| 3326 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">preinitialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 3327 |
_.<span class="hljs-title function_">extend</span>(<span class="hljs-variable language_">this</span>, _.<span class="hljs-title function_">pick</span>(options, viewOptions)); |
| 3328 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_ensureElement</span>(); |
| 3329 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">initialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 3330 |
};</pre></div></div> |
| 3331 |
|
| 3332 |
</li> |
| 3333 |
|
| 3334 |
|
| 3335 |
<li id="section-168"> |
| 3336 |
<div class="annotation"> |
| 3337 |
|
| 3338 |
<div class="sswrap "> |
| 3339 |
<a class="ss" href="#section-168">§</a> |
| 3340 |
</div> |
| 3341 |
<p>Cached regex to split keys for <code>delegate</code>.</p> |
| 3342 |
|
| 3343 |
</div> |
| 3344 |
|
| 3345 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> delegateEventSplitter = <span class="hljs-regexp">/^(\S+)\s*(.*)$/</span>;</pre></div></div> |
| 3346 |
|
| 3347 |
</li> |
| 3348 |
|
| 3349 |
|
| 3350 |
<li id="section-169"> |
| 3351 |
<div class="annotation"> |
| 3352 |
|
| 3353 |
<div class="sswrap "> |
| 3354 |
<a class="ss" href="#section-169">§</a> |
| 3355 |
</div> |
| 3356 |
<p>List of view options to be set as properties.</p> |
| 3357 |
|
| 3358 |
</div> |
| 3359 |
|
| 3360 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> viewOptions = [<span class="hljs-string">'model'</span>, <span class="hljs-string">'collection'</span>, <span class="hljs-string">'el'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'attributes'</span>, <span class="hljs-string">'className'</span>, <span class="hljs-string">'tagName'</span>, <span class="hljs-string">'events'</span>];</pre></div></div> |
| 3361 |
|
| 3362 |
</li> |
| 3363 |
|
| 3364 |
|
| 3365 |
<li id="section-170"> |
| 3366 |
<div class="annotation"> |
| 3367 |
|
| 3368 |
<div class="sswrap "> |
| 3369 |
<a class="ss" href="#section-170">§</a> |
| 3370 |
</div> |
| 3371 |
<p>Set up all inheritable <strong>Backbone.View</strong> properties and methods.</p> |
| 3372 |
|
| 3373 |
</div> |
| 3374 |
|
| 3375 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">View</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, <span class="hljs-title class_">Events</span>, {</pre></div></div> |
| 3376 |
|
| 3377 |
</li> |
| 3378 |
|
| 3379 |
|
| 3380 |
<li id="section-171"> |
| 3381 |
<div class="annotation"> |
| 3382 |
|
| 3383 |
<div class="sswrap "> |
| 3384 |
<a class="ss" href="#section-171">§</a> |
| 3385 |
</div> |
| 3386 |
<p>The default <code>tagName</code> of a View’s element is <code>"div"</code>.</p> |
| 3387 |
|
| 3388 |
</div> |
| 3389 |
|
| 3390 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">tagName</span>: <span class="hljs-string">'div'</span>,</pre></div></div> |
| 3391 |
|
| 3392 |
</li> |
| 3393 |
|
| 3394 |
|
| 3395 |
<li id="section-172"> |
| 3396 |
<div class="annotation"> |
| 3397 |
|
| 3398 |
<div class="sswrap "> |
| 3399 |
<a class="ss" href="#section-172">§</a> |
| 3400 |
</div> |
| 3401 |
<p>jQuery delegate for element lookup, scoped to DOM elements within the |
| 3402 |
current view. This should be preferred to global lookups where possible.</p> |
| 3403 |
|
| 3404 |
</div> |
| 3405 |
|
| 3406 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">$</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">selector</span>) { |
| 3407 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">find</span>(selector); |
| 3408 |
},</pre></div></div> |
| 3409 |
|
| 3410 |
</li> |
| 3411 |
|
| 3412 |
|
| 3413 |
<li id="section-173"> |
| 3414 |
<div class="annotation"> |
| 3415 |
|
| 3416 |
<div class="sswrap "> |
| 3417 |
<a class="ss" href="#section-173">§</a> |
| 3418 |
</div> |
| 3419 |
<p>preinitialize is an empty function by default. You can override it with a function |
| 3420 |
or object. preinitialize will run before any instantiation logic is run in the View</p> |
| 3421 |
|
| 3422 |
</div> |
| 3423 |
|
| 3424 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">preinitialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 3425 |
|
| 3426 |
</li> |
| 3427 |
|
| 3428 |
|
| 3429 |
<li id="section-174"> |
| 3430 |
<div class="annotation"> |
| 3431 |
|
| 3432 |
<div class="sswrap "> |
| 3433 |
<a class="ss" href="#section-174">§</a> |
| 3434 |
</div> |
| 3435 |
<p>Initialize is an empty function by default. Override it with your own |
| 3436 |
initialization logic.</p> |
| 3437 |
|
| 3438 |
</div> |
| 3439 |
|
| 3440 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">initialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 3441 |
|
| 3442 |
</li> |
| 3443 |
|
| 3444 |
|
| 3445 |
<li id="section-175"> |
| 3446 |
<div class="annotation"> |
| 3447 |
|
| 3448 |
<div class="sswrap "> |
| 3449 |
<a class="ss" href="#section-175">§</a> |
| 3450 |
</div> |
| 3451 |
<p><strong>render</strong> is the core function that your view should override, in order |
| 3452 |
to populate its element (<code>this.el</code>), with the appropriate HTML. The |
| 3453 |
convention is for <strong>render</strong> to always return <code>this</code>.</p> |
| 3454 |
|
| 3455 |
</div> |
| 3456 |
|
| 3457 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">render</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3458 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3459 |
},</pre></div></div> |
| 3460 |
|
| 3461 |
</li> |
| 3462 |
|
| 3463 |
|
| 3464 |
<li id="section-176"> |
| 3465 |
<div class="annotation"> |
| 3466 |
|
| 3467 |
<div class="sswrap "> |
| 3468 |
<a class="ss" href="#section-176">§</a> |
| 3469 |
</div> |
| 3470 |
<p>Remove this view by taking the element out of the DOM, and removing any |
| 3471 |
applicable Backbone.Events listeners.</p> |
| 3472 |
|
| 3473 |
</div> |
| 3474 |
|
| 3475 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">remove</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3476 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_removeElement</span>(); |
| 3477 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">stopListening</span>(); |
| 3478 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3479 |
},</pre></div></div> |
| 3480 |
|
| 3481 |
</li> |
| 3482 |
|
| 3483 |
|
| 3484 |
<li id="section-177"> |
| 3485 |
<div class="annotation"> |
| 3486 |
|
| 3487 |
<div class="sswrap "> |
| 3488 |
<a class="ss" href="#section-177">§</a> |
| 3489 |
</div> |
| 3490 |
<p>Remove this view’s element from the document and all event listeners |
| 3491 |
attached to it. Exposed for subclasses using an alternative DOM |
| 3492 |
manipulation API.</p> |
| 3493 |
|
| 3494 |
</div> |
| 3495 |
|
| 3496 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_removeElement</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3497 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">remove</span>(); |
| 3498 |
},</pre></div></div> |
| 3499 |
|
| 3500 |
</li> |
| 3501 |
|
| 3502 |
|
| 3503 |
<li id="section-178"> |
| 3504 |
<div class="annotation"> |
| 3505 |
|
| 3506 |
<div class="sswrap "> |
| 3507 |
<a class="ss" href="#section-178">§</a> |
| 3508 |
</div> |
| 3509 |
<p>Change the view’s element (<code>this.el</code> property) and re-delegate the |
| 3510 |
view’s events on the new element.</p> |
| 3511 |
|
| 3512 |
</div> |
| 3513 |
|
| 3514 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">setElement</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">element</span>) { |
| 3515 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">undelegateEvents</span>(); |
| 3516 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_setElement</span>(element); |
| 3517 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">delegateEvents</span>(); |
| 3518 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3519 |
},</pre></div></div> |
| 3520 |
|
| 3521 |
</li> |
| 3522 |
|
| 3523 |
|
| 3524 |
<li id="section-179"> |
| 3525 |
<div class="annotation"> |
| 3526 |
|
| 3527 |
<div class="sswrap "> |
| 3528 |
<a class="ss" href="#section-179">§</a> |
| 3529 |
</div> |
| 3530 |
<p>Creates the <code>this.el</code> and <code>this.$el</code> references for this view using the |
| 3531 |
given <code>el</code>. <code>el</code> can be a CSS selector or an HTML string, a jQuery |
| 3532 |
context or an element. Subclasses can override this to utilize an |
| 3533 |
alternative DOM manipulation API and are only required to set the |
| 3534 |
<code>this.el</code> property.</p> |
| 3535 |
|
| 3536 |
</div> |
| 3537 |
|
| 3538 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_setElement</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">el</span>) { |
| 3539 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span> = el <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">$</span> ? el : <span class="hljs-title class_">Backbone</span>.$(el); |
| 3540 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">el</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>[<span class="hljs-number">0</span>]; |
| 3541 |
},</pre></div></div> |
| 3542 |
|
| 3543 |
</li> |
| 3544 |
|
| 3545 |
|
| 3546 |
<li id="section-180"> |
| 3547 |
<div class="annotation"> |
| 3548 |
|
| 3549 |
<div class="sswrap "> |
| 3550 |
<a class="ss" href="#section-180">§</a> |
| 3551 |
</div> |
| 3552 |
<p>Set callbacks, where <code>this.events</code> is a hash of</p> |
| 3553 |
<p><em>{“event selector”: “callback”}</em></p> |
| 3554 |
<pre><code>{ |
| 3555 |
<span class="hljs-string">'mousedown .title'</span>: <span class="hljs-string">'edit'</span>, |
| 3556 |
<span class="hljs-string">'click .button'</span>: <span class="hljs-string">'save'</span>, |
| 3557 |
<span class="hljs-string">'click .open'</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) { ... } |
| 3558 |
} |
| 3559 |
</code></pre> |
| 3560 |
<p>pairs. Callbacks will be bound to the view, with <code>this</code> set properly. |
| 3561 |
Uses event delegation for efficiency. |
| 3562 |
Omitting the selector binds the event to <code>this.el</code>.</p> |
| 3563 |
|
| 3564 |
</div> |
| 3565 |
|
| 3566 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">delegateEvents</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">events</span>) { |
| 3567 |
events || (events = _.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'events'</span>)); |
| 3568 |
<span class="hljs-keyword">if</span> (!events) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3569 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">undelegateEvents</span>(); |
| 3570 |
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> key <span class="hljs-keyword">in</span> events) { |
| 3571 |
<span class="hljs-keyword">var</span> method = events[key]; |
| 3572 |
<span class="hljs-keyword">if</span> (!_.<span class="hljs-title function_">isFunction</span>(method)) method = <span class="hljs-variable language_">this</span>[method]; |
| 3573 |
<span class="hljs-keyword">if</span> (!method) <span class="hljs-keyword">continue</span>; |
| 3574 |
<span class="hljs-keyword">var</span> match = key.<span class="hljs-title function_">match</span>(delegateEventSplitter); |
| 3575 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">delegate</span>(match[<span class="hljs-number">1</span>], match[<span class="hljs-number">2</span>], method.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>)); |
| 3576 |
} |
| 3577 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3578 |
},</pre></div></div> |
| 3579 |
|
| 3580 |
</li> |
| 3581 |
|
| 3582 |
|
| 3583 |
<li id="section-181"> |
| 3584 |
<div class="annotation"> |
| 3585 |
|
| 3586 |
<div class="sswrap "> |
| 3587 |
<a class="ss" href="#section-181">§</a> |
| 3588 |
</div> |
| 3589 |
<p>Add a single event listener to the view’s element (or a child element |
| 3590 |
using <code>selector</code>). This only works for delegate-able events: not <code>focus</code>, |
| 3591 |
<code>blur</code>, and not <code>change</code>, <code>submit</code>, and <code>reset</code> in Internet Explorer.</p> |
| 3592 |
|
| 3593 |
</div> |
| 3594 |
|
| 3595 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">delegate</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">eventName, selector, listener</span>) { |
| 3596 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">on</span>(eventName + <span class="hljs-string">'.delegateEvents'</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">cid</span>, selector, listener); |
| 3597 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3598 |
},</pre></div></div> |
| 3599 |
|
| 3600 |
</li> |
| 3601 |
|
| 3602 |
|
| 3603 |
<li id="section-182"> |
| 3604 |
<div class="annotation"> |
| 3605 |
|
| 3606 |
<div class="sswrap "> |
| 3607 |
<a class="ss" href="#section-182">§</a> |
| 3608 |
</div> |
| 3609 |
<p>Clears all callbacks previously bound to the view by <code>delegateEvents</code>. |
| 3610 |
You usually don’t need to use this, but may wish to if you have multiple |
| 3611 |
Backbone views attached to the same DOM element.</p> |
| 3612 |
|
| 3613 |
</div> |
| 3614 |
|
| 3615 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">undelegateEvents</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3616 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">off</span>(<span class="hljs-string">'.delegateEvents'</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">cid</span>); |
| 3617 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3618 |
},</pre></div></div> |
| 3619 |
|
| 3620 |
</li> |
| 3621 |
|
| 3622 |
|
| 3623 |
<li id="section-183"> |
| 3624 |
<div class="annotation"> |
| 3625 |
|
| 3626 |
<div class="sswrap "> |
| 3627 |
<a class="ss" href="#section-183">§</a> |
| 3628 |
</div> |
| 3629 |
<p>A finer-grained <code>undelegateEvents</code> for removing a single delegated event. |
| 3630 |
<code>selector</code> and <code>listener</code> are both optional.</p> |
| 3631 |
|
| 3632 |
</div> |
| 3633 |
|
| 3634 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">undelegate</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">eventName, selector, listener</span>) { |
| 3635 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">off</span>(eventName + <span class="hljs-string">'.delegateEvents'</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">cid</span>, selector, listener); |
| 3636 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 3637 |
},</pre></div></div> |
| 3638 |
|
| 3639 |
</li> |
| 3640 |
|
| 3641 |
|
| 3642 |
<li id="section-184"> |
| 3643 |
<div class="annotation"> |
| 3644 |
|
| 3645 |
<div class="sswrap "> |
| 3646 |
<a class="ss" href="#section-184">§</a> |
| 3647 |
</div> |
| 3648 |
<p>Produces a DOM element to be assigned to your view. Exposed for |
| 3649 |
subclasses using an alternative DOM manipulation API.</p> |
| 3650 |
|
| 3651 |
</div> |
| 3652 |
|
| 3653 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_createElement</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">tagName</span>) { |
| 3654 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(tagName); |
| 3655 |
},</pre></div></div> |
| 3656 |
|
| 3657 |
</li> |
| 3658 |
|
| 3659 |
|
| 3660 |
<li id="section-185"> |
| 3661 |
<div class="annotation"> |
| 3662 |
|
| 3663 |
<div class="sswrap "> |
| 3664 |
<a class="ss" href="#section-185">§</a> |
| 3665 |
</div> |
| 3666 |
<p>Ensure that the View has a DOM element to render into. |
| 3667 |
If <code>this.el</code> is a string, pass it through <code>$()</code>, take the first |
| 3668 |
matching element, and re-assign it to <code>el</code>. Otherwise, create |
| 3669 |
an element from the <code>id</code>, <code>className</code> and <code>tagName</code> properties.</p> |
| 3670 |
|
| 3671 |
</div> |
| 3672 |
|
| 3673 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_ensureElement</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3674 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">el</span>) { |
| 3675 |
<span class="hljs-keyword">var</span> attrs = _.<span class="hljs-title function_">extend</span>({}, _.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'attributes'</span>)); |
| 3676 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">id</span>) attrs.<span class="hljs-property">id</span> = _.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'id'</span>); |
| 3677 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">className</span>) attrs[<span class="hljs-string">'class'</span>] = _.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'className'</span>); |
| 3678 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setElement</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_createElement</span>(_.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'tagName'</span>))); |
| 3679 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_setAttributes</span>(attrs); |
| 3680 |
} <span class="hljs-keyword">else</span> { |
| 3681 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">setElement</span>(_.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'el'</span>)); |
| 3682 |
} |
| 3683 |
},</pre></div></div> |
| 3684 |
|
| 3685 |
</li> |
| 3686 |
|
| 3687 |
|
| 3688 |
<li id="section-186"> |
| 3689 |
<div class="annotation"> |
| 3690 |
|
| 3691 |
<div class="sswrap "> |
| 3692 |
<a class="ss" href="#section-186">§</a> |
| 3693 |
</div> |
| 3694 |
<p>Set attributes from a hash on this view’s element. Exposed for |
| 3695 |
subclasses using an alternative DOM manipulation API.</p> |
| 3696 |
|
| 3697 |
</div> |
| 3698 |
|
| 3699 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_setAttributes</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">attributes</span>) { |
| 3700 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">$el</span>.<span class="hljs-title function_">attr</span>(attributes); |
| 3701 |
} |
| 3702 |
|
| 3703 |
});</pre></div></div> |
| 3704 |
|
| 3705 |
</li> |
| 3706 |
|
| 3707 |
|
| 3708 |
<li id="section-187"> |
| 3709 |
<div class="annotation"> |
| 3710 |
|
| 3711 |
<div class="sswrap "> |
| 3712 |
<a class="ss" href="#section-187">§</a> |
| 3713 |
</div> |
| 3714 |
<p>Proxy Backbone class methods to Underscore functions, wrapping the model’s |
| 3715 |
<code>attributes</code> object or collection’s <code>models</code> array behind the scenes.</p> |
| 3716 |
<p>collection.filter(function(model) { return model.get(‘age’) > 10 }); |
| 3717 |
collection.each(this.addView);</p> |
| 3718 |
<p><code>Function#apply</code> can be slow so we use the method’s arg count, if we know it.</p> |
| 3719 |
|
| 3720 |
</div> |
| 3721 |
|
| 3722 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> addMethod = <span class="hljs-keyword">function</span>(<span class="hljs-params">base, length, method, attribute</span>) { |
| 3723 |
<span class="hljs-keyword">switch</span> (length) { |
| 3724 |
<span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3725 |
<span class="hljs-keyword">return</span> base[method](<span class="hljs-variable language_">this</span>[attribute]); |
| 3726 |
}; |
| 3727 |
<span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">value</span>) { |
| 3728 |
<span class="hljs-keyword">return</span> base[method](<span class="hljs-variable language_">this</span>[attribute], value); |
| 3729 |
}; |
| 3730 |
<span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">iteratee, context</span>) { |
| 3731 |
<span class="hljs-keyword">return</span> base[method](<span class="hljs-variable language_">this</span>[attribute], <span class="hljs-title function_">cb</span>(iteratee, <span class="hljs-variable language_">this</span>), context); |
| 3732 |
}; |
| 3733 |
<span class="hljs-keyword">case</span> <span class="hljs-number">4</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">iteratee, defaultVal, context</span>) { |
| 3734 |
<span class="hljs-keyword">return</span> base[method](<span class="hljs-variable language_">this</span>[attribute], <span class="hljs-title function_">cb</span>(iteratee, <span class="hljs-variable language_">this</span>), defaultVal, context); |
| 3735 |
}; |
| 3736 |
<span class="hljs-attr">default</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 3737 |
<span class="hljs-keyword">var</span> args = slice.<span class="hljs-title function_">call</span>(<span class="hljs-variable language_">arguments</span>); |
| 3738 |
args.<span class="hljs-title function_">unshift</span>(<span class="hljs-variable language_">this</span>[attribute]); |
| 3739 |
<span class="hljs-keyword">return</span> base[method].<span class="hljs-title function_">apply</span>(base, args); |
| 3740 |
}; |
| 3741 |
} |
| 3742 |
}; |
| 3743 |
|
| 3744 |
<span class="hljs-keyword">var</span> addUnderscoreMethods = <span class="hljs-keyword">function</span>(<span class="hljs-params">Class, base, methods, attribute</span>) { |
| 3745 |
_.<span class="hljs-title function_">each</span>(methods, <span class="hljs-keyword">function</span>(<span class="hljs-params">length, method</span>) { |
| 3746 |
<span class="hljs-keyword">if</span> (base[method]) <span class="hljs-title class_">Class</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>[method] = <span class="hljs-title function_">addMethod</span>(base, length, method, attribute); |
| 3747 |
}); |
| 3748 |
};</pre></div></div> |
| 3749 |
|
| 3750 |
</li> |
| 3751 |
|
| 3752 |
|
| 3753 |
<li id="section-188"> |
| 3754 |
<div class="annotation"> |
| 3755 |
|
| 3756 |
<div class="sswrap "> |
| 3757 |
<a class="ss" href="#section-188">§</a> |
| 3758 |
</div> |
| 3759 |
<p>Support <code>collection.sortBy('attr')</code> and <code>collection.findWhere({id: 1})</code>.</p> |
| 3760 |
|
| 3761 |
</div> |
| 3762 |
|
| 3763 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> cb = <span class="hljs-keyword">function</span>(<span class="hljs-params">iteratee, instance</span>) { |
| 3764 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isFunction</span>(iteratee)) <span class="hljs-keyword">return</span> iteratee; |
| 3765 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isObject</span>(iteratee) && !instance.<span class="hljs-title function_">_isModel</span>(iteratee)) <span class="hljs-keyword">return</span> <span class="hljs-title function_">modelMatcher</span>(iteratee); |
| 3766 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isString</span>(iteratee)) <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { <span class="hljs-keyword">return</span> model.<span class="hljs-title function_">get</span>(iteratee); }; |
| 3767 |
<span class="hljs-keyword">return</span> iteratee; |
| 3768 |
}; |
| 3769 |
<span class="hljs-keyword">var</span> modelMatcher = <span class="hljs-keyword">function</span>(<span class="hljs-params">attrs</span>) { |
| 3770 |
<span class="hljs-keyword">var</span> matcher = _.<span class="hljs-title function_">matches</span>(attrs); |
| 3771 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 3772 |
<span class="hljs-keyword">return</span> <span class="hljs-title function_">matcher</span>(model.<span class="hljs-property">attributes</span>); |
| 3773 |
}; |
| 3774 |
};</pre></div></div> |
| 3775 |
|
| 3776 |
</li> |
| 3777 |
|
| 3778 |
|
| 3779 |
<li id="section-189"> |
| 3780 |
<div class="annotation"> |
| 3781 |
|
| 3782 |
<div class="sswrap "> |
| 3783 |
<a class="ss" href="#section-189">§</a> |
| 3784 |
</div> |
| 3785 |
<p>Underscore methods that we want to implement on the Collection. |
| 3786 |
90% of the core usefulness of Backbone Collections is actually implemented |
| 3787 |
right here:</p> |
| 3788 |
|
| 3789 |
</div> |
| 3790 |
|
| 3791 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> collectionMethods = {<span class="hljs-attr">forEach</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">each</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">map</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">collect</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">reduce</span>: <span class="hljs-number">0</span>, |
| 3792 |
<span class="hljs-attr">foldl</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">inject</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">reduceRight</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">foldr</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">find</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">detect</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">filter</span>: <span class="hljs-number">3</span>, |
| 3793 |
<span class="hljs-attr">select</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">reject</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">every</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">all</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">some</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">any</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">include</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">includes</span>: <span class="hljs-number">3</span>, |
| 3794 |
<span class="hljs-attr">contains</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">invoke</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">max</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">min</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">toArray</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">size</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">first</span>: <span class="hljs-number">3</span>, |
| 3795 |
<span class="hljs-attr">head</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">take</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">initial</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">rest</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">tail</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">drop</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">last</span>: <span class="hljs-number">3</span>, |
| 3796 |
<span class="hljs-attr">without</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">difference</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">indexOf</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">shuffle</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">lastIndexOf</span>: <span class="hljs-number">3</span>, |
| 3797 |
<span class="hljs-attr">isEmpty</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">chain</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">sample</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">partition</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">groupBy</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">countBy</span>: <span class="hljs-number">3</span>, |
| 3798 |
<span class="hljs-attr">sortBy</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">indexBy</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">findIndex</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">findLastIndex</span>: <span class="hljs-number">3</span>};</pre></div></div> |
| 3799 |
|
| 3800 |
</li> |
| 3801 |
|
| 3802 |
|
| 3803 |
<li id="section-190"> |
| 3804 |
<div class="annotation"> |
| 3805 |
|
| 3806 |
<div class="sswrap "> |
| 3807 |
<a class="ss" href="#section-190">§</a> |
| 3808 |
</div> |
| 3809 |
<p>Underscore methods that we want to implement on the Model, mapped to the |
| 3810 |
number of arguments they take.</p> |
| 3811 |
|
| 3812 |
</div> |
| 3813 |
|
| 3814 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> modelMethods = {<span class="hljs-attr">keys</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">values</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">pairs</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">invert</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">pick</span>: <span class="hljs-number">0</span>, |
| 3815 |
<span class="hljs-attr">omit</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">chain</span>: <span class="hljs-number">1</span>, <span class="hljs-attr">isEmpty</span>: <span class="hljs-number">1</span>};</pre></div></div> |
| 3816 |
|
| 3817 |
</li> |
| 3818 |
|
| 3819 |
|
| 3820 |
<li id="section-191"> |
| 3821 |
<div class="annotation"> |
| 3822 |
|
| 3823 |
<div class="sswrap "> |
| 3824 |
<a class="ss" href="#section-191">§</a> |
| 3825 |
</div> |
| 3826 |
<p>Mix in each Underscore method as a proxy to <code>Collection#models</code>.</p> |
| 3827 |
|
| 3828 |
</div> |
| 3829 |
|
| 3830 |
<div class="content"><div class='highlight'><pre> |
| 3831 |
_.<span class="hljs-title function_">each</span>([ |
| 3832 |
[<span class="hljs-title class_">Collection</span>, collectionMethods, <span class="hljs-string">'models'</span>], |
| 3833 |
[<span class="hljs-title class_">Model</span>, modelMethods, <span class="hljs-string">'attributes'</span>] |
| 3834 |
], <span class="hljs-keyword">function</span>(<span class="hljs-params">config</span>) { |
| 3835 |
<span class="hljs-keyword">var</span> <span class="hljs-title class_">Base</span> = config[<span class="hljs-number">0</span>], |
| 3836 |
methods = config[<span class="hljs-number">1</span>], |
| 3837 |
attribute = config[<span class="hljs-number">2</span>]; |
| 3838 |
|
| 3839 |
<span class="hljs-title class_">Base</span>.<span class="hljs-property">mixin</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">obj</span>) { |
| 3840 |
<span class="hljs-keyword">var</span> mappings = _.<span class="hljs-title function_">reduce</span>(_.<span class="hljs-title function_">functions</span>(obj), <span class="hljs-keyword">function</span>(<span class="hljs-params">memo, name</span>) { |
| 3841 |
memo[name] = <span class="hljs-number">0</span>; |
| 3842 |
<span class="hljs-keyword">return</span> memo; |
| 3843 |
}, {}); |
| 3844 |
<span class="hljs-title function_">addUnderscoreMethods</span>(<span class="hljs-title class_">Base</span>, obj, mappings, attribute); |
| 3845 |
}; |
| 3846 |
|
| 3847 |
<span class="hljs-title function_">addUnderscoreMethods</span>(<span class="hljs-title class_">Base</span>, _, methods, attribute); |
| 3848 |
});</pre></div></div> |
| 3849 |
|
| 3850 |
</li> |
| 3851 |
|
| 3852 |
|
| 3853 |
<li id="section-192"> |
| 3854 |
<div class="annotation"> |
| 3855 |
|
| 3856 |
<div class="sswrap "> |
| 3857 |
<a class="ss" href="#section-192">§</a> |
| 3858 |
</div> |
| 3859 |
<h2 id="backbonesync">Backbone.sync</h2> |
| 3860 |
|
| 3861 |
</div> |
| 3862 |
|
| 3863 |
</li> |
| 3864 |
|
| 3865 |
|
| 3866 |
<li id="section-193"> |
| 3867 |
<div class="annotation"> |
| 3868 |
|
| 3869 |
<div class="sswrap "> |
| 3870 |
<a class="ss" href="#section-193">§</a> |
| 3871 |
</div> |
| 3872 |
|
| 3873 |
</div> |
| 3874 |
|
| 3875 |
</li> |
| 3876 |
|
| 3877 |
|
| 3878 |
<li id="section-194"> |
| 3879 |
<div class="annotation"> |
| 3880 |
|
| 3881 |
<div class="sswrap "> |
| 3882 |
<a class="ss" href="#section-194">§</a> |
| 3883 |
</div> |
| 3884 |
<p>Override this function to change the manner in which Backbone persists |
| 3885 |
models to the server. You will be passed the type of request, and the |
| 3886 |
model in question. By default, makes a RESTful Ajax request |
| 3887 |
to the model’s <code>url()</code>. Some possible customizations could be:</p> |
| 3888 |
<ul> |
| 3889 |
<li>Use <code>setTimeout</code> to batch rapid-fire updates into a single request.</li> |
| 3890 |
<li>Send up the models as XML instead of JSON.</li> |
| 3891 |
<li>Persist models via WebSockets instead of Ajax.</li> |
| 3892 |
</ul> |
| 3893 |
<p>Turn on <code>Backbone.emulateHTTP</code> in order to send <code>PUT</code> and <code>DELETE</code> requests |
| 3894 |
as <code>POST</code>, with a <code>_method</code> parameter containing the true HTTP method, |
| 3895 |
as well as all requests with the body as <code>application/x-www-form-urlencoded</code> |
| 3896 |
instead of <code>application/json</code> with the model in a param named <code>model</code>. |
| 3897 |
Useful when interfacing with server-side languages like <strong>PHP</strong> that make |
| 3898 |
it difficult to read the body of <code>PUT</code> requests.</p> |
| 3899 |
|
| 3900 |
</div> |
| 3901 |
|
| 3902 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">sync</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">method, model, options</span>) { |
| 3903 |
<span class="hljs-keyword">var</span> type = methodMap[method];</pre></div></div> |
| 3904 |
|
| 3905 |
</li> |
| 3906 |
|
| 3907 |
|
| 3908 |
<li id="section-195"> |
| 3909 |
<div class="annotation"> |
| 3910 |
|
| 3911 |
<div class="sswrap "> |
| 3912 |
<a class="ss" href="#section-195">§</a> |
| 3913 |
</div> |
| 3914 |
<p>Default options, unless specified.</p> |
| 3915 |
|
| 3916 |
</div> |
| 3917 |
|
| 3918 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">defaults</span>(options || (options = {}), { |
| 3919 |
<span class="hljs-attr">emulateHTTP</span>: <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">emulateHTTP</span>, |
| 3920 |
<span class="hljs-attr">emulateJSON</span>: <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">emulateJSON</span> |
| 3921 |
});</pre></div></div> |
| 3922 |
|
| 3923 |
</li> |
| 3924 |
|
| 3925 |
|
| 3926 |
<li id="section-196"> |
| 3927 |
<div class="annotation"> |
| 3928 |
|
| 3929 |
<div class="sswrap "> |
| 3930 |
<a class="ss" href="#section-196">§</a> |
| 3931 |
</div> |
| 3932 |
<p>Default JSON-request options.</p> |
| 3933 |
|
| 3934 |
</div> |
| 3935 |
|
| 3936 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> params = {<span class="hljs-attr">type</span>: type, <span class="hljs-attr">dataType</span>: <span class="hljs-string">'json'</span>};</pre></div></div> |
| 3937 |
|
| 3938 |
</li> |
| 3939 |
|
| 3940 |
|
| 3941 |
<li id="section-197"> |
| 3942 |
<div class="annotation"> |
| 3943 |
|
| 3944 |
<div class="sswrap "> |
| 3945 |
<a class="ss" href="#section-197">§</a> |
| 3946 |
</div> |
| 3947 |
<p>Ensure that we have a URL.</p> |
| 3948 |
|
| 3949 |
</div> |
| 3950 |
|
| 3951 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!options.<span class="hljs-property">url</span>) { |
| 3952 |
params.<span class="hljs-property">url</span> = _.<span class="hljs-title function_">result</span>(model, <span class="hljs-string">'url'</span>) || <span class="hljs-title function_">urlError</span>(); |
| 3953 |
}</pre></div></div> |
| 3954 |
|
| 3955 |
</li> |
| 3956 |
|
| 3957 |
|
| 3958 |
<li id="section-198"> |
| 3959 |
<div class="annotation"> |
| 3960 |
|
| 3961 |
<div class="sswrap "> |
| 3962 |
<a class="ss" href="#section-198">§</a> |
| 3963 |
</div> |
| 3964 |
<p>Ensure that we have the appropriate request data.</p> |
| 3965 |
|
| 3966 |
</div> |
| 3967 |
|
| 3968 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (options.<span class="hljs-property">data</span> == <span class="hljs-literal">null</span> && model && (method === <span class="hljs-string">'create'</span> || method === <span class="hljs-string">'update'</span> || method === <span class="hljs-string">'patch'</span>)) { |
| 3969 |
params.<span class="hljs-property">contentType</span> = <span class="hljs-string">'application/json'</span>; |
| 3970 |
params.<span class="hljs-property">data</span> = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(options.<span class="hljs-property">attrs</span> || model.<span class="hljs-title function_">toJSON</span>(options)); |
| 3971 |
}</pre></div></div> |
| 3972 |
|
| 3973 |
</li> |
| 3974 |
|
| 3975 |
|
| 3976 |
<li id="section-199"> |
| 3977 |
<div class="annotation"> |
| 3978 |
|
| 3979 |
<div class="sswrap "> |
| 3980 |
<a class="ss" href="#section-199">§</a> |
| 3981 |
</div> |
| 3982 |
<p>For older servers, emulate JSON by encoding the request into an HTML-form.</p> |
| 3983 |
|
| 3984 |
</div> |
| 3985 |
|
| 3986 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (options.<span class="hljs-property">emulateJSON</span>) { |
| 3987 |
params.<span class="hljs-property">contentType</span> = <span class="hljs-string">'application/x-www-form-urlencoded'</span>; |
| 3988 |
params.<span class="hljs-property">data</span> = params.<span class="hljs-property">data</span> ? {<span class="hljs-attr">model</span>: params.<span class="hljs-property">data</span>} : {}; |
| 3989 |
}</pre></div></div> |
| 3990 |
|
| 3991 |
</li> |
| 3992 |
|
| 3993 |
|
| 3994 |
<li id="section-200"> |
| 3995 |
<div class="annotation"> |
| 3996 |
|
| 3997 |
<div class="sswrap "> |
| 3998 |
<a class="ss" href="#section-200">§</a> |
| 3999 |
</div> |
| 4000 |
<p>For older servers, emulate HTTP by mimicking the HTTP method with <code>_method</code> |
| 4001 |
And an <code>X-HTTP-Method-Override</code> header.</p> |
| 4002 |
|
| 4003 |
</div> |
| 4004 |
|
| 4005 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (options.<span class="hljs-property">emulateHTTP</span> && (type === <span class="hljs-string">'PUT'</span> || type === <span class="hljs-string">'DELETE'</span> || type === <span class="hljs-string">'PATCH'</span>)) { |
| 4006 |
params.<span class="hljs-property">type</span> = <span class="hljs-string">'POST'</span>; |
| 4007 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">emulateJSON</span>) params.<span class="hljs-property">data</span>.<span class="hljs-property">_method</span> = type; |
| 4008 |
<span class="hljs-keyword">var</span> beforeSend = options.<span class="hljs-property">beforeSend</span>; |
| 4009 |
options.<span class="hljs-property">beforeSend</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">xhr</span>) { |
| 4010 |
xhr.<span class="hljs-title function_">setRequestHeader</span>(<span class="hljs-string">'X-HTTP-Method-Override'</span>, type); |
| 4011 |
<span class="hljs-keyword">if</span> (beforeSend) <span class="hljs-keyword">return</span> beforeSend.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 4012 |
}; |
| 4013 |
}</pre></div></div> |
| 4014 |
|
| 4015 |
</li> |
| 4016 |
|
| 4017 |
|
| 4018 |
<li id="section-201"> |
| 4019 |
<div class="annotation"> |
| 4020 |
|
| 4021 |
<div class="sswrap "> |
| 4022 |
<a class="ss" href="#section-201">§</a> |
| 4023 |
</div> |
| 4024 |
<p>Don’t process data on a non-GET request.</p> |
| 4025 |
|
| 4026 |
</div> |
| 4027 |
|
| 4028 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (params.<span class="hljs-property">type</span> !== <span class="hljs-string">'GET'</span> && !options.<span class="hljs-property">emulateJSON</span>) { |
| 4029 |
params.<span class="hljs-property">processData</span> = <span class="hljs-literal">false</span>; |
| 4030 |
}</pre></div></div> |
| 4031 |
|
| 4032 |
</li> |
| 4033 |
|
| 4034 |
|
| 4035 |
<li id="section-202"> |
| 4036 |
<div class="annotation"> |
| 4037 |
|
| 4038 |
<div class="sswrap "> |
| 4039 |
<a class="ss" href="#section-202">§</a> |
| 4040 |
</div> |
| 4041 |
<p>Pass along <code>textStatus</code> and <code>errorThrown</code> from jQuery.</p> |
| 4042 |
|
| 4043 |
</div> |
| 4044 |
|
| 4045 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> error = options.<span class="hljs-property">error</span>; |
| 4046 |
options.<span class="hljs-property">error</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">xhr, textStatus, errorThrown</span>) { |
| 4047 |
options.<span class="hljs-property">textStatus</span> = textStatus; |
| 4048 |
options.<span class="hljs-property">errorThrown</span> = errorThrown; |
| 4049 |
<span class="hljs-keyword">if</span> (error) error.<span class="hljs-title function_">call</span>(options.<span class="hljs-property">context</span>, xhr, textStatus, errorThrown); |
| 4050 |
};</pre></div></div> |
| 4051 |
|
| 4052 |
</li> |
| 4053 |
|
| 4054 |
|
| 4055 |
<li id="section-203"> |
| 4056 |
<div class="annotation"> |
| 4057 |
|
| 4058 |
<div class="sswrap "> |
| 4059 |
<a class="ss" href="#section-203">§</a> |
| 4060 |
</div> |
| 4061 |
<p>Make the request, allowing the user to override any Ajax options.</p> |
| 4062 |
|
| 4063 |
</div> |
| 4064 |
|
| 4065 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> xhr = options.<span class="hljs-property">xhr</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-title function_">ajax</span>(_.<span class="hljs-title function_">extend</span>(params, options)); |
| 4066 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'request'</span>, model, xhr, options); |
| 4067 |
<span class="hljs-keyword">return</span> xhr; |
| 4068 |
};</pre></div></div> |
| 4069 |
|
| 4070 |
</li> |
| 4071 |
|
| 4072 |
|
| 4073 |
<li id="section-204"> |
| 4074 |
<div class="annotation"> |
| 4075 |
|
| 4076 |
<div class="sswrap "> |
| 4077 |
<a class="ss" href="#section-204">§</a> |
| 4078 |
</div> |
| 4079 |
<p>Map from CRUD to HTTP for our default <code>Backbone.sync</code> implementation.</p> |
| 4080 |
|
| 4081 |
</div> |
| 4082 |
|
| 4083 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> methodMap = { |
| 4084 |
<span class="hljs-string">'create'</span>: <span class="hljs-string">'POST'</span>, |
| 4085 |
<span class="hljs-string">'update'</span>: <span class="hljs-string">'PUT'</span>, |
| 4086 |
<span class="hljs-string">'patch'</span>: <span class="hljs-string">'PATCH'</span>, |
| 4087 |
<span class="hljs-string">'delete'</span>: <span class="hljs-string">'DELETE'</span>, |
| 4088 |
<span class="hljs-string">'read'</span>: <span class="hljs-string">'GET'</span> |
| 4089 |
};</pre></div></div> |
| 4090 |
|
| 4091 |
</li> |
| 4092 |
|
| 4093 |
|
| 4094 |
<li id="section-205"> |
| 4095 |
<div class="annotation"> |
| 4096 |
|
| 4097 |
<div class="sswrap "> |
| 4098 |
<a class="ss" href="#section-205">§</a> |
| 4099 |
</div> |
| 4100 |
<p>Set the default implementation of <code>Backbone.ajax</code> to proxy through to <code>$</code>. |
| 4101 |
Override this if you’d like to use a different library.</p> |
| 4102 |
|
| 4103 |
</div> |
| 4104 |
|
| 4105 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">ajax</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4106 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">$</span>.<span class="hljs-property">ajax</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">$</span>, <span class="hljs-variable language_">arguments</span>); |
| 4107 |
};</pre></div></div> |
| 4108 |
|
| 4109 |
</li> |
| 4110 |
|
| 4111 |
|
| 4112 |
<li id="section-206"> |
| 4113 |
<div class="annotation"> |
| 4114 |
|
| 4115 |
<div class="sswrap "> |
| 4116 |
<a class="ss" href="#section-206">§</a> |
| 4117 |
</div> |
| 4118 |
<h2 id="backbonerouter">Backbone.Router</h2> |
| 4119 |
|
| 4120 |
</div> |
| 4121 |
|
| 4122 |
</li> |
| 4123 |
|
| 4124 |
|
| 4125 |
<li id="section-207"> |
| 4126 |
<div class="annotation"> |
| 4127 |
|
| 4128 |
<div class="sswrap "> |
| 4129 |
<a class="ss" href="#section-207">§</a> |
| 4130 |
</div> |
| 4131 |
|
| 4132 |
</div> |
| 4133 |
|
| 4134 |
</li> |
| 4135 |
|
| 4136 |
|
| 4137 |
<li id="section-208"> |
| 4138 |
<div class="annotation"> |
| 4139 |
|
| 4140 |
<div class="sswrap "> |
| 4141 |
<a class="ss" href="#section-208">§</a> |
| 4142 |
</div> |
| 4143 |
<p>Routers map faux-URLs to actions, and fire events when routes are |
| 4144 |
matched. Creating a new one sets its <code>routes</code> hash, if not set statically.</p> |
| 4145 |
|
| 4146 |
</div> |
| 4147 |
|
| 4148 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">Router</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">Router</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 4149 |
options || (options = {}); |
| 4150 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">preinitialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 4151 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">routes</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-property">routes</span> = options.<span class="hljs-property">routes</span>; |
| 4152 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_bindRoutes</span>(); |
| 4153 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">initialize</span>.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); |
| 4154 |
};</pre></div></div> |
| 4155 |
|
| 4156 |
</li> |
| 4157 |
|
| 4158 |
|
| 4159 |
<li id="section-209"> |
| 4160 |
<div class="annotation"> |
| 4161 |
|
| 4162 |
<div class="sswrap "> |
| 4163 |
<a class="ss" href="#section-209">§</a> |
| 4164 |
</div> |
| 4165 |
<p>Cached regular expressions for matching named param parts and splatted |
| 4166 |
parts of route strings.</p> |
| 4167 |
|
| 4168 |
</div> |
| 4169 |
|
| 4170 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> optionalParam = <span class="hljs-regexp">/\((.*?)\)/g</span>; |
| 4171 |
<span class="hljs-keyword">var</span> namedParam = <span class="hljs-regexp">/(\(\?)?:\w+/g</span>; |
| 4172 |
<span class="hljs-keyword">var</span> splatParam = <span class="hljs-regexp">/\*\w+/g</span>; |
| 4173 |
<span class="hljs-keyword">var</span> escapeRegExp = <span class="hljs-regexp">/[\-{}\[\]+?.,\\\^$|#\s]/g</span>;</pre></div></div> |
| 4174 |
|
| 4175 |
</li> |
| 4176 |
|
| 4177 |
|
| 4178 |
<li id="section-210"> |
| 4179 |
<div class="annotation"> |
| 4180 |
|
| 4181 |
<div class="sswrap "> |
| 4182 |
<a class="ss" href="#section-210">§</a> |
| 4183 |
</div> |
| 4184 |
<p>Set up all inheritable <strong>Backbone.Router</strong> properties and methods.</p> |
| 4185 |
|
| 4186 |
</div> |
| 4187 |
|
| 4188 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">Router</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, <span class="hljs-title class_">Events</span>, {</pre></div></div> |
| 4189 |
|
| 4190 |
</li> |
| 4191 |
|
| 4192 |
|
| 4193 |
<li id="section-211"> |
| 4194 |
<div class="annotation"> |
| 4195 |
|
| 4196 |
<div class="sswrap "> |
| 4197 |
<a class="ss" href="#section-211">§</a> |
| 4198 |
</div> |
| 4199 |
<p>preinitialize is an empty function by default. You can override it with a function |
| 4200 |
or object. preinitialize will run before any instantiation logic is run in the Router.</p> |
| 4201 |
|
| 4202 |
</div> |
| 4203 |
|
| 4204 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">preinitialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 4205 |
|
| 4206 |
</li> |
| 4207 |
|
| 4208 |
|
| 4209 |
<li id="section-212"> |
| 4210 |
<div class="annotation"> |
| 4211 |
|
| 4212 |
<div class="sswrap "> |
| 4213 |
<a class="ss" href="#section-212">§</a> |
| 4214 |
</div> |
| 4215 |
<p>Initialize is an empty function by default. Override it with your own |
| 4216 |
initialization logic.</p> |
| 4217 |
|
| 4218 |
</div> |
| 4219 |
|
| 4220 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">initialize</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){},</pre></div></div> |
| 4221 |
|
| 4222 |
</li> |
| 4223 |
|
| 4224 |
|
| 4225 |
<li id="section-213"> |
| 4226 |
<div class="annotation"> |
| 4227 |
|
| 4228 |
<div class="sswrap "> |
| 4229 |
<a class="ss" href="#section-213">§</a> |
| 4230 |
</div> |
| 4231 |
<p>Manually bind a single named route to a callback. For example:</p> |
| 4232 |
<pre><code><span class="hljs-variable language_">this</span>.<span class="hljs-title function_">route</span>(<span class="hljs-string">'search/:query/p:num'</span>, <span class="hljs-string">'search'</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">query, num</span>) { |
| 4233 |
... |
| 4234 |
}); |
| 4235 |
</code></pre> |
| 4236 |
|
| 4237 |
</div> |
| 4238 |
|
| 4239 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">route</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">route, name, callback</span>) { |
| 4240 |
<span class="hljs-keyword">if</span> (!_.<span class="hljs-title function_">isRegExp</span>(route)) route = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_routeToRegExp</span>(route); |
| 4241 |
<span class="hljs-keyword">if</span> (_.<span class="hljs-title function_">isFunction</span>(name)) { |
| 4242 |
callback = name; |
| 4243 |
name = <span class="hljs-string">''</span>; |
| 4244 |
} |
| 4245 |
<span class="hljs-keyword">if</span> (!callback) callback = <span class="hljs-variable language_">this</span>[name]; |
| 4246 |
<span class="hljs-keyword">var</span> router = <span class="hljs-variable language_">this</span>; |
| 4247 |
<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">history</span>.<span class="hljs-title function_">route</span>(route, <span class="hljs-keyword">function</span>(<span class="hljs-params">fragment</span>) { |
| 4248 |
<span class="hljs-keyword">var</span> args = router.<span class="hljs-title function_">_extractParameters</span>(route, fragment); |
| 4249 |
<span class="hljs-keyword">if</span> (router.<span class="hljs-title function_">execute</span>(callback, args, name) !== <span class="hljs-literal">false</span>) { |
| 4250 |
router.<span class="hljs-property">trigger</span>.<span class="hljs-title function_">apply</span>(router, [<span class="hljs-string">'route:'</span> + name].<span class="hljs-title function_">concat</span>(args)); |
| 4251 |
router.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'route'</span>, name, args); |
| 4252 |
<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">history</span>.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'route'</span>, router, name, args); |
| 4253 |
} |
| 4254 |
}); |
| 4255 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 4256 |
},</pre></div></div> |
| 4257 |
|
| 4258 |
</li> |
| 4259 |
|
| 4260 |
|
| 4261 |
<li id="section-214"> |
| 4262 |
<div class="annotation"> |
| 4263 |
|
| 4264 |
<div class="sswrap "> |
| 4265 |
<a class="ss" href="#section-214">§</a> |
| 4266 |
</div> |
| 4267 |
<p>Execute a route handler with the provided parameters. This is an |
| 4268 |
excellent place to do pre-route setup or post-route cleanup.</p> |
| 4269 |
|
| 4270 |
</div> |
| 4271 |
|
| 4272 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">execute</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">callback, args, name</span>) { |
| 4273 |
<span class="hljs-keyword">if</span> (callback) callback.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); |
| 4274 |
},</pre></div></div> |
| 4275 |
|
| 4276 |
</li> |
| 4277 |
|
| 4278 |
|
| 4279 |
<li id="section-215"> |
| 4280 |
<div class="annotation"> |
| 4281 |
|
| 4282 |
<div class="sswrap "> |
| 4283 |
<a class="ss" href="#section-215">§</a> |
| 4284 |
</div> |
| 4285 |
<p>Simple proxy to <code>Backbone.history</code> to save a fragment into the history.</p> |
| 4286 |
|
| 4287 |
</div> |
| 4288 |
|
| 4289 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">navigate</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">fragment, options</span>) { |
| 4290 |
<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">history</span>.<span class="hljs-title function_">navigate</span>(fragment, options); |
| 4291 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>; |
| 4292 |
},</pre></div></div> |
| 4293 |
|
| 4294 |
</li> |
| 4295 |
|
| 4296 |
|
| 4297 |
<li id="section-216"> |
| 4298 |
<div class="annotation"> |
| 4299 |
|
| 4300 |
<div class="sswrap "> |
| 4301 |
<a class="ss" href="#section-216">§</a> |
| 4302 |
</div> |
| 4303 |
<p>Bind all defined routes to <code>Backbone.history</code>. We have to reverse the |
| 4304 |
order of the routes here to support behavior where the most general |
| 4305 |
routes can be defined at the bottom of the route map.</p> |
| 4306 |
|
| 4307 |
</div> |
| 4308 |
|
| 4309 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_bindRoutes</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4310 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">routes</span>) <span class="hljs-keyword">return</span>; |
| 4311 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">routes</span> = _.<span class="hljs-title function_">result</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-string">'routes'</span>); |
| 4312 |
<span class="hljs-keyword">var</span> route, routes = _.<span class="hljs-title function_">keys</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">routes</span>); |
| 4313 |
<span class="hljs-keyword">while</span> ((route = routes.<span class="hljs-title function_">pop</span>()) != <span class="hljs-literal">null</span>) { |
| 4314 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">route</span>(route, <span class="hljs-variable language_">this</span>.<span class="hljs-property">routes</span>[route]); |
| 4315 |
} |
| 4316 |
},</pre></div></div> |
| 4317 |
|
| 4318 |
</li> |
| 4319 |
|
| 4320 |
|
| 4321 |
<li id="section-217"> |
| 4322 |
<div class="annotation"> |
| 4323 |
|
| 4324 |
<div class="sswrap "> |
| 4325 |
<a class="ss" href="#section-217">§</a> |
| 4326 |
</div> |
| 4327 |
<p>Convert a route string into a regular expression, suitable for matching |
| 4328 |
against the current location hash.</p> |
| 4329 |
|
| 4330 |
</div> |
| 4331 |
|
| 4332 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_routeToRegExp</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">route</span>) { |
| 4333 |
route = route.<span class="hljs-title function_">replace</span>(escapeRegExp, <span class="hljs-string">'\\$&'</span>) |
| 4334 |
.<span class="hljs-title function_">replace</span>(optionalParam, <span class="hljs-string">'(?:$1)?'</span>) |
| 4335 |
.<span class="hljs-title function_">replace</span>(namedParam, <span class="hljs-keyword">function</span>(<span class="hljs-params">match, optional</span>) { |
| 4336 |
<span class="hljs-keyword">return</span> optional ? match : <span class="hljs-string">'([^/?]+)'</span>; |
| 4337 |
}) |
| 4338 |
.<span class="hljs-title function_">replace</span>(splatParam, <span class="hljs-string">'([^?]*?)'</span>); |
| 4339 |
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RegExp</span>(<span class="hljs-string">'^'</span> + route + <span class="hljs-string">'(?:\\?([\\s\\S]*))?$'</span>); |
| 4340 |
},</pre></div></div> |
| 4341 |
|
| 4342 |
</li> |
| 4343 |
|
| 4344 |
|
| 4345 |
<li id="section-218"> |
| 4346 |
<div class="annotation"> |
| 4347 |
|
| 4348 |
<div class="sswrap "> |
| 4349 |
<a class="ss" href="#section-218">§</a> |
| 4350 |
</div> |
| 4351 |
<p>Given a route, and a URL fragment that it matches, return the array of |
| 4352 |
extracted decoded parameters. Empty or unmatched parameters will be |
| 4353 |
treated as <code>null</code> to normalize cross-browser behavior.</p> |
| 4354 |
|
| 4355 |
</div> |
| 4356 |
|
| 4357 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_extractParameters</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">route, fragment</span>) { |
| 4358 |
<span class="hljs-keyword">var</span> params = route.<span class="hljs-title function_">exec</span>(fragment).<span class="hljs-title function_">slice</span>(<span class="hljs-number">1</span>); |
| 4359 |
<span class="hljs-keyword">return</span> _.<span class="hljs-title function_">map</span>(params, <span class="hljs-keyword">function</span>(<span class="hljs-params">param, i</span>) {</pre></div></div> |
| 4360 |
|
| 4361 |
</li> |
| 4362 |
|
| 4363 |
|
| 4364 |
<li id="section-219"> |
| 4365 |
<div class="annotation"> |
| 4366 |
|
| 4367 |
<div class="sswrap "> |
| 4368 |
<a class="ss" href="#section-219">§</a> |
| 4369 |
</div> |
| 4370 |
<p>Don’t decode the search params.</p> |
| 4371 |
|
| 4372 |
</div> |
| 4373 |
|
| 4374 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (i === params.<span class="hljs-property">length</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> param || <span class="hljs-literal">null</span>; |
| 4375 |
<span class="hljs-keyword">return</span> param ? <span class="hljs-built_in">decodeURIComponent</span>(param) : <span class="hljs-literal">null</span>; |
| 4376 |
}); |
| 4377 |
} |
| 4378 |
|
| 4379 |
});</pre></div></div> |
| 4380 |
|
| 4381 |
</li> |
| 4382 |
|
| 4383 |
|
| 4384 |
<li id="section-220"> |
| 4385 |
<div class="annotation"> |
| 4386 |
|
| 4387 |
<div class="sswrap "> |
| 4388 |
<a class="ss" href="#section-220">§</a> |
| 4389 |
</div> |
| 4390 |
<h2 id="backbonehistory">Backbone.History</h2> |
| 4391 |
|
| 4392 |
</div> |
| 4393 |
|
| 4394 |
</li> |
| 4395 |
|
| 4396 |
|
| 4397 |
<li id="section-221"> |
| 4398 |
<div class="annotation"> |
| 4399 |
|
| 4400 |
<div class="sswrap "> |
| 4401 |
<a class="ss" href="#section-221">§</a> |
| 4402 |
</div> |
| 4403 |
|
| 4404 |
</div> |
| 4405 |
|
| 4406 |
</li> |
| 4407 |
|
| 4408 |
|
| 4409 |
<li id="section-222"> |
| 4410 |
<div class="annotation"> |
| 4411 |
|
| 4412 |
<div class="sswrap "> |
| 4413 |
<a class="ss" href="#section-222">§</a> |
| 4414 |
</div> |
| 4415 |
<p>Handles cross-browser history management, based on either |
| 4416 |
<a href="http://diveintohtml5.info/history.html">pushState</a> and real URLs, or |
| 4417 |
<a href="https://developer.mozilla.org/en-US/docs/DOM/window.onhashchange">onhashchange</a> |
| 4418 |
and URL fragments. If the browser supports neither (old IE, natch), |
| 4419 |
falls back to polling.</p> |
| 4420 |
|
| 4421 |
</div> |
| 4422 |
|
| 4423 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> <span class="hljs-title class_">History</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">History</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4424 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">handlers</span> = []; |
| 4425 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span>.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>);</pre></div></div> |
| 4426 |
|
| 4427 |
</li> |
| 4428 |
|
| 4429 |
|
| 4430 |
<li id="section-223"> |
| 4431 |
<div class="annotation"> |
| 4432 |
|
| 4433 |
<div class="sswrap "> |
| 4434 |
<a class="ss" href="#section-223">§</a> |
| 4435 |
</div> |
| 4436 |
<p>Ensure that <code>History</code> can be used outside of the browser.</p> |
| 4437 |
|
| 4438 |
</div> |
| 4439 |
|
| 4440 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> <span class="hljs-variable language_">window</span> !== <span class="hljs-string">'undefined'</span>) { |
| 4441 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span> = <span class="hljs-variable language_">window</span>.<span class="hljs-property">location</span>; |
| 4442 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">history</span> = <span class="hljs-variable language_">window</span>.<span class="hljs-property">history</span>; |
| 4443 |
} |
| 4444 |
};</pre></div></div> |
| 4445 |
|
| 4446 |
</li> |
| 4447 |
|
| 4448 |
|
| 4449 |
<li id="section-224"> |
| 4450 |
<div class="annotation"> |
| 4451 |
|
| 4452 |
<div class="sswrap "> |
| 4453 |
<a class="ss" href="#section-224">§</a> |
| 4454 |
</div> |
| 4455 |
<p>Cached regex for stripping a leading hash/slash and trailing space.</p> |
| 4456 |
|
| 4457 |
</div> |
| 4458 |
|
| 4459 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> routeStripper = <span class="hljs-regexp">/^[#\/]|\s+$/g</span>;</pre></div></div> |
| 4460 |
|
| 4461 |
</li> |
| 4462 |
|
| 4463 |
|
| 4464 |
<li id="section-225"> |
| 4465 |
<div class="annotation"> |
| 4466 |
|
| 4467 |
<div class="sswrap "> |
| 4468 |
<a class="ss" href="#section-225">§</a> |
| 4469 |
</div> |
| 4470 |
<p>Cached regex for stripping leading and trailing slashes.</p> |
| 4471 |
|
| 4472 |
</div> |
| 4473 |
|
| 4474 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> rootStripper = <span class="hljs-regexp">/^\/+|\/+$/g</span>;</pre></div></div> |
| 4475 |
|
| 4476 |
</li> |
| 4477 |
|
| 4478 |
|
| 4479 |
<li id="section-226"> |
| 4480 |
<div class="annotation"> |
| 4481 |
|
| 4482 |
<div class="sswrap "> |
| 4483 |
<a class="ss" href="#section-226">§</a> |
| 4484 |
</div> |
| 4485 |
<p>Cached regex for stripping urls of hash.</p> |
| 4486 |
|
| 4487 |
</div> |
| 4488 |
|
| 4489 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> pathStripper = <span class="hljs-regexp">/#.*$/</span>;</pre></div></div> |
| 4490 |
|
| 4491 |
</li> |
| 4492 |
|
| 4493 |
|
| 4494 |
<li id="section-227"> |
| 4495 |
<div class="annotation"> |
| 4496 |
|
| 4497 |
<div class="sswrap "> |
| 4498 |
<a class="ss" href="#section-227">§</a> |
| 4499 |
</div> |
| 4500 |
<p>Has the history handling already been started?</p> |
| 4501 |
|
| 4502 |
</div> |
| 4503 |
|
| 4504 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">History</span>.<span class="hljs-property">started</span> = <span class="hljs-literal">false</span>;</pre></div></div> |
| 4505 |
|
| 4506 |
</li> |
| 4507 |
|
| 4508 |
|
| 4509 |
<li id="section-228"> |
| 4510 |
<div class="annotation"> |
| 4511 |
|
| 4512 |
<div class="sswrap "> |
| 4513 |
<a class="ss" href="#section-228">§</a> |
| 4514 |
</div> |
| 4515 |
<p>Set up all inheritable <strong>Backbone.History</strong> properties and methods.</p> |
| 4516 |
|
| 4517 |
</div> |
| 4518 |
|
| 4519 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">History</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, <span class="hljs-title class_">Events</span>, {</pre></div></div> |
| 4520 |
|
| 4521 |
</li> |
| 4522 |
|
| 4523 |
|
| 4524 |
<li id="section-229"> |
| 4525 |
<div class="annotation"> |
| 4526 |
|
| 4527 |
<div class="sswrap "> |
| 4528 |
<a class="ss" href="#section-229">§</a> |
| 4529 |
</div> |
| 4530 |
<p>The default interval to poll for hash changes, if necessary, is |
| 4531 |
twenty times a second.</p> |
| 4532 |
|
| 4533 |
</div> |
| 4534 |
|
| 4535 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">interval</span>: <span class="hljs-number">50</span>,</pre></div></div> |
| 4536 |
|
| 4537 |
</li> |
| 4538 |
|
| 4539 |
|
| 4540 |
<li id="section-230"> |
| 4541 |
<div class="annotation"> |
| 4542 |
|
| 4543 |
<div class="sswrap "> |
| 4544 |
<a class="ss" href="#section-230">§</a> |
| 4545 |
</div> |
| 4546 |
<p>Are we at the app root?</p> |
| 4547 |
|
| 4548 |
</div> |
| 4549 |
|
| 4550 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">atRoot</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4551 |
<span class="hljs-keyword">var</span> path = <span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>.<span class="hljs-property">pathname</span>.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/[^\/]$/</span>, <span class="hljs-string">'$&/'</span>); |
| 4552 |
<span class="hljs-keyword">return</span> path === <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span> && !<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getSearch</span>(); |
| 4553 |
},</pre></div></div> |
| 4554 |
|
| 4555 |
</li> |
| 4556 |
|
| 4557 |
|
| 4558 |
<li id="section-231"> |
| 4559 |
<div class="annotation"> |
| 4560 |
|
| 4561 |
<div class="sswrap "> |
| 4562 |
<a class="ss" href="#section-231">§</a> |
| 4563 |
</div> |
| 4564 |
<p>Does the pathname match the root?</p> |
| 4565 |
|
| 4566 |
</div> |
| 4567 |
|
| 4568 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">matchRoot</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4569 |
<span class="hljs-keyword">var</span> path = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">decodeFragment</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>.<span class="hljs-property">pathname</span>); |
| 4570 |
<span class="hljs-keyword">var</span> rootPath = path.<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span>.<span class="hljs-property">length</span> - <span class="hljs-number">1</span>) + <span class="hljs-string">'/'</span>; |
| 4571 |
<span class="hljs-keyword">return</span> rootPath === <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span>; |
| 4572 |
},</pre></div></div> |
| 4573 |
|
| 4574 |
</li> |
| 4575 |
|
| 4576 |
|
| 4577 |
<li id="section-232"> |
| 4578 |
<div class="annotation"> |
| 4579 |
|
| 4580 |
<div class="sswrap "> |
| 4581 |
<a class="ss" href="#section-232">§</a> |
| 4582 |
</div> |
| 4583 |
<p>Unicode characters in <code>location.pathname</code> are percent encoded so they’re |
| 4584 |
decoded for comparison. <code>%25</code> should not be decoded since it may be part |
| 4585 |
of an encoded parameter.</p> |
| 4586 |
|
| 4587 |
</div> |
| 4588 |
|
| 4589 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">decodeFragment</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">fragment</span>) { |
| 4590 |
<span class="hljs-keyword">return</span> <span class="hljs-built_in">decodeURI</span>(fragment.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/%25/g</span>, <span class="hljs-string">'%2525'</span>)); |
| 4591 |
},</pre></div></div> |
| 4592 |
|
| 4593 |
</li> |
| 4594 |
|
| 4595 |
|
| 4596 |
<li id="section-233"> |
| 4597 |
<div class="annotation"> |
| 4598 |
|
| 4599 |
<div class="sswrap "> |
| 4600 |
<a class="ss" href="#section-233">§</a> |
| 4601 |
</div> |
| 4602 |
<p>In IE6, the hash fragment and search params are incorrect if the |
| 4603 |
fragment contains <code>?</code>.</p> |
| 4604 |
|
| 4605 |
</div> |
| 4606 |
|
| 4607 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">getSearch</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4608 |
<span class="hljs-keyword">var</span> match = <span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>.<span class="hljs-property">href</span>.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/#.*/</span>, <span class="hljs-string">''</span>).<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/\?.+/</span>); |
| 4609 |
<span class="hljs-keyword">return</span> match ? match[<span class="hljs-number">0</span>] : <span class="hljs-string">''</span>; |
| 4610 |
},</pre></div></div> |
| 4611 |
|
| 4612 |
</li> |
| 4613 |
|
| 4614 |
|
| 4615 |
<li id="section-234"> |
| 4616 |
<div class="annotation"> |
| 4617 |
|
| 4618 |
<div class="sswrap "> |
| 4619 |
<a class="ss" href="#section-234">§</a> |
| 4620 |
</div> |
| 4621 |
<p>Gets the true hash value. Cannot use location.hash directly due to bug |
| 4622 |
in Firefox where location.hash will always be decoded.</p> |
| 4623 |
|
| 4624 |
</div> |
| 4625 |
|
| 4626 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">getHash</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-variable language_">window</span></span>) { |
| 4627 |
<span class="hljs-keyword">var</span> match = (<span class="hljs-variable language_">window</span> || <span class="hljs-variable language_">this</span>).<span class="hljs-property">location</span>.<span class="hljs-property">href</span>.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/#(.*)$/</span>); |
| 4628 |
<span class="hljs-keyword">return</span> match ? match[<span class="hljs-number">1</span>] : <span class="hljs-string">''</span>; |
| 4629 |
},</pre></div></div> |
| 4630 |
|
| 4631 |
</li> |
| 4632 |
|
| 4633 |
|
| 4634 |
<li id="section-235"> |
| 4635 |
<div class="annotation"> |
| 4636 |
|
| 4637 |
<div class="sswrap "> |
| 4638 |
<a class="ss" href="#section-235">§</a> |
| 4639 |
</div> |
| 4640 |
<p>Get the pathname and search params, without the root.</p> |
| 4641 |
|
| 4642 |
</div> |
| 4643 |
|
| 4644 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">getPath</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 4645 |
<span class="hljs-keyword">var</span> path = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">decodeFragment</span>( |
| 4646 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>.<span class="hljs-property">pathname</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getSearch</span>() |
| 4647 |
).<span class="hljs-title function_">slice</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span>.<span class="hljs-property">length</span> - <span class="hljs-number">1</span>); |
| 4648 |
<span class="hljs-keyword">return</span> path.<span class="hljs-title function_">charAt</span>(<span class="hljs-number">0</span>) === <span class="hljs-string">'/'</span> ? path.<span class="hljs-title function_">slice</span>(<span class="hljs-number">1</span>) : path; |
| 4649 |
},</pre></div></div> |
| 4650 |
|
| 4651 |
</li> |
| 4652 |
|
| 4653 |
|
| 4654 |
<li id="section-236"> |
| 4655 |
<div class="annotation"> |
| 4656 |
|
| 4657 |
<div class="sswrap "> |
| 4658 |
<a class="ss" href="#section-236">§</a> |
| 4659 |
</div> |
| 4660 |
<p>Get the cross-browser normalized URL fragment from the path or hash.</p> |
| 4661 |
|
| 4662 |
</div> |
| 4663 |
|
| 4664 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">getFragment</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">fragment</span>) { |
| 4665 |
<span class="hljs-keyword">if</span> (fragment == <span class="hljs-literal">null</span>) { |
| 4666 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_usePushState</span> || !<span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span>) { |
| 4667 |
fragment = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getPath</span>(); |
| 4668 |
} <span class="hljs-keyword">else</span> { |
| 4669 |
fragment = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getHash</span>(); |
| 4670 |
} |
| 4671 |
} |
| 4672 |
<span class="hljs-keyword">return</span> fragment.<span class="hljs-title function_">replace</span>(routeStripper, <span class="hljs-string">''</span>); |
| 4673 |
},</pre></div></div> |
| 4674 |
|
| 4675 |
</li> |
| 4676 |
|
| 4677 |
|
| 4678 |
<li id="section-237"> |
| 4679 |
<div class="annotation"> |
| 4680 |
|
| 4681 |
<div class="sswrap "> |
| 4682 |
<a class="ss" href="#section-237">§</a> |
| 4683 |
</div> |
| 4684 |
<p>Start the hash change handling, returning <code>true</code> if the current URL matches |
| 4685 |
an existing route, and <code>false</code> otherwise.</p> |
| 4686 |
|
| 4687 |
</div> |
| 4688 |
|
| 4689 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">start</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">options</span>) { |
| 4690 |
<span class="hljs-keyword">if</span> (<span class="hljs-title class_">History</span>.<span class="hljs-property">started</span>) <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'Backbone.history has already been started'</span>); |
| 4691 |
<span class="hljs-title class_">History</span>.<span class="hljs-property">started</span> = <span class="hljs-literal">true</span>;</pre></div></div> |
| 4692 |
|
| 4693 |
</li> |
| 4694 |
|
| 4695 |
|
| 4696 |
<li id="section-238"> |
| 4697 |
<div class="annotation"> |
| 4698 |
|
| 4699 |
<div class="sswrap "> |
| 4700 |
<a class="ss" href="#section-238">§</a> |
| 4701 |
</div> |
| 4702 |
<p>Figure out the initial configuration. Do we need an iframe? |
| 4703 |
Is pushState desired … is it available?</p> |
| 4704 |
|
| 4705 |
</div> |
| 4706 |
|
| 4707 |
<div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">options</span> = _.<span class="hljs-title function_">extend</span>({<span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>}, <span class="hljs-variable language_">this</span>.<span class="hljs-property">options</span>, options); |
| 4708 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">options</span>.<span class="hljs-property">root</span>; |
| 4709 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">options</span>.<span class="hljs-property">hashChange</span> !== <span class="hljs-literal">false</span>; |
| 4710 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasHashChange</span> = <span class="hljs-string">'onhashchange'</span> <span class="hljs-keyword">in</span> <span class="hljs-variable language_">window</span> && (<span class="hljs-variable language_">document</span>.<span class="hljs-property">documentMode</span> === <span class="hljs-keyword">void</span> <span class="hljs-number">0</span> || <span class="hljs-variable language_">document</span>.<span class="hljs-property">documentMode</span> > <span class="hljs-number">7</span>); |
| 4711 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_useHashChange</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasHashChange</span>; |
| 4712 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsPushState</span> = !!<span class="hljs-variable language_">this</span>.<span class="hljs-property">options</span>.<span class="hljs-property">pushState</span>; |
| 4713 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasPushState</span> = !!(<span class="hljs-variable language_">this</span>.<span class="hljs-property">history</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">history</span>.<span class="hljs-property">pushState</span>); |
| 4714 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_usePushState</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsPushState</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasPushState</span>; |
| 4715 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getFragment</span>();</pre></div></div> |
| 4716 |
|
| 4717 |
</li> |
| 4718 |
|
| 4719 |
|
| 4720 |
<li id="section-239"> |
| 4721 |
<div class="annotation"> |
| 4722 |
|
| 4723 |
<div class="sswrap "> |
| 4724 |
<a class="ss" href="#section-239">§</a> |
| 4725 |
</div> |
| 4726 |
<p>Normalize root to always include a leading and trailing slash.</p> |
| 4727 |
|
| 4728 |
</div> |
| 4729 |
|
| 4730 |
<div class="content"><div class='highlight'><pre> <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span> = (<span class="hljs-string">'/'</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span> + <span class="hljs-string">'/'</span>).<span class="hljs-title function_">replace</span>(rootStripper, <span class="hljs-string">'/'</span>);</pre></div></div> |
| 4731 |
|
| 4732 |
</li> |
| 4733 |
|
| 4734 |
|
| 4735 |
<li id="section-240"> |
| 4736 |
<div class="annotation"> |
| 4737 |
|
| 4738 |
<div class="sswrap "> |
| 4739 |
<a class="ss" href="#section-240">§</a> |
| 4740 |
</div> |
| 4741 |
<p>Transition from hashChange to pushState or vice versa if both are |
| 4742 |
requested.</p> |
| 4743 |
|
| 4744 |
</div> |
| 4745 |
|
| 4746 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsPushState</span>) {</pre></div></div> |
| 4747 |
|
| 4748 |
</li> |
| 4749 |
|
| 4750 |
|
| 4751 |
<li id="section-241"> |
| 4752 |
<div class="annotation"> |
| 4753 |
|
| 4754 |
<div class="sswrap "> |
| 4755 |
<a class="ss" href="#section-241">§</a> |
| 4756 |
</div> |
| 4757 |
<p>If we’ve started off with a route from a <code>pushState</code>-enabled |
| 4758 |
browser, but we’re currently in a browser that doesn’t support it…</p> |
| 4759 |
|
| 4760 |
</div> |
| 4761 |
|
| 4762 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasPushState</span> && !<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">atRoot</span>()) { |
| 4763 |
<span class="hljs-keyword">var</span> rootPath = <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span>.<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>) || <span class="hljs-string">'/'</span>; |
| 4764 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>.<span class="hljs-title function_">replace</span>(rootPath + <span class="hljs-string">'#'</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getPath</span>());</pre></div></div> |
| 4765 |
|
| 4766 |
</li> |
| 4767 |
|
| 4768 |
|
| 4769 |
<li id="section-242"> |
| 4770 |
<div class="annotation"> |
| 4771 |
|
| 4772 |
<div class="sswrap "> |
| 4773 |
<a class="ss" href="#section-242">§</a> |
| 4774 |
</div> |
| 4775 |
<p>Return immediately as browser will do redirect to new url</p> |
| 4776 |
|
| 4777 |
</div> |
| 4778 |
|
| 4779 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;</pre></div></div> |
| 4780 |
|
| 4781 |
</li> |
| 4782 |
|
| 4783 |
|
| 4784 |
<li id="section-243"> |
| 4785 |
<div class="annotation"> |
| 4786 |
|
| 4787 |
<div class="sswrap "> |
| 4788 |
<a class="ss" href="#section-243">§</a> |
| 4789 |
</div> |
| 4790 |
<p>Or if we’ve started out with a hash-based route, but we’re currently |
| 4791 |
in a browser where it could be <code>pushState</code>-based instead…</p> |
| 4792 |
|
| 4793 |
</div> |
| 4794 |
|
| 4795 |
<div class="content"><div class='highlight'><pre> } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasPushState</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">atRoot</span>()) { |
| 4796 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">navigate</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getHash</span>(), {<span class="hljs-attr">replace</span>: <span class="hljs-literal">true</span>}); |
| 4797 |
} |
| 4798 |
|
| 4799 |
}</pre></div></div> |
| 4800 |
|
| 4801 |
</li> |
| 4802 |
|
| 4803 |
|
| 4804 |
<li id="section-244"> |
| 4805 |
<div class="annotation"> |
| 4806 |
|
| 4807 |
<div class="sswrap "> |
| 4808 |
<a class="ss" href="#section-244">§</a> |
| 4809 |
</div> |
| 4810 |
<p>Proxy an iframe to handle location events if the browser doesn’t |
| 4811 |
support the <code>hashchange</code> event, HTML5 history, or the user wants |
| 4812 |
<code>hashChange</code> but not <code>pushState</code>.</p> |
| 4813 |
|
| 4814 |
</div> |
| 4815 |
|
| 4816 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">_hasHashChange</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span> && !<span class="hljs-variable language_">this</span>.<span class="hljs-property">_usePushState</span>) { |
| 4817 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span> = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">'iframe'</span>); |
| 4818 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>.<span class="hljs-property">src</span> = <span class="hljs-string">'javascript:0'</span>; |
| 4819 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>.<span class="hljs-property">style</span>.<span class="hljs-property">display</span> = <span class="hljs-string">'none'</span>; |
| 4820 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>.<span class="hljs-property">tabIndex</span> = -<span class="hljs-number">1</span>; |
| 4821 |
<span class="hljs-keyword">var</span> body = <span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>;</pre></div></div> |
| 4822 |
|
| 4823 |
</li> |
| 4824 |
|
| 4825 |
|
| 4826 |
<li id="section-245"> |
| 4827 |
<div class="annotation"> |
| 4828 |
|
| 4829 |
<div class="sswrap "> |
| 4830 |
<a class="ss" href="#section-245">§</a> |
| 4831 |
</div> |
| 4832 |
<p>Using <code>appendChild</code> will throw on IE < 9 if the document is not ready.</p> |
| 4833 |
|
| 4834 |
</div> |
| 4835 |
|
| 4836 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> iWindow = body.<span class="hljs-title function_">insertBefore</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>, body.<span class="hljs-property">firstChild</span>).<span class="hljs-property">contentWindow</span>; |
| 4837 |
iWindow.<span class="hljs-property">document</span>.<span class="hljs-title function_">open</span>(); |
| 4838 |
iWindow.<span class="hljs-property">document</span>.<span class="hljs-title function_">close</span>(); |
| 4839 |
iWindow.<span class="hljs-property">location</span>.<span class="hljs-property">hash</span> = <span class="hljs-string">'#'</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span>; |
| 4840 |
}</pre></div></div> |
| 4841 |
|
| 4842 |
</li> |
| 4843 |
|
| 4844 |
|
| 4845 |
<li id="section-246"> |
| 4846 |
<div class="annotation"> |
| 4847 |
|
| 4848 |
<div class="sswrap "> |
| 4849 |
<a class="ss" href="#section-246">§</a> |
| 4850 |
</div> |
| 4851 |
<p>Add a cross-platform <code>addEventListener</code> shim for older browsers.</p> |
| 4852 |
|
| 4853 |
</div> |
| 4854 |
|
| 4855 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> addEventListener = <span class="hljs-variable language_">window</span>.<span class="hljs-property">addEventListener</span> || <span class="hljs-keyword">function</span>(<span class="hljs-params">eventName, listener</span>) { |
| 4856 |
<span class="hljs-keyword">return</span> <span class="hljs-title function_">attachEvent</span>(<span class="hljs-string">'on'</span> + eventName, listener); |
| 4857 |
};</pre></div></div> |
| 4858 |
|
| 4859 |
</li> |
| 4860 |
|
| 4861 |
|
| 4862 |
<li id="section-247"> |
| 4863 |
<div class="annotation"> |
| 4864 |
|
| 4865 |
<div class="sswrap "> |
| 4866 |
<a class="ss" href="#section-247">§</a> |
| 4867 |
</div> |
| 4868 |
<p>Depending on whether we’re using pushState or hashes, and whether |
| 4869 |
‘onhashchange’ is supported, determine how we check the URL state.</p> |
| 4870 |
|
| 4871 |
</div> |
| 4872 |
|
| 4873 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_usePushState</span>) { |
| 4874 |
<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'popstate'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span>, <span class="hljs-literal">false</span>); |
| 4875 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_useHashChange</span> && !<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>) { |
| 4876 |
<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'hashchange'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span>, <span class="hljs-literal">false</span>); |
| 4877 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span>) { |
| 4878 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">_checkUrlInterval</span> = <span class="hljs-built_in">setInterval</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">interval</span>); |
| 4879 |
} |
| 4880 |
|
| 4881 |
<span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-property">options</span>.<span class="hljs-property">silent</span>) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">loadUrl</span>(); |
| 4882 |
},</pre></div></div> |
| 4883 |
|
| 4884 |
</li> |
| 4885 |
|
| 4886 |
|
| 4887 |
<li id="section-248"> |
| 4888 |
<div class="annotation"> |
| 4889 |
|
| 4890 |
<div class="sswrap "> |
| 4891 |
<a class="ss" href="#section-248">§</a> |
| 4892 |
</div> |
| 4893 |
<p>Disable Backbone.history, perhaps temporarily. Not useful in a real app, |
| 4894 |
but possibly useful for unit testing Routers.</p> |
| 4895 |
|
| 4896 |
</div> |
| 4897 |
|
| 4898 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">stop</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {</pre></div></div> |
| 4899 |
|
| 4900 |
</li> |
| 4901 |
|
| 4902 |
|
| 4903 |
<li id="section-249"> |
| 4904 |
<div class="annotation"> |
| 4905 |
|
| 4906 |
<div class="sswrap "> |
| 4907 |
<a class="ss" href="#section-249">§</a> |
| 4908 |
</div> |
| 4909 |
<p>Add a cross-platform <code>removeEventListener</code> shim for older browsers.</p> |
| 4910 |
|
| 4911 |
</div> |
| 4912 |
|
| 4913 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> removeEventListener = <span class="hljs-variable language_">window</span>.<span class="hljs-property">removeEventListener</span> || <span class="hljs-keyword">function</span>(<span class="hljs-params">eventName, listener</span>) { |
| 4914 |
<span class="hljs-keyword">return</span> <span class="hljs-title function_">detachEvent</span>(<span class="hljs-string">'on'</span> + eventName, listener); |
| 4915 |
};</pre></div></div> |
| 4916 |
|
| 4917 |
</li> |
| 4918 |
|
| 4919 |
|
| 4920 |
<li id="section-250"> |
| 4921 |
<div class="annotation"> |
| 4922 |
|
| 4923 |
<div class="sswrap "> |
| 4924 |
<a class="ss" href="#section-250">§</a> |
| 4925 |
</div> |
| 4926 |
<p>Remove window listeners.</p> |
| 4927 |
|
| 4928 |
</div> |
| 4929 |
|
| 4930 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_usePushState</span>) { |
| 4931 |
<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'popstate'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span>, <span class="hljs-literal">false</span>); |
| 4932 |
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_useHashChange</span> && !<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>) { |
| 4933 |
<span class="hljs-title function_">removeEventListener</span>(<span class="hljs-string">'hashchange'</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">checkUrl</span>, <span class="hljs-literal">false</span>); |
| 4934 |
}</pre></div></div> |
| 4935 |
|
| 4936 |
</li> |
| 4937 |
|
| 4938 |
|
| 4939 |
<li id="section-251"> |
| 4940 |
<div class="annotation"> |
| 4941 |
|
| 4942 |
<div class="sswrap "> |
| 4943 |
<a class="ss" href="#section-251">§</a> |
| 4944 |
</div> |
| 4945 |
<p>Clean up the iframe if necessary.</p> |
| 4946 |
|
| 4947 |
</div> |
| 4948 |
|
| 4949 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>) { |
| 4950 |
<span class="hljs-variable language_">document</span>.<span class="hljs-property">body</span>.<span class="hljs-title function_">removeChild</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>); |
| 4951 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span> = <span class="hljs-literal">null</span>; |
| 4952 |
}</pre></div></div> |
| 4953 |
|
| 4954 |
</li> |
| 4955 |
|
| 4956 |
|
| 4957 |
<li id="section-252"> |
| 4958 |
<div class="annotation"> |
| 4959 |
|
| 4960 |
<div class="sswrap "> |
| 4961 |
<a class="ss" href="#section-252">§</a> |
| 4962 |
</div> |
| 4963 |
<p>Some environments will throw when clearing an undefined interval.</p> |
| 4964 |
|
| 4965 |
</div> |
| 4966 |
|
| 4967 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_checkUrlInterval</span>) <span class="hljs-built_in">clearInterval</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">_checkUrlInterval</span>); |
| 4968 |
<span class="hljs-title class_">History</span>.<span class="hljs-property">started</span> = <span class="hljs-literal">false</span>; |
| 4969 |
},</pre></div></div> |
| 4970 |
|
| 4971 |
</li> |
| 4972 |
|
| 4973 |
|
| 4974 |
<li id="section-253"> |
| 4975 |
<div class="annotation"> |
| 4976 |
|
| 4977 |
<div class="sswrap "> |
| 4978 |
<a class="ss" href="#section-253">§</a> |
| 4979 |
</div> |
| 4980 |
<p>Add a route to be tested when the fragment changes. Routes added later |
| 4981 |
may override previous routes.</p> |
| 4982 |
|
| 4983 |
</div> |
| 4984 |
|
| 4985 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">route</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">route, callback</span>) { |
| 4986 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">handlers</span>.<span class="hljs-title function_">unshift</span>({<span class="hljs-attr">route</span>: route, <span class="hljs-attr">callback</span>: callback}); |
| 4987 |
},</pre></div></div> |
| 4988 |
|
| 4989 |
</li> |
| 4990 |
|
| 4991 |
|
| 4992 |
<li id="section-254"> |
| 4993 |
<div class="annotation"> |
| 4994 |
|
| 4995 |
<div class="sswrap "> |
| 4996 |
<a class="ss" href="#section-254">§</a> |
| 4997 |
</div> |
| 4998 |
<p>Checks the current URL to see if it has changed, and if it has, |
| 4999 |
calls <code>loadUrl</code>, normalizing across the hidden iframe.</p> |
| 5000 |
|
| 5001 |
</div> |
| 5002 |
|
| 5003 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">checkUrl</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) { |
| 5004 |
<span class="hljs-keyword">var</span> current = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getFragment</span>();</pre></div></div> |
| 5005 |
|
| 5006 |
</li> |
| 5007 |
|
| 5008 |
|
| 5009 |
<li id="section-255"> |
| 5010 |
<div class="annotation"> |
| 5011 |
|
| 5012 |
<div class="sswrap "> |
| 5013 |
<a class="ss" href="#section-255">§</a> |
| 5014 |
</div> |
| 5015 |
<p>If the user pressed the back button, the iframe’s hash will have |
| 5016 |
changed and we should use that for comparison.</p> |
| 5017 |
|
| 5018 |
</div> |
| 5019 |
|
| 5020 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (current === <span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span> && <span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>) { |
| 5021 |
current = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getHash</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>.<span class="hljs-property">contentWindow</span>); |
| 5022 |
} |
| 5023 |
|
| 5024 |
<span class="hljs-keyword">if</span> (current === <span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 5025 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>) <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">navigate</span>(current); |
| 5026 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">loadUrl</span>(); |
| 5027 |
},</pre></div></div> |
| 5028 |
|
| 5029 |
</li> |
| 5030 |
|
| 5031 |
|
| 5032 |
<li id="section-256"> |
| 5033 |
<div class="annotation"> |
| 5034 |
|
| 5035 |
<div class="sswrap "> |
| 5036 |
<a class="ss" href="#section-256">§</a> |
| 5037 |
</div> |
| 5038 |
<p>Attempt to load the current URL fragment. If a route succeeds with a |
| 5039 |
match, returns <code>true</code>. If no defined routes matches the fragment, |
| 5040 |
returns <code>false</code>.</p> |
| 5041 |
|
| 5042 |
</div> |
| 5043 |
|
| 5044 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">loadUrl</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">fragment</span>) {</pre></div></div> |
| 5045 |
|
| 5046 |
</li> |
| 5047 |
|
| 5048 |
|
| 5049 |
<li id="section-257"> |
| 5050 |
<div class="annotation"> |
| 5051 |
|
| 5052 |
<div class="sswrap "> |
| 5053 |
<a class="ss" href="#section-257">§</a> |
| 5054 |
</div> |
| 5055 |
<p>If the root doesn’t match, no routes can match either.</p> |
| 5056 |
|
| 5057 |
</div> |
| 5058 |
|
| 5059 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">matchRoot</span>()) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 5060 |
fragment = <span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getFragment</span>(fragment); |
| 5061 |
<span class="hljs-keyword">return</span> _.<span class="hljs-title function_">some</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">handlers</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">handler</span>) { |
| 5062 |
<span class="hljs-keyword">if</span> (handler.<span class="hljs-property">route</span>.<span class="hljs-title function_">test</span>(fragment)) { |
| 5063 |
handler.<span class="hljs-title function_">callback</span>(fragment); |
| 5064 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; |
| 5065 |
} |
| 5066 |
}); |
| 5067 |
},</pre></div></div> |
| 5068 |
|
| 5069 |
</li> |
| 5070 |
|
| 5071 |
|
| 5072 |
<li id="section-258"> |
| 5073 |
<div class="annotation"> |
| 5074 |
|
| 5075 |
<div class="sswrap "> |
| 5076 |
<a class="ss" href="#section-258">§</a> |
| 5077 |
</div> |
| 5078 |
<p>Save a fragment into the hash history, or replace the URL state if the |
| 5079 |
‘replace’ option is passed. You are responsible for properly URL-encoding |
| 5080 |
the fragment in advance.</p> |
| 5081 |
<p>The options object can contain <code>trigger: true</code> if you wish to have the |
| 5082 |
route callback be fired (not usually desirable), or <code>replace: true</code>, if |
| 5083 |
you wish to modify the current URL without adding an entry to the history.</p> |
| 5084 |
|
| 5085 |
</div> |
| 5086 |
|
| 5087 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">navigate</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">fragment, options</span>) { |
| 5088 |
<span class="hljs-keyword">if</span> (!<span class="hljs-title class_">History</span>.<span class="hljs-property">started</span>) <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; |
| 5089 |
<span class="hljs-keyword">if</span> (!options || options === <span class="hljs-literal">true</span>) options = {<span class="hljs-attr">trigger</span>: !!options};</pre></div></div> |
| 5090 |
|
| 5091 |
</li> |
| 5092 |
|
| 5093 |
|
| 5094 |
<li id="section-259"> |
| 5095 |
<div class="annotation"> |
| 5096 |
|
| 5097 |
<div class="sswrap "> |
| 5098 |
<a class="ss" href="#section-259">§</a> |
| 5099 |
</div> |
| 5100 |
<p>Normalize the fragment.</p> |
| 5101 |
|
| 5102 |
</div> |
| 5103 |
|
| 5104 |
<div class="content"><div class='highlight'><pre> fragment = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getFragment</span>(fragment || <span class="hljs-string">''</span>);</pre></div></div> |
| 5105 |
|
| 5106 |
</li> |
| 5107 |
|
| 5108 |
|
| 5109 |
<li id="section-260"> |
| 5110 |
<div class="annotation"> |
| 5111 |
|
| 5112 |
<div class="sswrap "> |
| 5113 |
<a class="ss" href="#section-260">§</a> |
| 5114 |
</div> |
| 5115 |
<p>Don’t include a trailing slash on the root.</p> |
| 5116 |
|
| 5117 |
</div> |
| 5118 |
|
| 5119 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> rootPath = <span class="hljs-variable language_">this</span>.<span class="hljs-property">root</span>; |
| 5120 |
<span class="hljs-keyword">if</span> (fragment === <span class="hljs-string">''</span> || fragment.<span class="hljs-title function_">charAt</span>(<span class="hljs-number">0</span>) === <span class="hljs-string">'?'</span>) { |
| 5121 |
rootPath = rootPath.<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>) || <span class="hljs-string">'/'</span>; |
| 5122 |
} |
| 5123 |
<span class="hljs-keyword">var</span> url = rootPath + fragment;</pre></div></div> |
| 5124 |
|
| 5125 |
</li> |
| 5126 |
|
| 5127 |
|
| 5128 |
<li id="section-261"> |
| 5129 |
<div class="annotation"> |
| 5130 |
|
| 5131 |
<div class="sswrap "> |
| 5132 |
<a class="ss" href="#section-261">§</a> |
| 5133 |
</div> |
| 5134 |
<p>Strip the fragment of the query and hash for matching.</p> |
| 5135 |
|
| 5136 |
</div> |
| 5137 |
|
| 5138 |
<div class="content"><div class='highlight'><pre> fragment = fragment.<span class="hljs-title function_">replace</span>(pathStripper, <span class="hljs-string">''</span>);</pre></div></div> |
| 5139 |
|
| 5140 |
</li> |
| 5141 |
|
| 5142 |
|
| 5143 |
<li id="section-262"> |
| 5144 |
<div class="annotation"> |
| 5145 |
|
| 5146 |
<div class="sswrap "> |
| 5147 |
<a class="ss" href="#section-262">§</a> |
| 5148 |
</div> |
| 5149 |
<p>Decode for matching.</p> |
| 5150 |
|
| 5151 |
</div> |
| 5152 |
|
| 5153 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> decodedFragment = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">decodeFragment</span>(fragment); |
| 5154 |
|
| 5155 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span> === decodedFragment) <span class="hljs-keyword">return</span>; |
| 5156 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">fragment</span> = decodedFragment;</pre></div></div> |
| 5157 |
|
| 5158 |
</li> |
| 5159 |
|
| 5160 |
|
| 5161 |
<li id="section-263"> |
| 5162 |
<div class="annotation"> |
| 5163 |
|
| 5164 |
<div class="sswrap "> |
| 5165 |
<a class="ss" href="#section-263">§</a> |
| 5166 |
</div> |
| 5167 |
<p>If pushState is available, we use it to set the fragment as a real URL.</p> |
| 5168 |
|
| 5169 |
</div> |
| 5170 |
|
| 5171 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_usePushState</span>) { |
| 5172 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">history</span>[options.<span class="hljs-property">replace</span> ? <span class="hljs-string">'replaceState'</span> : <span class="hljs-string">'pushState'</span>]({}, <span class="hljs-variable language_">document</span>.<span class="hljs-property">title</span>, url);</pre></div></div> |
| 5173 |
|
| 5174 |
</li> |
| 5175 |
|
| 5176 |
|
| 5177 |
<li id="section-264"> |
| 5178 |
<div class="annotation"> |
| 5179 |
|
| 5180 |
<div class="sswrap "> |
| 5181 |
<a class="ss" href="#section-264">§</a> |
| 5182 |
</div> |
| 5183 |
<p>If hash changes haven’t been explicitly disabled, update the hash |
| 5184 |
fragment to store history.</p> |
| 5185 |
|
| 5186 |
</div> |
| 5187 |
|
| 5188 |
<div class="content"><div class='highlight'><pre> } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">_wantsHashChange</span>) { |
| 5189 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_updateHash</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>, fragment, options.<span class="hljs-property">replace</span>); |
| 5190 |
<span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span> && fragment !== <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">getHash</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>.<span class="hljs-property">contentWindow</span>)) { |
| 5191 |
<span class="hljs-keyword">var</span> iWindow = <span class="hljs-variable language_">this</span>.<span class="hljs-property">iframe</span>.<span class="hljs-property">contentWindow</span>;</pre></div></div> |
| 5192 |
|
| 5193 |
</li> |
| 5194 |
|
| 5195 |
|
| 5196 |
<li id="section-265"> |
| 5197 |
<div class="annotation"> |
| 5198 |
|
| 5199 |
<div class="sswrap "> |
| 5200 |
<a class="ss" href="#section-265">§</a> |
| 5201 |
</div> |
| 5202 |
<p>Opening and closing the iframe tricks IE7 and earlier to push a |
| 5203 |
history entry on hash-tag change. When replace is true, we don’t |
| 5204 |
want this.</p> |
| 5205 |
|
| 5206 |
</div> |
| 5207 |
|
| 5208 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (!options.<span class="hljs-property">replace</span>) { |
| 5209 |
iWindow.<span class="hljs-property">document</span>.<span class="hljs-title function_">open</span>(); |
| 5210 |
iWindow.<span class="hljs-property">document</span>.<span class="hljs-title function_">close</span>(); |
| 5211 |
} |
| 5212 |
|
| 5213 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">_updateHash</span>(iWindow.<span class="hljs-property">location</span>, fragment, options.<span class="hljs-property">replace</span>); |
| 5214 |
}</pre></div></div> |
| 5215 |
|
| 5216 |
</li> |
| 5217 |
|
| 5218 |
|
| 5219 |
<li id="section-266"> |
| 5220 |
<div class="annotation"> |
| 5221 |
|
| 5222 |
<div class="sswrap "> |
| 5223 |
<a class="ss" href="#section-266">§</a> |
| 5224 |
</div> |
| 5225 |
<p>If you’ve told us that you explicitly don’t want fallback hashchange- |
| 5226 |
based history, then <code>navigate</code> becomes a page refresh.</p> |
| 5227 |
|
| 5228 |
</div> |
| 5229 |
|
| 5230 |
<div class="content"><div class='highlight'><pre> } <span class="hljs-keyword">else</span> { |
| 5231 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">location</span>.<span class="hljs-title function_">assign</span>(url); |
| 5232 |
} |
| 5233 |
<span class="hljs-keyword">if</span> (options.<span class="hljs-property">trigger</span>) <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">loadUrl</span>(fragment); |
| 5234 |
},</pre></div></div> |
| 5235 |
|
| 5236 |
</li> |
| 5237 |
|
| 5238 |
|
| 5239 |
<li id="section-267"> |
| 5240 |
<div class="annotation"> |
| 5241 |
|
| 5242 |
<div class="sswrap "> |
| 5243 |
<a class="ss" href="#section-267">§</a> |
| 5244 |
</div> |
| 5245 |
<p>Update the hash location, either replacing the current entry, or adding |
| 5246 |
a new one to the browser history.</p> |
| 5247 |
|
| 5248 |
</div> |
| 5249 |
|
| 5250 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">_updateHash</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">location, fragment, replace</span>) { |
| 5251 |
<span class="hljs-keyword">if</span> (replace) { |
| 5252 |
<span class="hljs-keyword">var</span> href = location.<span class="hljs-property">href</span>.<span class="hljs-title function_">replace</span>(<span class="hljs-regexp">/(javascript:|#).*$/</span>, <span class="hljs-string">''</span>); |
| 5253 |
location.<span class="hljs-title function_">replace</span>(href + <span class="hljs-string">'#'</span> + fragment); |
| 5254 |
} <span class="hljs-keyword">else</span> {</pre></div></div> |
| 5255 |
|
| 5256 |
</li> |
| 5257 |
|
| 5258 |
|
| 5259 |
<li id="section-268"> |
| 5260 |
<div class="annotation"> |
| 5261 |
|
| 5262 |
<div class="sswrap "> |
| 5263 |
<a class="ss" href="#section-268">§</a> |
| 5264 |
</div> |
| 5265 |
<p>Some browsers require that <code>hash</code> contains a leading #.</p> |
| 5266 |
|
| 5267 |
</div> |
| 5268 |
|
| 5269 |
<div class="content"><div class='highlight'><pre> location.<span class="hljs-property">hash</span> = <span class="hljs-string">'#'</span> + fragment; |
| 5270 |
} |
| 5271 |
} |
| 5272 |
|
| 5273 |
});</pre></div></div> |
| 5274 |
|
| 5275 |
</li> |
| 5276 |
|
| 5277 |
|
| 5278 |
<li id="section-269"> |
| 5279 |
<div class="annotation"> |
| 5280 |
|
| 5281 |
<div class="sswrap "> |
| 5282 |
<a class="ss" href="#section-269">§</a> |
| 5283 |
</div> |
| 5284 |
<p>Create the default Backbone.history.</p> |
| 5285 |
|
| 5286 |
</div> |
| 5287 |
|
| 5288 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">history</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">History</span>;</pre></div></div> |
| 5289 |
|
| 5290 |
</li> |
| 5291 |
|
| 5292 |
|
| 5293 |
<li id="section-270"> |
| 5294 |
<div class="annotation"> |
| 5295 |
|
| 5296 |
<div class="sswrap "> |
| 5297 |
<a class="ss" href="#section-270">§</a> |
| 5298 |
</div> |
| 5299 |
<h2 id="helpers">Helpers</h2> |
| 5300 |
|
| 5301 |
</div> |
| 5302 |
|
| 5303 |
</li> |
| 5304 |
|
| 5305 |
|
| 5306 |
<li id="section-271"> |
| 5307 |
<div class="annotation"> |
| 5308 |
|
| 5309 |
<div class="sswrap "> |
| 5310 |
<a class="ss" href="#section-271">§</a> |
| 5311 |
</div> |
| 5312 |
|
| 5313 |
</div> |
| 5314 |
|
| 5315 |
</li> |
| 5316 |
|
| 5317 |
|
| 5318 |
<li id="section-272"> |
| 5319 |
<div class="annotation"> |
| 5320 |
|
| 5321 |
<div class="sswrap "> |
| 5322 |
<a class="ss" href="#section-272">§</a> |
| 5323 |
</div> |
| 5324 |
<p>Helper function to correctly set up the prototype chain for subclasses. |
| 5325 |
Similar to <code>goog.inherits</code>, but uses a hash of prototype properties and |
| 5326 |
class properties to be extended.</p> |
| 5327 |
|
| 5328 |
</div> |
| 5329 |
|
| 5330 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> extend = <span class="hljs-keyword">function</span>(<span class="hljs-params">protoProps, staticProps</span>) { |
| 5331 |
<span class="hljs-keyword">var</span> parent = <span class="hljs-variable language_">this</span>; |
| 5332 |
<span class="hljs-keyword">var</span> child;</pre></div></div> |
| 5333 |
|
| 5334 |
</li> |
| 5335 |
|
| 5336 |
|
| 5337 |
<li id="section-273"> |
| 5338 |
<div class="annotation"> |
| 5339 |
|
| 5340 |
<div class="sswrap "> |
| 5341 |
<a class="ss" href="#section-273">§</a> |
| 5342 |
</div> |
| 5343 |
<p>The constructor function for the new subclass is either defined by you |
| 5344 |
(the “constructor” property in your <code>extend</code> definition), or defaulted |
| 5345 |
by us to simply call the parent constructor.</p> |
| 5346 |
|
| 5347 |
</div> |
| 5348 |
|
| 5349 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (protoProps && _.<span class="hljs-title function_">has</span>(protoProps, <span class="hljs-string">'constructor'</span>)) { |
| 5350 |
child = protoProps.<span class="hljs-property">constructor</span>; |
| 5351 |
} <span class="hljs-keyword">else</span> { |
| 5352 |
child = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>){ <span class="hljs-keyword">return</span> parent.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, <span class="hljs-variable language_">arguments</span>); }; |
| 5353 |
}</pre></div></div> |
| 5354 |
|
| 5355 |
</li> |
| 5356 |
|
| 5357 |
|
| 5358 |
<li id="section-274"> |
| 5359 |
<div class="annotation"> |
| 5360 |
|
| 5361 |
<div class="sswrap "> |
| 5362 |
<a class="ss" href="#section-274">§</a> |
| 5363 |
</div> |
| 5364 |
<p>Add static properties to the constructor function, if supplied.</p> |
| 5365 |
|
| 5366 |
</div> |
| 5367 |
|
| 5368 |
<div class="content"><div class='highlight'><pre> _.<span class="hljs-title function_">extend</span>(child, parent, staticProps);</pre></div></div> |
| 5369 |
|
| 5370 |
</li> |
| 5371 |
|
| 5372 |
|
| 5373 |
<li id="section-275"> |
| 5374 |
<div class="annotation"> |
| 5375 |
|
| 5376 |
<div class="sswrap "> |
| 5377 |
<a class="ss" href="#section-275">§</a> |
| 5378 |
</div> |
| 5379 |
<p>Set the prototype chain to inherit from <code>parent</code>, without calling |
| 5380 |
<code>parent</code>‘s constructor function and add the prototype properties.</p> |
| 5381 |
|
| 5382 |
</div> |
| 5383 |
|
| 5384 |
<div class="content"><div class='highlight'><pre> child.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = _.<span class="hljs-title function_">create</span>(parent.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, protoProps); |
| 5385 |
child.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">constructor</span> = child;</pre></div></div> |
| 5386 |
|
| 5387 |
</li> |
| 5388 |
|
| 5389 |
|
| 5390 |
<li id="section-276"> |
| 5391 |
<div class="annotation"> |
| 5392 |
|
| 5393 |
<div class="sswrap "> |
| 5394 |
<a class="ss" href="#section-276">§</a> |
| 5395 |
</div> |
| 5396 |
<p>Set a convenience property in case the parent’s prototype is needed |
| 5397 |
later.</p> |
| 5398 |
|
| 5399 |
</div> |
| 5400 |
|
| 5401 |
<div class="content"><div class='highlight'><pre> child.<span class="hljs-property">__super__</span> = parent.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>; |
| 5402 |
|
| 5403 |
<span class="hljs-keyword">return</span> child; |
| 5404 |
};</pre></div></div> |
| 5405 |
|
| 5406 |
</li> |
| 5407 |
|
| 5408 |
|
| 5409 |
<li id="section-277"> |
| 5410 |
<div class="annotation"> |
| 5411 |
|
| 5412 |
<div class="sswrap "> |
| 5413 |
<a class="ss" href="#section-277">§</a> |
| 5414 |
</div> |
| 5415 |
<p>Set up inheritance for the model, collection, router, view and history.</p> |
| 5416 |
|
| 5417 |
</div> |
| 5418 |
|
| 5419 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title class_">Model</span>.<span class="hljs-property">extend</span> = <span class="hljs-title class_">Collection</span>.<span class="hljs-property">extend</span> = <span class="hljs-title class_">Router</span>.<span class="hljs-property">extend</span> = <span class="hljs-title class_">View</span>.<span class="hljs-property">extend</span> = <span class="hljs-title class_">History</span>.<span class="hljs-property">extend</span> = extend;</pre></div></div> |
| 5420 |
|
| 5421 |
</li> |
| 5422 |
|
| 5423 |
|
| 5424 |
<li id="section-278"> |
| 5425 |
<div class="annotation"> |
| 5426 |
|
| 5427 |
<div class="sswrap "> |
| 5428 |
<a class="ss" href="#section-278">§</a> |
| 5429 |
</div> |
| 5430 |
<p>Throw an error when a URL is needed, and none is supplied.</p> |
| 5431 |
|
| 5432 |
</div> |
| 5433 |
|
| 5434 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> urlError = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 5435 |
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-string">'A "url" property or function must be specified'</span>); |
| 5436 |
};</pre></div></div> |
| 5437 |
|
| 5438 |
</li> |
| 5439 |
|
| 5440 |
|
| 5441 |
<li id="section-279"> |
| 5442 |
<div class="annotation"> |
| 5443 |
|
| 5444 |
<div class="sswrap "> |
| 5445 |
<a class="ss" href="#section-279">§</a> |
| 5446 |
</div> |
| 5447 |
<p>Wrap an optional error callback with a fallback error event.</p> |
| 5448 |
|
| 5449 |
</div> |
| 5450 |
|
| 5451 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> wrapError = <span class="hljs-keyword">function</span>(<span class="hljs-params">model, options</span>) { |
| 5452 |
<span class="hljs-keyword">var</span> error = options.<span class="hljs-property">error</span>; |
| 5453 |
options.<span class="hljs-property">error</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">resp</span>) { |
| 5454 |
<span class="hljs-keyword">if</span> (error) error.<span class="hljs-title function_">call</span>(options.<span class="hljs-property">context</span>, model, resp, options); |
| 5455 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">'error'</span>, model, resp, options); |
| 5456 |
}; |
| 5457 |
}; |
| 5458 |
|
| 5459 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>; |
| 5460 |
});</pre></div></div> |
| 5461 |
|
| 5462 |
</li> |
| 5463 |
|
| 5464 |
</ul> |
| 5465 |
</div> |
| 5466 |
</body> |
| 5467 |
</html> |