[Pkg-javascript-commits] [backbone] 24/74: Reworking the localStorage plugin.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:05 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.3.0
in repository backbone.

commit cc744682dd8a5a0695b339eb733abdbf38f84427
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Tue Oct 26 10:12:49 2010 -0400

    Reworking the localStorage plugin.
---
 Rakefile                             |   5 +-
 docs/backbone.localstorage.html      |  56 +++++++++++++
 docs/todos.html                      |   6 +-
 examples/backbone.localstorage.js    |  84 ++++++++++++++++++++
 examples/todos/index.html            |   7 +-
 examples/todos/todos.js              |   4 +-
 index.html                           |   2 +-
 test/vendor/backbone.localStorage.js | 150 -----------------------------------
 8 files changed, 154 insertions(+), 160 deletions(-)

diff --git a/Rakefile b/Rakefile
index 3af0285..b71e488 100644
--- a/Rakefile
+++ b/Rakefile
@@ -9,7 +9,10 @@ end
 
 desc "build the docco documentation"
 task :doc do
-  system "docco backbone.js && docco examples/todos/todos.js"
+  system [
+    'docco backbone.js',
+    'docco examples/todos/todos.js examples/backbone.localstorage.js'
+  ].join(' && ')
 end
 
 desc "run JavaScriptLint on the source"
