| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<title>backbone.localStorage.js</title> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 7 |
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> |
| 8 |
<link rel="stylesheet" media="all" href="../docco.css" /> |
| 9 |
</head> |
| 10 |
<body> |
| 11 |
<div id="container"> |
| 12 |
<div id="background"></div> |
| 13 |
|
| 14 |
<ul id="jump_to"> |
| 15 |
<li> |
| 16 |
<a class="large" href="javascript:void(0);">Jump To …</a> |
| 17 |
<a class="small" href="javascript:void(0);">+</a> |
| 18 |
<div id="jump_wrapper"> |
| 19 |
<div id="jump_page_wrapper"> |
| 20 |
<div id="jump_page"> |
| 21 |
|
| 22 |
|
| 23 |
<a class="source" href="backbone.localStorage.html"> |
| 24 |
examples/backbone.localStorage.js |
| 25 |
</a> |
| 26 |
|
| 27 |
|
| 28 |
<a class="source" href="todos/todos.html"> |
| 29 |
examples/todos/todos.js |
| 30 |
</a> |
| 31 |
|
| 32 |
</div> |
| 33 |
</div> |
| 34 |
</li> |
| 35 |
</ul> |
| 36 |
|
| 37 |
<ul class="sections"> |
| 38 |
|
| 39 |
<li id="title"> |
| 40 |
<div class="annotation"> |
| 41 |
<h1>backbone.localStorage.js</h1> |
| 42 |
</div> |
| 43 |
</li> |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
<li id="section-1"> |
| 48 |
<div class="annotation"> |
| 49 |
|
| 50 |
<div class="sswrap "> |
| 51 |
<a class="ss" href="#section-1">§</a> |
| 52 |
</div> |
| 53 |
|
| 54 |
</div> |
| 55 |
|
| 56 |
<div class="content"><div class='highlight'><pre><span class="hljs-comment">/** |
| 57 |
* Backbone localStorage Adapter |
| 58 |
* Version 1.1.0 |
| 59 |
* |
| 60 |
* https://github.com/jeromegn/Backbone.localStorage |
| 61 |
*/</span> |
| 62 |
(<span class="hljs-keyword">function</span> (<span class="hljs-params">root, factory</span>) { |
| 63 |
<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>) {</pre></div></div> |
| 64 |
|
| 65 |
</li> |
| 66 |
|
| 67 |
|
| 68 |
<li id="section-2"> |
| 69 |
<div class="annotation"> |
| 70 |
|
| 71 |
<div class="sswrap "> |
| 72 |
<a class="ss" href="#section-2">§</a> |
| 73 |
</div> |
| 74 |
<p>AMD. Register as an anonymous module.</p> |
| 75 |
|
| 76 |
</div> |
| 77 |
|
| 78 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title function_">define</span>([<span class="hljs-string">"underscore"</span>,<span class="hljs-string">"backbone"</span>], <span class="hljs-keyword">function</span>(<span class="hljs-params">_, Backbone</span>) {</pre></div></div> |
| 79 |
|
| 80 |
</li> |
| 81 |
|
| 82 |
|
| 83 |
<li id="section-3"> |
| 84 |
<div class="annotation"> |
| 85 |
|
| 86 |
<div class="sswrap "> |
| 87 |
<a class="ss" href="#section-3">§</a> |
| 88 |
</div> |
| 89 |
<p>Use global variables if the locals are undefined.</p> |
| 90 |
|
| 91 |
</div> |
| 92 |
|
| 93 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> <span class="hljs-title function_">factory</span>(_ || root.<span class="hljs-property">_</span>, <span class="hljs-title class_">Backbone</span> || root.<span class="hljs-property">Backbone</span>); |
| 94 |
}); |
| 95 |
} <span class="hljs-keyword">else</span> {</pre></div></div> |
| 96 |
|
| 97 |
</li> |
| 98 |
|
| 99 |
|
| 100 |
<li id="section-4"> |
| 101 |
<div class="annotation"> |
| 102 |
|
| 103 |
<div class="sswrap "> |
| 104 |
<a class="ss" href="#section-4">§</a> |
| 105 |
</div> |
| 106 |
<p>RequireJS isn’t being used. Assume underscore and backbone are loaded in script tags</p> |
| 107 |
|
| 108 |
</div> |
| 109 |
|
| 110 |
<div class="content"><div class='highlight'><pre> <span class="hljs-title function_">factory</span>(_, <span class="hljs-title class_">Backbone</span>); |
| 111 |
} |
| 112 |
}(<span class="hljs-variable language_">this</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">_, Backbone</span>) {</pre></div></div> |
| 113 |
|
| 114 |
</li> |
| 115 |
|
| 116 |
|
| 117 |
<li id="section-5"> |
| 118 |
<div class="annotation"> |
| 119 |
|
| 120 |
<div class="sswrap "> |
| 121 |
<a class="ss" href="#section-5">§</a> |
| 122 |
</div> |
| 123 |
<p>A simple module to replace <code>Backbone.sync</code> with <em>localStorage</em>-based |
| 124 |
persistence. Models are given GUIDS, and saved into a JSON object. Simple |
| 125 |
as that.</p> |
| 126 |
|
| 127 |
</div> |
| 128 |
|
| 129 |
</li> |
| 130 |
|
| 131 |
|
| 132 |
<li id="section-6"> |
| 133 |
<div class="annotation"> |
| 134 |
|
| 135 |
<div class="sswrap "> |
| 136 |
<a class="ss" href="#section-6">§</a> |
| 137 |
</div> |
| 138 |
<p>Hold reference to Underscore.js and Backbone.js in the closure in order |
| 139 |
to make things work even if they are removed from the global namespace</p> |
| 140 |
|
| 141 |
</div> |
| 142 |
|
| 143 |
</li> |
| 144 |
|
| 145 |
|
| 146 |
<li id="section-7"> |
| 147 |
<div class="annotation"> |
| 148 |
|
| 149 |
<div class="sswrap "> |
| 150 |
<a class="ss" href="#section-7">§</a> |
| 151 |
</div> |
| 152 |
<p>Generate four random hex digits.</p> |
| 153 |
|
| 154 |
</div> |
| 155 |
|
| 156 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">S4</span>(<span class="hljs-params"></span>) { |
| 157 |
<span class="hljs-keyword">return</span> (((<span class="hljs-number">1</span>+<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">random</span>())*<span class="hljs-number">0x10000</span>)|<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>).<span class="hljs-title function_">substring</span>(<span class="hljs-number">1</span>); |
| 158 |
};</pre></div></div> |
| 159 |
|
| 160 |
</li> |
| 161 |
|
| 162 |
|
| 163 |
<li id="section-8"> |
| 164 |
<div class="annotation"> |
| 165 |
|
| 166 |
<div class="sswrap "> |
| 167 |
<a class="ss" href="#section-8">§</a> |
| 168 |
</div> |
| 169 |
<p>Generate a pseudo-GUID by concatenating random hexadecimal.</p> |
| 170 |
|
| 171 |
</div> |
| 172 |
|
| 173 |
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">guid</span>(<span class="hljs-params"></span>) { |
| 174 |
<span class="hljs-keyword">return</span> (<span class="hljs-title function_">S4</span>()+<span class="hljs-title function_">S4</span>()+<span class="hljs-string">"-"</span>+<span class="hljs-title function_">S4</span>()+<span class="hljs-string">"-"</span>+<span class="hljs-title function_">S4</span>()+<span class="hljs-string">"-"</span>+<span class="hljs-title function_">S4</span>()+<span class="hljs-string">"-"</span>+<span class="hljs-title function_">S4</span>()+<span class="hljs-title function_">S4</span>()+<span class="hljs-title function_">S4</span>()); |
| 175 |
};</pre></div></div> |
| 176 |
|
| 177 |
</li> |
| 178 |
|
| 179 |
|
| 180 |
<li id="section-9"> |
| 181 |
<div class="annotation"> |
| 182 |
|
| 183 |
<div class="sswrap "> |
| 184 |
<a class="ss" href="#section-9">§</a> |
| 185 |
</div> |
| 186 |
<p>Our Store is represented by a single JS object in <em>localStorage</em>. Create it |
| 187 |
with a meaningful name, like the name you’d give a table. |
| 188 |
window.Store is deprecated, use Backbone.LocalStorage instead</p> |
| 189 |
|
| 190 |
</div> |
| 191 |
|
| 192 |
<div class="content"><div class='highlight'><pre><span class="hljs-title class_">Backbone</span>.<span class="hljs-property">LocalStorage</span> = <span class="hljs-variable language_">window</span>.<span class="hljs-property">Store</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">name</span>) { |
| 193 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name; |
| 194 |
<span class="hljs-keyword">var</span> store = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">getItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>); |
| 195 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span> = (store && store.<span class="hljs-title function_">split</span>(<span class="hljs-string">","</span>)) || []; |
| 196 |
}; |
| 197 |
|
| 198 |
_.<span class="hljs-title function_">extend</span>(<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">LocalStorage</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>, {</pre></div></div> |
| 199 |
|
| 200 |
</li> |
| 201 |
|
| 202 |
|
| 203 |
<li id="section-10"> |
| 204 |
<div class="annotation"> |
| 205 |
|
| 206 |
<div class="sswrap "> |
| 207 |
<a class="ss" href="#section-10">§</a> |
| 208 |
</div> |
| 209 |
<p>Save the current state of the <strong>Store</strong> to <em>localStorage</em>.</p> |
| 210 |
|
| 211 |
</div> |
| 212 |
|
| 213 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">save</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 214 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">setItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span>.<span class="hljs-title function_">join</span>(<span class="hljs-string">","</span>)); |
| 215 |
},</pre></div></div> |
| 216 |
|
| 217 |
</li> |
| 218 |
|
| 219 |
|
| 220 |
<li id="section-11"> |
| 221 |
<div class="annotation"> |
| 222 |
|
| 223 |
<div class="sswrap "> |
| 224 |
<a class="ss" href="#section-11">§</a> |
| 225 |
</div> |
| 226 |
<p>Add a model, giving it a (hopefully)-unique GUID, if it doesn’t already |
| 227 |
have an id of it’s own.</p> |
| 228 |
|
| 229 |
</div> |
| 230 |
|
| 231 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">create</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 232 |
<span class="hljs-keyword">if</span> (!model.<span class="hljs-property">id</span>) { |
| 233 |
model.<span class="hljs-property">id</span> = <span class="hljs-title function_">guid</span>(); |
| 234 |
model.<span class="hljs-title function_">set</span>(model.<span class="hljs-property">idAttribute</span>, model.<span class="hljs-property">id</span>); |
| 235 |
} |
| 236 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">setItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>+<span class="hljs-string">"-"</span>+model.<span class="hljs-property">id</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(model)); |
| 237 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span>.<span class="hljs-title function_">push</span>(model.<span class="hljs-property">id</span>.<span class="hljs-title function_">toString</span>()); |
| 238 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">save</span>(); |
| 239 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">find</span>(model); |
| 240 |
},</pre></div></div> |
| 241 |
|
| 242 |
</li> |
| 243 |
|
| 244 |
|
| 245 |
<li id="section-12"> |
| 246 |
<div class="annotation"> |
| 247 |
|
| 248 |
<div class="sswrap "> |
| 249 |
<a class="ss" href="#section-12">§</a> |
| 250 |
</div> |
| 251 |
<p>Update a model by replacing its copy in <code>this.data</code>.</p> |
| 252 |
|
| 253 |
</div> |
| 254 |
|
| 255 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">update</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 256 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">setItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>+<span class="hljs-string">"-"</span>+model.<span class="hljs-property">id</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(model)); |
| 257 |
<span class="hljs-keyword">if</span> (!_.<span class="hljs-title function_">include</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span>, model.<span class="hljs-property">id</span>.<span class="hljs-title function_">toString</span>())) |
| 258 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span>.<span class="hljs-title function_">push</span>(model.<span class="hljs-property">id</span>.<span class="hljs-title function_">toString</span>()); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">save</span>(); |
| 259 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">find</span>(model); |
| 260 |
},</pre></div></div> |
| 261 |
|
| 262 |
</li> |
| 263 |
|
| 264 |
|
| 265 |
<li id="section-13"> |
| 266 |
<div class="annotation"> |
| 267 |
|
| 268 |
<div class="sswrap "> |
| 269 |
<a class="ss" href="#section-13">§</a> |
| 270 |
</div> |
| 271 |
<p>Retrieve a model from <code>this.data</code> by id.</p> |
| 272 |
|
| 273 |
</div> |
| 274 |
|
| 275 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">find</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 276 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">jsonData</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">getItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>+<span class="hljs-string">"-"</span>+model.<span class="hljs-property">id</span>)); |
| 277 |
},</pre></div></div> |
| 278 |
|
| 279 |
</li> |
| 280 |
|
| 281 |
|
| 282 |
<li id="section-14"> |
| 283 |
<div class="annotation"> |
| 284 |
|
| 285 |
<div class="sswrap "> |
| 286 |
<a class="ss" href="#section-14">§</a> |
| 287 |
</div> |
| 288 |
<p>Return the array of all models currently in storage.</p> |
| 289 |
|
| 290 |
</div> |
| 291 |
|
| 292 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">findAll</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 293 |
<span class="hljs-keyword">return</span> <span class="hljs-title function_">_</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span>).<span class="hljs-title function_">chain</span>() |
| 294 |
.<span class="hljs-title function_">map</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params">id</span>){ |
| 295 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">jsonData</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">getItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>+<span class="hljs-string">"-"</span>+id)); |
| 296 |
}, <span class="hljs-variable language_">this</span>) |
| 297 |
.<span class="hljs-title function_">compact</span>() |
| 298 |
.<span class="hljs-title function_">value</span>(); |
| 299 |
},</pre></div></div> |
| 300 |
|
| 301 |
</li> |
| 302 |
|
| 303 |
|
| 304 |
<li id="section-15"> |
| 305 |
<div class="annotation"> |
| 306 |
|
| 307 |
<div class="sswrap "> |
| 308 |
<a class="ss" href="#section-15">§</a> |
| 309 |
</div> |
| 310 |
<p>Delete a model from <code>this.data</code>, returning it.</p> |
| 311 |
|
| 312 |
</div> |
| 313 |
|
| 314 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">destroy</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 315 |
<span class="hljs-keyword">if</span> (model.<span class="hljs-title function_">isNew</span>()) |
| 316 |
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span> |
| 317 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">localStorage</span>().<span class="hljs-title function_">removeItem</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>+<span class="hljs-string">"-"</span>+model.<span class="hljs-property">id</span>); |
| 318 |
<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span> = _.<span class="hljs-title function_">reject</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">records</span>, <span class="hljs-keyword">function</span>(<span class="hljs-params">id</span>){ |
| 319 |
<span class="hljs-keyword">return</span> id === model.<span class="hljs-property">id</span>.<span class="hljs-title function_">toString</span>(); |
| 320 |
}); |
| 321 |
<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">save</span>(); |
| 322 |
<span class="hljs-keyword">return</span> model; |
| 323 |
}, |
| 324 |
|
| 325 |
<span class="hljs-attr">localStorage</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { |
| 326 |
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">localStorage</span>; |
| 327 |
},</pre></div></div> |
| 328 |
|
| 329 |
</li> |
| 330 |
|
| 331 |
|
| 332 |
<li id="section-16"> |
| 333 |
<div class="annotation"> |
| 334 |
|
| 335 |
<div class="sswrap "> |
| 336 |
<a class="ss" href="#section-16">§</a> |
| 337 |
</div> |
| 338 |
<p>fix for “illegal access” error on Android when JSON.parse is passed null</p> |
| 339 |
|
| 340 |
</div> |
| 341 |
|
| 342 |
<div class="content"><div class='highlight'><pre> <span class="hljs-attr">jsonData</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) { |
| 343 |
<span class="hljs-keyword">return</span> data && <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(data); |
| 344 |
} |
| 345 |
|
| 346 |
});</pre></div></div> |
| 347 |
|
| 348 |
</li> |
| 349 |
|
| 350 |
|
| 351 |
<li id="section-17"> |
| 352 |
<div class="annotation"> |
| 353 |
|
| 354 |
<div class="sswrap "> |
| 355 |
<a class="ss" href="#section-17">§</a> |
| 356 |
</div> |
| 357 |
<p>localSync delegate to the model or collection’s |
| 358 |
<em>localStorage</em> property, which should be an instance of <code>Store</code>. |
| 359 |
window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead</p> |
| 360 |
|
| 361 |
</div> |
| 362 |
|
| 363 |
<div class="content"><div class='highlight'><pre><span class="hljs-title class_">Backbone</span>.<span class="hljs-property">LocalStorage</span>.<span class="hljs-property">sync</span> = <span class="hljs-variable language_">window</span>.<span class="hljs-property">Store</span>.<span class="hljs-property">sync</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">localSync</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">method, model, options</span>) { |
| 364 |
<span class="hljs-keyword">var</span> store = model.<span class="hljs-property">localStorage</span> || model.<span class="hljs-property">collection</span>.<span class="hljs-property">localStorage</span>; |
| 365 |
|
| 366 |
<span class="hljs-keyword">var</span> resp, errorMessage, syncDfd = $.<span class="hljs-title class_">Deferred</span> && $.<span class="hljs-title class_">Deferred</span>(); <span class="hljs-comment">//If $ is having Deferred - use it.</span> |
| 367 |
|
| 368 |
<span class="hljs-keyword">try</span> { |
| 369 |
|
| 370 |
<span class="hljs-keyword">switch</span> (method) { |
| 371 |
<span class="hljs-keyword">case</span> <span class="hljs-string">"read"</span>: |
| 372 |
resp = model.<span class="hljs-property">id</span> != <span class="hljs-literal">undefined</span> ? store.<span class="hljs-title function_">find</span>(model) : store.<span class="hljs-title function_">findAll</span>(); |
| 373 |
<span class="hljs-keyword">break</span>; |
| 374 |
<span class="hljs-keyword">case</span> <span class="hljs-string">"create"</span>: |
| 375 |
resp = store.<span class="hljs-title function_">create</span>(model); |
| 376 |
<span class="hljs-keyword">break</span>; |
| 377 |
<span class="hljs-keyword">case</span> <span class="hljs-string">"update"</span>: |
| 378 |
resp = store.<span class="hljs-title function_">update</span>(model); |
| 379 |
<span class="hljs-keyword">break</span>; |
| 380 |
<span class="hljs-keyword">case</span> <span class="hljs-string">"delete"</span>: |
| 381 |
resp = store.<span class="hljs-title function_">destroy</span>(model); |
| 382 |
<span class="hljs-keyword">break</span>; |
| 383 |
} |
| 384 |
|
| 385 |
} <span class="hljs-keyword">catch</span>(error) { |
| 386 |
<span class="hljs-keyword">if</span> (error.<span class="hljs-property">code</span> === <span class="hljs-title class_">DOMException</span>.<span class="hljs-property">QUOTA_EXCEEDED_ERR</span> && <span class="hljs-variable language_">window</span>.<span class="hljs-property">localStorage</span>.<span class="hljs-property">length</span> === <span class="hljs-number">0</span>) |
| 387 |
errorMessage = <span class="hljs-string">"Private browsing is unsupported"</span>; |
| 388 |
<span class="hljs-keyword">else</span> |
| 389 |
errorMessage = error.<span class="hljs-property">message</span>; |
| 390 |
} |
| 391 |
|
| 392 |
<span class="hljs-keyword">if</span> (resp) { |
| 393 |
model.<span class="hljs-title function_">trigger</span>(<span class="hljs-string">"sync"</span>, model, resp, options); |
| 394 |
<span class="hljs-keyword">if</span> (options && options.<span class="hljs-property">success</span>) |
| 395 |
options.<span class="hljs-title function_">success</span>(resp); |
| 396 |
<span class="hljs-keyword">if</span> (syncDfd) |
| 397 |
syncDfd.<span class="hljs-title function_">resolve</span>(resp); |
| 398 |
|
| 399 |
} <span class="hljs-keyword">else</span> { |
| 400 |
errorMessage = errorMessage ? errorMessage |
| 401 |
: <span class="hljs-string">"Record Not Found"</span>; |
| 402 |
|
| 403 |
<span class="hljs-keyword">if</span> (options && options.<span class="hljs-property">error</span>) |
| 404 |
options.<span class="hljs-title function_">error</span>(errorMessage); |
| 405 |
<span class="hljs-keyword">if</span> (syncDfd) |
| 406 |
syncDfd.<span class="hljs-title function_">reject</span>(errorMessage); |
| 407 |
}</pre></div></div> |
| 408 |
|
| 409 |
</li> |
| 410 |
|
| 411 |
|
| 412 |
<li id="section-18"> |
| 413 |
<div class="annotation"> |
| 414 |
|
| 415 |
<div class="sswrap "> |
| 416 |
<a class="ss" href="#section-18">§</a> |
| 417 |
</div> |
| 418 |
<p>add compatibility with $.ajax |
| 419 |
always execute callback for success and error</p> |
| 420 |
|
| 421 |
</div> |
| 422 |
|
| 423 |
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (options && options.<span class="hljs-property">complete</span>) options.<span class="hljs-title function_">complete</span>(resp); |
| 424 |
|
| 425 |
<span class="hljs-keyword">return</span> syncDfd && syncDfd.<span class="hljs-title function_">promise</span>(); |
| 426 |
}; |
| 427 |
|
| 428 |
<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">ajaxSync</span> = <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">sync</span>; |
| 429 |
|
| 430 |
<span class="hljs-title class_">Backbone</span>.<span class="hljs-property">getSyncMethod</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">model</span>) { |
| 431 |
<span class="hljs-keyword">if</span>(model.<span class="hljs-property">localStorage</span> || (model.<span class="hljs-property">collection</span> && model.<span class="hljs-property">collection</span>.<span class="hljs-property">localStorage</span>)) { |
| 432 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">localSync</span>; |
| 433 |
} |
| 434 |
|
| 435 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">ajaxSync</span>; |
| 436 |
};</pre></div></div> |
| 437 |
|
| 438 |
</li> |
| 439 |
|
| 440 |
|
| 441 |
<li id="section-19"> |
| 442 |
<div class="annotation"> |
| 443 |
|
| 444 |
<div class="sswrap "> |
| 445 |
<a class="ss" href="#section-19">§</a> |
| 446 |
</div> |
| 447 |
<p>Override ‘Backbone.sync’ to default to localSync, |
| 448 |
the original ‘Backbone.sync’ is still available in ‘Backbone.ajaxSync’</p> |
| 449 |
|
| 450 |
</div> |
| 451 |
|
| 452 |
<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>) { |
| 453 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-title function_">getSyncMethod</span>(model).<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, [method, model, options]); |
| 454 |
}; |
| 455 |
|
| 456 |
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Backbone</span>.<span class="hljs-property">LocalStorage</span>; |
| 457 |
}));</pre></div></div> |
| 458 |
|
| 459 |
</li> |
| 460 |
|
| 461 |
</ul> |
| 462 |
</div> |
| 463 |
</body> |
| 464 |
</html> |