Permalink
Browse files

docs

  • Loading branch information...
1 parent c144533 commit 3864657ede3f511e099c50f85b5a6ef1eba58eba @ggozad committed Jan 4, 2013
Showing with 74 additions and 27 deletions.
  1. +74 −27 docs/backbone.cachingsync.html
@@ -17,7 +17,7 @@
<div class='octowrap'>
<a class='octothorpe' href='#section-0'>#</a>
</div>
- <p>Backbone.cachingSync v0.1</p>
+ <p>Backbone.cachingSync v0.1.1</p>
</div>
<div class='code'>
<div class="highlight"><pre></pre></div>
@@ -29,12 +29,12 @@
<div class='octowrap'>
<a class='octothorpe' href='#section-1'>#</a>
</div>
- <p>(c) 2012 Yiorgis Gozadinos.
+ <p>(c) 2012 Yiorgis Gozadinos, Crypho AS.
Backbone.cachingSync is distributed under the MIT license.
http://github.com/ggozad/Backbone.cachingSync</p>
</div>
<div class='code'>
- <div class="highlight"><pre><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">$</span><span class="p">,</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">Burry</span><span class="p">)</span> <span class="p">{</span></pre></div>
+ <div class="highlight"><pre></pre></div>
</div>
</div>
<div class='clearall'></div>
@@ -43,6 +43,48 @@
<div class='octowrap'>
<a class='octothorpe' href='#section-2'>#</a>
</div>
+ <p>AMD/global registrations</p>
+ </div>
+ <div class='code'>
+ <div class="highlight"><pre><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">root</span><span class="p">,</span> <span class="nx">factory</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">define</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">define</span><span class="p">.</span><span class="nx">amd</span><span class="p">)</span> <span class="p">{</span></pre></div>
+ </div>
+ </div>
+ <div class='clearall'></div>
+ <div class='section' id='section-3'>
+ <div class='docs'>
+ <div class='octowrap'>
+ <a class='octothorpe' href='#section-3'>#</a>
+ </div>
+ <p>AMD. Register as an anonymous module.</p>
+ </div>
+ <div class='code'>
+ <div class="highlight"><pre> <span class="nx">define</span><span class="p">([</span><span class="s1">&#39;jquery&#39;</span><span class="p">,</span> <span class="s1">&#39;underscore&#39;</span><span class="p">,</span> <span class="s1">&#39;backbone&#39;</span><span class="p">,</span> <span class="s1">&#39;burry&#39;</span><span class="p">],</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">$</span><span class="p">,</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">Burry</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">cachingSync</span> <span class="o">=</span> <span class="nx">factory</span><span class="p">(</span><span class="nx">$</span><span class="p">,</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">Burry</span><span class="p">));</span>
+ <span class="p">});</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div>
+ </div>
+ </div>
+ <div class='clearall'></div>
+ <div class='section' id='section-4'>
+ <div class='docs'>
+ <div class='octowrap'>
+ <a class='octothorpe' href='#section-4'>#</a>
+ </div>
+ <p>Browser globals</p>
+ </div>
+ <div class='code'>
+ <div class="highlight"><pre> <span class="nx">root</span><span class="p">.</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">cachingSync</span> <span class="o">=</span> <span class="nx">factory</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">$</span><span class="p">,</span> <span class="nx">root</span><span class="p">.</span><span class="nx">_</span><span class="p">,</span> <span class="nx">root</span><span class="p">.</span><span class="nx">Backbone</span><span class="p">,</span> <span class="nx">root</span><span class="p">.</span><span class="nx">Burry</span><span class="p">);</span>
+ <span class="p">}</span>
+<span class="p">}(</span><span class="k">this</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">$</span><span class="p">,</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">Burry</span><span class="p">)</span> <span class="p">{</span></pre></div>
+ </div>
+ </div>
+ <div class='clearall'></div>
+ <div class='section' id='section-5'>
+ <div class='docs'>
+ <div class='octowrap'>
+ <a class='octothorpe' href='#section-5'>#</a>
+ </div>
<p><strong>Backbone.cachingSync</strong> provides <code>localStorage</code> caching for your models/collections.
In order to use it assign your model/collection's <strong>sync</strong> function to a wrapped
version. For instance <code>Collection.sync = Backbone.cachingSync(Backbone.sync, 'mycollection');</code>
@@ -52,14 +94,14 @@
<code>default_ttl</code>, a default time-to-live for the cache in minutes.</p>
</div>
<div class='code'>
- <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">cachingSync</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">wrapped</span><span class="p">,</span> <span class="nx">ns</span><span class="p">,</span> <span class="nx">default_ttl</span><span class="p">)</span> <span class="p">{</span></pre></div>
+ <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">cachingSync</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">wrapped</span><span class="p">,</span> <span class="nx">ns</span><span class="p">,</span> <span class="nx">default_ttl</span><span class="p">)</span> <span class="p">{</span></pre></div>
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-3'>
+ <div class='section' id='section-6'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-3'>#</a>
+ <a class='octothorpe' href='#section-6'>#</a>
</div>
<p>Create the <code>Burry.Store</code></p>
</div>
@@ -68,10 +110,10 @@
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-4'>
+ <div class='section' id='section-7'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-4'>#</a>
+ <a class='octothorpe' href='#section-7'>#</a>
</div>
<p><strong>get</strong> caches <em>read</em> operations on a model. If the model is cached,
it will resolve immediately with the updated attributes, triggering a <code>change</code>
@@ -104,10 +146,10 @@
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-5'>
+ <div class='section' id='section-8'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-5'>#</a>
+ <a class='octothorpe' href='#section-8'>#</a>
</div>
<p><strong>gets</strong> behaves similarly to <strong>get</strong> except it applies to collections.</p>
</div>
@@ -119,8 +161,12 @@
<span class="nx">wp</span> <span class="o">=</span> <span class="nx">wrapped</span><span class="p">(</span><span class="s1">&#39;read&#39;</span><span class="p">,</span> <span class="nx">collection</span><span class="p">,</span> <span class="nx">options</span><span class="p">).</span><span class="nx">done</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">models</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="nx">models</span><span class="p">,</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="nx">burry</span><span class="p">.</span><span class="nx">set</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="nx">model</span><span class="p">);</span> <span class="p">});</span>
- <span class="nx">burry</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s1">&#39;__ids__&#39;</span><span class="p">,</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span><span class="p">(</span><span class="nx">models</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span><span class="p">));</span>
- <span class="nx">collection</span><span class="p">.</span><span class="nx">reset</span><span class="p">(</span><span class="nx">models</span><span class="p">);</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">update</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">collection</span><span class="p">.</span><span class="nx">update</span><span class="p">(</span><span class="nx">models</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
+ <span class="nx">collection</span><span class="p">.</span><span class="nx">reset</span><span class="p">(</span><span class="nx">models</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="nx">burry</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s1">&#39;__ids__&#39;</span><span class="p">,</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pluck</span><span class="p">(</span><span class="nx">collection</span><span class="p">.</span><span class="nx">models</span><span class="p">,</span> <span class="s1">&#39;id&#39;</span><span class="p">));</span>
<span class="p">});</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">ids</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
@@ -138,34 +184,34 @@
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-6'>
+ <div class='section' id='section-9'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-6'>#</a>
+ <a class='octothorpe' href='#section-9'>#</a>
</div>
<p><strong>create</strong> saves a model on the server, and when the server save is resolved,
the model (and potentially its collection) is cached.</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="kd">function</span> <span class="nx">create</span> <span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">wrapped</span><span class="p">(</span><span class="s1">&#39;create&#39;</span><span class="p">,</span> <span class="nx">model</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span>
- <span class="p">.</span><span class="nx">done</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">newmodel</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">burry</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">newmodel</span><span class="p">.</span><span class="nx">id</span><span class="p">,</span> <span class="nx">newmodel</span><span class="p">.</span><span class="nx">attributes</span><span class="p">);</span>
+ <span class="p">.</span><span class="nx">done</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">attrs</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">burry</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">attrs</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">idAttribute</span><span class="p">],</span> <span class="nx">attrs</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">model</span><span class="p">.</span><span class="nx">collection</span><span class="p">)</span>
<span class="nx">burry</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s1">&#39;__ids__&#39;</span><span class="p">,</span> <span class="nx">_</span><span class="p">(</span><span class="nx">model</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">models</span><span class="p">).</span><span class="nx">chain</span><span class="p">()</span>
<span class="p">.</span><span class="nx">pluck</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">)</span>
- <span class="p">.</span><span class="nx">union</span><span class="p">([</span><span class="nx">newmodel</span><span class="p">.</span><span class="nx">id</span><span class="p">])</span>
+ <span class="p">.</span><span class="nx">union</span><span class="p">([</span><span class="nx">attrs</span><span class="p">[</span><span class="nx">model</span><span class="p">.</span><span class="nx">idAttribute</span><span class="p">]])</span>
<span class="p">.</span><span class="nx">without</span><span class="p">(</span><span class="kc">undefined</span><span class="p">).</span><span class="nx">value</span><span class="p">());</span>
<span class="p">}).</span><span class="nx">promise</span><span class="p">();</span>
<span class="p">}</span></pre></div>
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-7'>
+ <div class='section' id='section-10'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-7'>#</a>
+ <a class='octothorpe' href='#section-10'>#</a>
</div>
<p><strong>update</strong> resolves immediately by caching the model. Additionally it calls the wrapped sync
to perform a server-side save, which if it fails reverts the cache.</p>
@@ -187,10 +233,10 @@
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-8'>
+ <div class='section' id='section-11'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-8'>#</a>
+ <a class='octothorpe' href='#section-11'>#</a>
</div>
<p><strong>destroy</strong> removes immediately the model from the cache. Additionally it calls the wrapped sync
to perform a server-side delete, which if it fails reverts the cache.</p>
@@ -206,17 +252,16 @@
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-9'>
+ <div class='section' id='section-12'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-9'>#</a>
+ <a class='octothorpe' href='#section-12'>#</a>
</div>
<p>The actual wrapping sync function</p>
</div>
<div class='code'>
<div class="highlight"><pre> <span class="k">return</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">options</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">p</span><span class="p">;</span>
-
<span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</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="s1">&#39;read&#39;</span><span class="o">:</span> <span class="nx">p</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">model</span><span class="p">.</span><span class="nx">id</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="nx">get</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">:</span> <span class="nx">gets</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span> <span class="k">break</span><span class="p">;</span>
@@ -227,10 +272,10 @@
</div>
</div>
<div class='clearall'></div>
- <div class='section' id='section-10'>
+ <div class='section' id='section-13'>
<div class='docs'>
<div class='octowrap'>
- <a class='octothorpe' href='#section-10'>#</a>
+ <a class='octothorpe' href='#section-13'>#</a>
</div>
<p>Fallback for old-style callbacks.</p>
</div>
@@ -242,8 +287,10 @@
<span class="p">};</span>
<span class="p">};</span>
+ <span class="k">return</span> <span class="nx">cachingSync</span><span class="p">;</span>
+
-<span class="p">})(</span><span class="k">this</span><span class="p">.</span><span class="nx">jQuery</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">Backbone</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">Burry</span><span class="p">);</span>
+<span class="p">}));</span>
</pre></div>
</div>

0 comments on commit 3864657

Please sign in to comment.