diff --git a/docs/backbone.localstorage.html b/docs/backbone.localstorage.html
new file mode 100644
index 0000000..e075b20
--- /dev/null
+++ b/docs/backbone.localstorage.html
@@ -0,0 +1,56 @@
+<!DOCTYPE html>  <html> <head>   <title>backbone.localstorage.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>            <div id="jump_to">         Jump To …         <div id="jump_wrapper">           <div id="jump_page">                                           <a class="source" href="backbone.localstorage.html">           [...]
+persistence. Models are given GUIDS, and saved into a JSON object. Simple
+as that.</p>             </td>             <td class="code">               <div class="highlight"><pre></pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">¶</a>               </div>               <p>Generate four random hex digits.</p>             </td>             <td class="code">               <div class="highlight"><p [...]
+   <span class="k">return</span> <span class="p">(((</span><span class="mi">1</span><span class="o">+</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">())</span><span class="o">*</span><span class="mh">0x10000</span><span class="p">)</span><span class="o">|</span><span class="mi">0</span><span class="p">).</span><span class="nx">toString</span><span class="p">(</span><span class="mi">16</span><span class="p">).</span><span class="nx" [...]
+<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">¶</a>               </div>               <p>Generate a pseudo-GUID by concatenating random hexadecimal.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">function</span> <span class="nx [...]
+   <span class="k">return</span> <span class="p">(</span><span class="nx">S4</span><span class="p">()</span><span class="o">+</span><span class="nx">S4</span><span class="p">()</span><span class="o">+</span><span class="s2">"-"</span><span class="o">+</span><span class="nx">S4</span><span class="p">()</span><span class="o">+</span><span class="s2">"-"</span><span class="o">+</span><span class="nx">S4</span><span class="p">()</span><span class="o">+</span><span class=" [...]
+<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-4">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-4">¶</a>               </div>               <p>Our Store is represented by a single JS object in <em>localStorage</em>. Create it
+with a meaningful name, like the name you'd give a table.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Store</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
+  <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="nx">name</span><span class="p">;</span>
+  <span class="kd">var</span> <span class="nx">store</span> <span class="o">=</span> <span class="nx">localStorage</span><span class="p">.</span><span class="nx">getItem</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">);</span>
+  <span class="k">this</span><span class="p">.</span><span class="nx">data</span> <span class="o">=</span> <span class="p">(</span><span class="nx">store</span> <span class="o">&&</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">store</span><span class="p">))</span> <span class="o">||</span> <span class="p">{};</span>
+<span class="p">};</span>
+
+<span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">Store</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">¶</a>               </div>        [...]
+    <span class="nx">localStorage</span><span class="p">.</span><span class="nx">setItem</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">name</span><span class="p">,</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">));</span>
+  <span class="p">},</span></pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">¶</a>               </div>               <p>Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
+have an id of it's own.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">create</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
+    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">model</span><span class="p">.</span><span class="nx">id</span><span class="p">)</span> <span class="nx">model</span><span class="p">.</span><span class="nx">id</span> <span class="o">=</span> <span class="nx">model</span><span class="p">.</span><span class="nx">attributes</span><span class="p">.</span><span class="nx">id</span> <span class="o">=</span> <span class="nx">guid</span><span class="p [...]
+    <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">id</span><span class="p">]</span> <span class="o">=</span> <span class="nx">model</span><span class="p">;</span>
+    <span class="k">this</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span>
+    <span class="k">return</span> <span class="nx">model</span><span class="p">;</span>
+  <span class="p">},</span></pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">¶</a>               </div>               <p>Update a model by replacing its copy in <code>this.data</code>.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">update</span><span clas [...]
+    <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">id</span><span class="p">]</span> <span class="o">=</span> <span class="nx">model</span><span class="p">;</span>
+    <span class="k">this</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span>
+    <span class="k">return</span> <span class="nx">model</span><span class="p">;</span>
+  <span class="p">},</span></pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">¶</a>               </div>               <p>Retrieve a model from <code>this.data</code> by id.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">find</span><span class="o">:</span> [...]
+    <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">id</span><span class="p">];</span>
+  <span class="p">},</span></pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">¶</a>               </div>               <p>Return the array of all models currently in storage.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">findAll</span><span class="o">:</s [...]
+    <span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">values</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">);</span>
+  <span class="p">},</span></pre></div>             </td>           </tr>                               <tr id="section-10">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-10">¶</a>               </div>               <p>Delete a model from <code>this.data</code>, returning it.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">destroy</span><span class=" [...]
+    <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">id</span><span class="p">];</span>
+    <span class="k">this</span><span class="p">.</span><span class="nx">save</span><span class="p">();</span>
+    <span class="k">return</span> <span class="nx">model</span><span class="p">;</span>
+  <span class="p">}</span>
+
+<span class="p">});</span></pre></div>             </td>           </tr>                               <tr id="section-11">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-11">¶</a>               </div>               <p>Override <code>Backbone.sync</code> to use delegate to the model or collection's
+<em>localStorage</em> property, which should be an instance of <code>Store</code>.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Backbone</span><span class="p">.</span><span class="nx">sync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">method</span><span class="p">,</span> <span class="nx">model</span><span class="p">,</span> <span class="nx">success</span><span cla [...]
+
+  <span class="kd">var</span> <span class="nx">resp</span><span class="p">;</span>
+  <span class="kd">var</span> <span class="nx">store</span> <span class="o">=</span> <span class="nx">model</span><span class="p">.</span><span class="nx">localStorage</span> <span class="o">||</span> <span class="nx">model</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">localStorage</span><span class="p">;</span>
+
+  <span class="k">switch</span> <span class="p">(</span><span class="nx">method</span><span class="p">)</span> <span class="p">{</span>
+    <span class="k">case</span> <span class="s2">"read"</span><span class="o">:</span>    <span class="nx">resp</span> <span class="o">=</span> <span class="nx">model</span><span class="p">.</span><span class="nx">id</span> <span class="o">?</span> <span class="nx">store</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">model</span><span class="p">)</span> <span class="o">:</span> <span class="nx">store</span><span class="p">.</s [...]
+    <span class="k">case</span> <span class="s2">"create"</span><span class="o">:</span>  <span class="nx">resp</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">model</span><span class="p">);</span>                            <span class="k">break</span><span class="p">;</span>
+    <span class="k">case</span> <span class="s2">"update"</span><span class="o">:</span>  <span class="nx">resp</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">update</span><span class="p">(</span><span class="nx">model</span><span class="p">);</span>                            <span class="k">break</span><span class="p">;</span>
+    <span class="k">case</span> <span class="s2">"delete"</span><span class="o">:</span>  <span class="nx">resp</span> <span class="o">=</span> <span class="nx">store</span><span class="p">.</span><span class="nx">destroy</span><span class="p">(</span><span class="nx">model</span><span class="p">);</span>                           <span class="k">break</span><span class="p">;</span>
+  <span class="p">}</span>
+
+  <span class="k">if</span> <span class="p">(</span><span class="nx">resp</span><span class="p">)</span> <span class="p">{</span>
+    <span class="nx">success</span><span class="p">(</span><span class="nx">resp</span><span class="p">);</span>
+  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+    <span class="nx">error</span><span class="p">(</span><span class="s2">"Record not found"</span><span class="p">);</span>
+  <span class="p">}</span>
+<span class="p">};</span>
+
+</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html> 
\ No newline at end of file
diff --git a/docs/todos.html b/docs/todos.html
index ba0eb70..eb31901 100644
--- a/docs/todos.html
+++ b/docs/todos.html
@@ -1,6 +1,6 @@
-<!DOCTYPE html>  <html> <head>   <title>todos.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>          <table cellpadding="0" cellspacing="0">       <thead>         <tr>           <th class="docs">             <h1>               todos.js             </h1>           </th>           <th class="code">           </th>         </tr>    [...]
+<!DOCTYPE html>  <html> <head>   <title>todos.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>            <div id="jump_to">         Jump To …         <div id="jump_wrapper">           <div id="jump_page">                                           <a class="source" href="backbone.localstorage.html">                 backbone. [...]
 <a href="http://jgn.me/">Jérôme Gravel-Niquet</a>. This demo uses a simple
-<a href="http://github.com/jeromegn/Backbone.localStorage">LocalStorage adapter</a>
+<a href="backbone.localstorage.html">LocalStorage adapter</a>
 to persist Backbone models within your browser.</p>             </td>             <td class="code">               <div class="highlight"><pre></pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">¶</a>               </div>               <p>Load the application once the DOM is ready, using <code>jQuery.ready</code>:</p>      [...]
       <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">"content"</span><span class="p">))</span> <span class="p">{</span>
         <span class="k">this</span><span class="p">.</span><span class="nx">set</span><span class="p">({</span><span class="s2">"content"</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">EMPTY</span><span class="p">});</span>
@@ -13,7 +13,7 @@ to persist Backbone models within your browser.</p>             </td>
     <span class="p">}</span>
 
   <span class="p">});</span></pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">¶</a>               </div>               <h2>Todo Collection</h2>             </td>             <td class="code">               <div class="highlight"><pre></pre></div>             </td>           </tr>                               <tr id="se [...]
-server.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nb">window</span><span class="p">.</span><span class="nx">TodoList</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></pre></div>             </td>           </tr>                               <tr id="section-11">           [...]
+server.</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nb">window</span><span class="p">.</span><span class="nx">TodoList</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Collection</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></pre></div>             </td>           </tr>                               <tr id="section-11">           [...]
       <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">todo</span><span class="p">){</span> <span class="k">return</span> <span class="nx">todo</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'done'</span><span class="p">);</span> <span class="p">});</span>
     <span class="p">},</span></pre></div>             </td>           </tr>                               <tr id="section-14">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-14">¶</a>               </div>               <p>Filter down the list to only todo items that are still not finished.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">remaining</s [...]
       <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">without</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">done</span><span class="p">());</span>
diff --git a/examples/backbone.localstorage.js b/examples/backbone.localstorage.js
new file mode 100644
index 0000000..add3cf7
--- /dev/null
+++ b/examples/backbone.localstorage.js
@@ -0,0 +1,84 @@
+// A simple module to replace `Backbone.sync` with *localStorage*-based
+// persistence. Models are given GUIDS, and saved into a JSON object. Simple
+// as that.
+
+// Generate four random hex digits.
+function S4() {
+   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
+};
+
+// Generate a pseudo-GUID by concatenating random hexadecimal.
+function guid() {
+   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
+};
+
+// Our Store is represented by a single JS object in *localStorage*. Create it
+// with a meaningful name, like the name you'd give a table.
+var Store = function(name) {
+  this.name = name;
+  var store = localStorage.getItem(this.name);
+  this.data = (store && JSON.parse(store)) || {};
+};
+
+_.extend(Store.prototype, {
+
+  // Save the current state of the **Store** to *localStorage*.
+  save: function() {
+    localStorage.setItem(this.name, JSON.stringify(this.data));
+  },
+
+  // Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
+  // have an id of it's own.
+  create: function(model) {
+    if (!model.id) model.id = model.attributes.id = guid();
+    this.data[model.id] = model;
+    this.save();
+    return model;
+  },
+
+  // Update a model by replacing its copy in `this.data`.
+  update: function(model) {
+    this.data[model.id] = model;
+    this.save();
+    return model;
+  },
+
+  // Retrieve a model from `this.data` by id.
+  find: function(model) {
+    return this.data[model.id];
+  },
+
+  // Return the array of all models currently in storage.
+  findAll: function() {
+    return _.values(this.data);
+  },
+
+  // Delete a model from `this.data`, returning it.
+  destroy: function(model) {
+    delete this.data[model.id];
+    this.save();
+    return model;
+  }
+
+});
+
+// Override `Backbone.sync` to use delegate to the model or collection's
+// *localStorage* property, which should be an instance of `Store`.
+Backbone.sync = function(method, model, success, error) {
+
+  var resp;
+  var store = model.localStorage || model.collection.localStorage;
+
+  switch (method) {
+    case "read":    resp = model.id ? store.find(model) : store.findAll(); break;
+    case "create":  resp = store.create(model);                            break;
+    case "update":  resp = store.update(model);                            break;
+    case "delete":  resp = store.destroy(model);                           break;
+  }
+
+  if (resp) {
+    success(resp);
+  } else {
+    error("Record not found");
+  }
+};
\ No newline at end of file
diff --git a/examples/todos/index.html b/examples/todos/index.html
index cb47331..c78c63d 100644
--- a/examples/todos/index.html
+++ b/examples/todos/index.html
@@ -4,15 +4,16 @@
   <head>
     <title>Backbone Demo: Todos</title>
     <link href="todos.css" media="all" rel="stylesheet" type="text/css"/>
+    <script src="../../test/vendor/json2.js"></script>
     <script src="../../test/vendor/jquery-1.4.2.js"></script>
     <script src="../../test/vendor/underscore-1.1.0.js"></script>
     <script src="../../backbone.js"></script>
-    <script src="../../test/vendor/backbone.localStorage.js"></script>
+    <script src="../backbone.localstorage.js"></script>
     <script src="todos.js"></script>
   </head>
 
   <body>
-    
+
     <!-- Todo App Interface -->
 
     <div id="todoapp">
@@ -48,7 +49,7 @@
       <br />
       <a href="http://jgn.me/">Jérôme Gravel-Niquet</a>
     </div>
-    
+
     <!-- Templates -->
 
     <script type="text/template" id="item-template">
diff --git a/examples/todos/todos.js b/examples/todos/todos.js
index a8650f9..90d231f 100644
--- a/examples/todos/todos.js
+++ b/examples/todos/todos.js
@@ -1,6 +1,6 @@
 // An example Backbone application contributed by
 // [Jérôme Gravel-Niquet](http://jgn.me/). This demo uses a simple
-// [LocalStorage adapter](http://github.com/jeromegn/Backbone.localStorage)
+// [LocalStorage adapter](backbone.localstorage.html)
 // to persist Backbone models within your browser.
 
 // Load the application once the DOM is ready, using `jQuery.ready`:
@@ -46,7 +46,7 @@ $(function(){
     model: Todo,
 
     // Save all of the todo items under the `"todos"` namespace.
-    localStore: "todos",
+    localStorage: new Store("todos"),
 
     // Filter down the list of all todo items that are finished.
     done: function() {
diff --git a/index.html b/index.html
index 1b94503..ed68060 100644
--- a/index.html
+++ b/index.html
@@ -1519,7 +1519,7 @@ var DocumentView = Backbone.View.extend({
       that is bundled in the repository as Backbone example. If you're wondering
       where to get started with Backbone in general, take a moment to
       <a href="docs/todos.html">read through the annotated source</a>. The app uses a
-      <a href="http://github.com/jeromegn/Backbone.localStorage">LocalStorage adapter</a>
+      <a href="docs/backbone.localstorage.js">LocalStorage adapter</a>
       to transparently save all of your todos within your browser, instead of
       sending them to a server. Jérôme also has a version hosted at
       <a href="http://localtodos.com/">localtodos.com</a> that uses a
diff --git a/test/vendor/backbone.localStorage.js b/test/vendor/backbone.localStorage.js
deleted file mode 100644
index 756fea6..0000000
--- a/test/vendor/backbone.localStorage.js
+++ /dev/null
@@ -1,150 +0,0 @@
-// UUID
-function S4() {
-   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
-};
-
-function guid() {
-   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
-};
-
-Storage.prototype.setObject = function(key, value) {
-  this.setItem(key, JSON.stringify(value));
-};
-
-Storage.prototype.getObject = function(key) {
-  return this.getItem(key) && JSON.parse(this.getItem(key));
-};
-
-var Store = function(name) {
-  this.name = name;
-};
-
-_.extend(Store.prototype, {
-
-  create: function(model) {
-    this.data = localStorage.getObject(this.name);
-
-    if (!this.data) {
-      this.data = [];
-    }
-
-    if (!model.id) model.attributes.id = guid();
-
-    this.data.push(model);
-
-    localStorage.setObject(this.name, this.data);
-
-    return {model: model, status: "success"};
-  },
-
-  update: function(model) {
-    var newData = [];
-    var succeeded = false;
-
-    this.data = localStorage.getObject(this.name);
-
-    if (!this.data) {
-      this.data = [];
-    }
-
-    newData = _.map(this.data, function(i) {
-      if (i.id == model.id) {
-        succeeded = true;
-        return model;
-      } else {
-        return i;
-      }
-    });
-
-    if (!succeeded) {
-      this.create(model);
-    } else {
-      localStorage.setObject(this.name, newData);
-    }
-
-    return {model: model, status: "success"};
-  },
-
-  find: function(model) {
-    var record;
-
-    this.data = localStorage.getObject(this.name);
-
-    if (!this.data) {
-      this.data = [];
-    }
-
-    _.each(this.data, function(item) {
-      if (item.id == model.id) {
-        record = item;
-        _.breakLoop();
-      }
-    });
-
-    if (typeof(record) == 'object') {
-      return {model: record, status: "success"};
-    } else {
-      return {error: "Record Not Found.", status: "error"};
-    }
-  },
-
-  findAll: function() {
-    this.data = localStorage.getObject(this.name);
-
-    if (!this.data) {
-      this.data = [];
-    }
-
-    return {model: this.data, status: "success"};
-  },
-
-  destroy: function(model) {
-    var newData = [];
-    var recordKey;
-    var succeeded = false;
-
-    this.data = localStorage.getObject(this.name);
-
-    if (!this.data) {
-      this.data = [];
-    }
-
-    _.each(this.data, function(item, key) {
-      if (item.id == model.id) {
-        succeeded = true;
-        recordKey = key;
-        _.breakLoop();
-      }
-    });
-
-    if (succeeded) this.data.splice(recordKey, 1);
-
-    localStorage.setObject(this.name, this.data);
-
-    if (succeeded) {
-      return {model: model, status: "success"};
-    } else {
-      return {error: "Record Not Found.", status: "error"};
-    }
-  }
-
-});
-
-Backbone.sync = function(method, model, success, error) {
-
-  var resp;
-  var store = new Store(model.localStore ? model.localStore : model.collection.localStore);
-
-  switch (method) {
-    case "read":    resp = model.id ? store.find(model) : store.findAll(); break;
-    case "create":  resp = store.create(model);                            break;
-    case "update":  resp = store.update(model);                            break;
-    case "delete":  resp = store.destroy(model);                           break;
-  }
-
-  if (resp.status == "success") {
-    success(resp.model);
-  } else if (resp.status == "error" && error) {
-    error(resp);
-  }
-};
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list