Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

508 lines (440 sloc) 172.222 kB
<!DOCTYPE html> <html> <head> <title>backbone-associations.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> backbone-associations.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>Backbone-associations.js 0.6.2</p>
<p>(c) 2014 Dhruva Ray, Jaynti Kanani, Persistent Systems Ltd.
Backbone-associations may be freely distributed under the MIT license.
For all details and documentation:
https://github.com/dhruvaray/backbone-associations/</p> </td> <td 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></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Set up Backbone-associations appropriately for the environment. Start with AMD.</p> </td> <td class="code"> <div class="highlight"><pre> <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>
<span class="nx">define</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="kd">function</span><span class="p">(</span><span class="nx">_</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Export global even in AMD case in case this script is loaded with
others that may still expect a global Backbone.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">factory</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">_</span><span class="p">);</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Next for Node.js or CommonJS.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">exports</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">_</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;underscore&#39;</span><span class="p">),</span>
<span class="nx">Backbone</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;backbone&#39;</span><span class="p">);</span>
<span class="nx">factory</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">_</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">module</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">exports</span> <span class="o">=</span> <span class="nx">Backbone</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">&#182;</a> </div> <p>Finally, as a browser global.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">factory</span><span class="p">(</span><span class="nx">root</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">_</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">root</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">,</span> <span class="nx">_</span><span class="p">)</span> <span class="p">{</span>
<span class="s2">&quot;use strict&quot;</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">&#182;</a> </div> <h2>Initial Setup</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>The top-level namespace. All public Backbone classes and modules will be attached to this.
Exported for the browser and CommonJS.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">BackboneModel</span><span class="p">,</span> <span class="nx">BackboneCollection</span><span class="p">,</span> <span class="nx">ModelProto</span><span class="p">,</span> <span class="nx">BackboneEvent</span><span class="p">,</span>
<span class="nx">CollectionProto</span><span class="p">,</span> <span class="nx">AssociatedModel</span><span class="p">,</span> <span class="nx">pathChecker</span><span class="p">,</span>
<span class="nx">delimiters</span><span class="p">,</span> <span class="nx">pathSeparator</span><span class="p">,</span> <span class="nx">sourceModel</span><span class="p">,</span> <span class="nx">sourceKey</span><span class="p">,</span> <span class="nx">endPoints</span> <span class="o">=</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">&#182;</a> </div> <p>Create local reference <code>Model</code> prototype.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">BackboneModel</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">;</span>
<span class="nx">BackboneCollection</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">ModelProto</span> <span class="o">=</span> <span class="nx">BackboneModel</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
<span class="nx">CollectionProto</span> <span class="o">=</span> <span class="nx">BackboneCollection</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
<span class="nx">BackboneEvent</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Events</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">VERSION</span><span class="o">:</span> <span class="s2">&quot;0.6.2&quot;</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">&#182;</a> </div> <p>Alternative scopes other than root</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">scopes</span> <span class="o">=</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">&#182;</a> </div> <p>Define <code>getter</code> and <code>setter</code> for <code>separator</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">getSeparator</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">pathSeparator</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">&#182;</a> </div> <p>Define <code>setSeperator</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">setSeparator</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">value</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">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">||</span> <span class="nx">_</span><span class="p">.</span><span class="nx">size</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">value</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>set private properties</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">pathSeparator</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
<span class="nx">pathChecker</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s2">&quot;[\\&quot;</span> <span class="o">+</span> <span class="nx">pathSeparator</span> <span class="o">+</span> <span class="s2">&quot;\\[\\]]+&quot;</span><span class="p">,</span> <span class="s2">&quot;g&quot;</span><span class="p">);</span>
<span class="nx">delimiters</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s2">&quot;[^\\&quot;</span> <span class="o">+</span> <span class="nx">pathSeparator</span> <span class="o">+</span> <span class="s2">&quot;\\[\\]]+&quot;</span><span class="p">,</span> <span class="s2">&quot;g&quot;</span><span class="p">);</span>
<span class="p">};</span>
<span class="k">try</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Define <code>SEPERATOR</code> property to Backbone.Associations</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">,</span> <span class="s1">&#39;SEPARATOR&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="nx">enumerable</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span>
<span class="nx">get</span><span class="o">:</span> <span class="nx">getSeparator</span><span class="p">,</span>
<span class="nx">set</span><span class="o">:</span> <span class="nx">setSeparator</span>
<span class="p">});</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">e</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">&#182;</a> </div> <h2>Backbone.AssociatedModel</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Add <code>Many</code> and <code>One</code> relations to Backbone Object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">Many</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Many</span> <span class="o">=</span> <span class="s2">&quot;Many&quot;</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">One</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">One</span> <span class="o">=</span> <span class="s2">&quot;One&quot;</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">Self</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Self</span> <span class="o">=</span> <span class="s2">&quot;Self&quot;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Set default separator</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">SEPARATOR</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">getSeparator</span> <span class="o">=</span> <span class="nx">getSeparator</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">setSeparator</span> <span class="o">=</span> <span class="nx">setSeparator</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_BUBBLE</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_WILDCARD</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_NC</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="nx">setSeparator</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Define <code>AssociatedModel</code> (Extends Backbone.Model).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">AssociatedModel</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">AssociatedModel</span> <span class="o">=</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">AssociatedModel</span> <span class="o">=</span> <span class="nx">BackboneModel</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Define relations with Associated Model.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">relations</span><span class="o">:</span><span class="kc">undefined</span><span class="p">,</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>Define <code>Model</code> property which can keep track of already fired <code>events</code>,
and prevent redundant event to be triggered in case of cyclic model graphs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_proxyCalls</span><span class="o">:</span><span class="kc">undefined</span><span class="p">,</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Override constructor to set parents</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">constructor</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">attributes</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>Set parent's opportunistically.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">__parents__</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">parents</span> <span class="o">=</span> <span class="p">[</span><span class="nx">options</span><span class="p">.</span><span class="nx">__parents__</span><span class="p">]);</span>
<span class="nx">BackboneModel</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="nx">arguments</span><span class="p">);</span>
<span class="p">},</span>
<span class="nx">on</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="nx">callback</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">BackboneEvent</span><span class="p">.</span><span class="nx">on</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="nx">arguments</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>No optimization possible if nested-events is wanted by the application</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_NC</span><span class="p">)</span> <span class="k">return</span> <span class="nx">result</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>Regular expression used to split event strings.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">eventSplitter</span> <span class="o">=</span> <span class="sr">/\s+/</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>Handle atomic event names only</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nx">name</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="nx">eventSplitter</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">name</span><span class="p">))</span> <span class="o">&amp;&amp;</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">endPoint</span> <span class="o">=</span> <span class="nx">getPathEndPoint</span><span class="p">(</span><span class="nx">name</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">endPoint</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>Increment end point counter. Represents # of nodes which listen to this end point</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">endPoints</span><span class="p">[</span><span class="nx">endPoint</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">endPoints</span><span class="p">[</span><span class="nx">endPoint</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="o">?</span> <span class="mi">1</span> <span class="o">:</span> <span class="p">(</span><span class="nx">endPoints</span><span class="p">[</span><span class="nx">endPoint</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
<span class="p">},</span>
<span class="nx">off</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="nx">callback</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>No optimization possible if nested-events is wanted by the application</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_NC</span><span class="p">)</span> <span class="k">return</span> <span class="nx">BackboneEvent</span><span class="p">.</span><span class="nx">off</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="nx">arguments</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">eventSplitter</span> <span class="o">=</span> <span class="sr">/\s+/</span><span class="p">,</span>
<span class="nx">events</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_events</span><span class="p">,</span>
<span class="nx">listeners</span> <span class="o">=</span> <span class="p">{},</span>
<span class="nx">names</span> <span class="o">=</span> <span class="nx">events</span> <span class="o">?</span> <span class="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">events</span><span class="p">)</span> <span class="o">:</span> <span class="p">[],</span>
<span class="nx">all</span> <span class="o">=</span> <span class="p">(</span><span class="o">!</span><span class="nx">name</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">callback</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">context</span><span class="p">),</span>
<span class="nx">atomic_event</span> <span class="o">=</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="nx">eventSplitter</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">name</span><span class="p">)));</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">all</span> <span class="o">||</span> <span class="nx">atomic_event</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">names</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Store the # of callbacks listening to the event name prior to the <code>off</code> call</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">listeners</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">events</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span> <span class="o">?</span> <span class="nx">events</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]].</span><span class="nx">length</span> <span class="o">:</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>Call Backbone off implementation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">BackboneEvent</span><span class="p">.</span><span class="nx">off</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="nx">arguments</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">all</span> <span class="o">||</span> <span class="nx">atomic_event</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">names</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">endPoint</span> <span class="o">=</span> <span class="nx">getPathEndPoint</span><span class="p">(</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">endPoint</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">events</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]])</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Some listeners wiped out for this name for this object</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">endPoints</span><span class="p">[</span><span class="nx">endPoint</span><span class="p">]</span> <span class="o">-=</span> <span class="p">(</span><span class="nx">listeners</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span> <span class="o">-</span> <span class="nx">events</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]].</span><span class="nx">length</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>All listeners wiped out for this name for this object</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">endPoints</span><span class="p">[</span><span class="nx">endPoint</span><span class="p">]</span> <span class="o">-=</span> <span class="nx">listeners</span><span class="p">[</span><span class="nx">names</span><span class="p">[</span><span class="nx">i</span><span class="p">]];</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <p>Get the value of an attribute.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">get</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">attr</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">cache</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">__attributes__</span><span class="p">,</span>
<span class="nx">val</span> <span class="o">=</span> <span class="nx">ModelProto</span><span class="p">.</span><span class="nx">get</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">attr</span><span class="p">),</span>
<span class="nx">obj</span> <span class="o">=</span> <span class="nx">cache</span> <span class="o">?</span> <span class="p">(</span><span class="nx">isValuePresent</span><span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="o">?</span> <span class="nx">val</span> <span class="o">:</span> <span class="nx">cache</span><span class="p">[</span><span class="nx">attr</span><span class="p">])</span> <span class="o">:</span> <span class="nx">val</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">isValuePresent</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="o">?</span> <span class="nx">obj</span> <span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">_getAttr</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="nx">arguments</span><span class="p">);</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">&#182;</a> </div> <p>Set a hash of model attributes on the Backbone Model.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">set</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">value</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">attributes</span><span class="p">,</span> <span class="nx">result</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <p>Duplicate backbone's behavior to allow separate key/value parameters,
instead of a single 'attributes' object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="o">||</span> <span class="nx">key</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">attributes</span> <span class="o">=</span> <span class="nx">key</span><span class="p">;</span>
<span class="nx">options</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">attributes</span> <span class="o">=</span> <span class="p">{};</span>
<span class="nx">attributes</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">result</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_set</span><span class="p">(</span><span class="nx">attributes</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">&#182;</a> </div> <p>Trigger events which have been blocked until the entire object graph is updated.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">_processPendingEvents</span><span class="p">();</span>
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">&#182;</a> </div> <p>Works with an attribute hash and options + fully qualified paths</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_set</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">attributes</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">attr</span><span class="p">,</span> <span class="nx">modelMap</span><span class="p">,</span> <span class="nx">modelId</span><span class="p">,</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">result</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">attributes</span><span class="p">)</span> <span class="k">return</span> <span class="k">this</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>temp cache of attributes</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">__attributes__</span> <span class="o">=</span> <span class="nx">attributes</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">attr</span> <span class="k">in</span> <span class="nx">attributes</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">&#182;</a> </div> <p>Create a map for each unique object whose attributes we want to set</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">modelMap</span> <span class="o">||</span> <span class="p">(</span><span class="nx">modelMap</span> <span class="o">=</span> <span class="p">{});</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">attr</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">pathChecker</span><span class="p">))</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">pathTokens</span> <span class="o">=</span> <span class="nx">getPathArray</span><span class="p">(</span><span class="nx">attr</span><span class="p">),</span> <span class="nx">initials</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">initial</span><span class="p">(</span><span class="nx">pathTokens</span><span class="p">),</span>
<span class="nx">last</span> <span class="o">=</span> <span class="nx">pathTokens</span><span class="p">[</span><span class="nx">pathTokens</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span>
<span class="nx">parentModel</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="nx">initials</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">parentModel</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">obj</span> <span class="o">=</span> <span class="nx">modelMap</span><span class="p">[</span><span class="nx">parentModel</span><span class="p">.</span><span class="nx">cid</span><span class="p">]</span> <span class="o">||</span>
<span class="p">(</span><span class="nx">modelMap</span><span class="p">[</span><span class="nx">parentModel</span><span class="p">.</span><span class="nx">cid</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;model&#39;</span><span class="o">:</span> <span class="nx">parentModel</span><span class="p">,</span> <span class="s1">&#39;data&#39;</span><span class="o">:</span> <span class="p">{}});</span>
<span class="nx">obj</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">last</span><span class="p">]</span> <span class="o">=</span> <span class="nx">attributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">obj</span> <span class="o">=</span> <span class="nx">modelMap</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">cid</span><span class="p">]</span> <span class="o">||</span> <span class="p">(</span><span class="nx">modelMap</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">cid</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;model&#39;</span><span class="o">:</span><span class="k">this</span><span class="p">,</span> <span class="s1">&#39;data&#39;</span><span class="o">:</span><span class="p">{}});</span>
<span class="nx">obj</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span> <span class="o">=</span> <span class="nx">attributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">];</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">modelMap</span><span class="p">)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">modelId</span> <span class="k">in</span> <span class="nx">modelMap</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">obj</span> <span class="o">=</span> <span class="nx">modelMap</span><span class="p">[</span><span class="nx">modelId</span><span class="p">];</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_setAttr</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">model</span><span class="p">,</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">data</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">||</span> <span class="p">(</span><span class="nx">result</span> <span class="o">=</span> <span class="kc">false</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">result</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_setAttr</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">__attributes__</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">&#182;</a> </div> <p>Set a hash of model attributes on the object,
fire Backbone <code>event</code> with options.
It maintains relations between models during the set operation.
It also bubbles up child events to the parent.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_setAttr</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">attributes</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">attr</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">&#182;</a> </div> <p>Extract attributes and options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span> <span class="o">||</span> <span class="p">(</span><span class="nx">options</span> <span class="o">=</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">unset</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="nx">attr</span> <span class="k">in</span> <span class="nx">attributes</span><span class="p">)</span> <span class="nx">attributes</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span> <span class="o">=</span> <span class="k">void</span> <span class="mi">0</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">parents</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">parents</span> <span class="o">||</span> <span class="p">[];</span>
<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">relations</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">&#182;</a> </div> <p>Iterate over <code>this.relations</code> and <code>set</code> model and collection values
if <code>relations</code> are available.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">relations</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">relation</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">relationKey</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">key</span><span class="p">,</span>
<span class="nx">activationContext</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">scope</span> <span class="o">||</span> <span class="nx">root</span><span class="p">,</span>
<span class="nx">relatedModel</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_transformRelatedModel</span><span class="p">(</span><span class="nx">relation</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">),</span>
<span class="nx">collectionType</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_transformCollectionType</span><span class="p">(</span><span class="nx">relation</span><span class="p">,</span> <span class="nx">relatedModel</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">),</span>
<span class="nx">map</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">relation</span><span class="p">.</span><span class="nx">map</span><span class="p">)</span> <span class="o">?</span> <span class="nx">map2Scope</span><span class="p">(</span><span class="nx">relation</span><span class="p">.</span><span class="nx">map</span><span class="p">,</span> <span class="nx">activationContext</span><span class="p">)</span> <span class="o">:</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">map</span><span class="p">,</span>
<span class="nx">currVal</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">],</span>
<span class="nx">idKey</span> <span class="o">=</span> <span class="nx">currVal</span> <span class="o">&amp;&amp;</span> <span class="nx">currVal</span><span class="p">.</span><span class="nx">idAttribute</span><span class="p">,</span>
<span class="nx">val</span><span class="p">,</span> <span class="nx">relationOptions</span><span class="p">,</span> <span class="nx">data</span><span class="p">,</span> <span class="nx">relationValue</span><span class="p">,</span> <span class="nx">newCtx</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">&#182;</a> </div> <p>Merge in <code>options</code> specific to this relation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">relationOptions</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">options</span> <span class="o">?</span> <span class="nx">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">({},</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">options</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">:</span> <span class="nx">options</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">])</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">&#182;</a> </div> <p>Get value of attribute with relation key in <code>val</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">val</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">result</span><span class="p">(</span><span class="nx">attributes</span><span class="p">,</span> <span class="nx">relationKey</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">&#182;</a> </div> <p>Map <code>val</code> if a transformation function is provided.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">val</span> <span class="o">=</span> <span class="nx">map</span> <span class="o">?</span> <span class="nx">map</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">collectionType</span> <span class="o">?</span> <span class="nx">collectionType</span> <span class="o">:</span> <span class="nx">relatedModel</span><span class="p">)</span> <span class="o">:</span> <span class="nx">val</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">isValuePresent</span><span class="p">(</span><span class="nx">val</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">data</span> <span class="o">=</span> <span class="nx">val</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">&#182;</a> </div> <p>If <code>relation.type</code> is <code>Backbone.Many</code>,
Create <code>Backbone.Collection</code> with passed data and perform Backbone <code>set</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">relation</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Many</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">currVal</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">&#182;</a> </div> <p>Setting this flag will prevent events from firing immediately. That way clients
will not get events until the entire object graph is updated.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">currVal</span><span class="p">.</span><span class="nx">_deferEvents</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">&#182;</a> </div> <p>Use Backbone.Collection's <code>reset</code> or smart <code>set</code> method</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">currVal</span><span class="p">[</span><span class="nx">relationOptions</span><span class="p">.</span><span class="nx">reset</span> <span class="o">?</span> <span class="s1">&#39;reset&#39;</span> <span class="o">:</span> <span class="s1">&#39;set&#39;</span><span class="p">](</span>
<span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">BackboneCollection</span> <span class="o">?</span> <span class="nx">val</span><span class="p">.</span><span class="nx">models</span> <span class="o">:</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">relationOptions</span><span class="p">);</span>
<span class="nx">data</span> <span class="o">=</span> <span class="nx">currVal</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">newCtx</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">BackboneCollection</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">data</span> <span class="o">=</span> <span class="nx">val</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">data</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_createCollection</span><span class="p">(</span>
<span class="nx">collectionType</span> <span class="o">||</span> <span class="nx">BackboneCollection</span><span class="p">,</span>
<span class="nx">relation</span><span class="p">.</span><span class="nx">collectionOptions</span> <span class="o">||</span> <span class="p">(</span><span class="nx">relatedModel</span> <span class="o">?</span> <span class="p">{</span><span class="nx">model</span><span class="o">:</span> <span class="nx">relatedModel</span><span class="p">}</span> <span class="o">:</span> <span class="p">{})</span>
<span class="p">);</span>
<span class="nx">data</span><span class="p">[</span><span class="nx">relationOptions</span><span class="p">.</span><span class="nx">reset</span> <span class="o">?</span> <span class="s1">&#39;reset&#39;</span> <span class="o">:</span> <span class="s1">&#39;set&#39;</span><span class="p">](</span><span class="nx">val</span><span class="p">,</span> <span class="nx">relationOptions</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">relation</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">One</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">hasOwnProperty</span> <span class="o">=</span> <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">)</span> <span class="o">?</span>
<span class="nx">val</span><span class="p">.</span><span class="nx">attributes</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">idKey</span><span class="p">)</span> <span class="o">:</span>
<span class="nx">val</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">idKey</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">newIdKey</span> <span class="o">=</span> <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">)</span> <span class="o">?</span>
<span class="nx">val</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">idKey</span><span class="p">]</span> <span class="o">:</span>
<span class="nx">val</span><span class="p">[</span><span class="nx">idKey</span><span class="p">];</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">&#182;</a> </div> <p>Is the passed in data for the same key?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">currVal</span> <span class="o">&amp;&amp;</span> <span class="nx">hasOwnProperty</span> <span class="o">&amp;&amp;</span>
<span class="nx">currVal</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">idKey</span><span class="p">]</span> <span class="o">===</span> <span class="nx">newIdKey</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">&#182;</a> </div> <p>Setting this flag will prevent events from firing immediately. That way clients
will not get events until the entire object graph is updated.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">currVal</span><span class="p">.</span><span class="nx">_deferEvents</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">&#182;</a> </div> <p>Perform the traditional <code>set</code> operation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">currVal</span><span class="p">.</span><span class="nx">_set</span><span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span> <span class="o">?</span> <span class="nx">val</span><span class="p">.</span><span class="nx">attributes</span> <span class="o">:</span> <span class="nx">val</span><span class="p">,</span> <span class="nx">relationOptions</span><span class="p">);</span>
<span class="nx">data</span> <span class="o">=</span> <span class="nx">currVal</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">newCtx</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">data</span> <span class="o">=</span> <span class="nx">val</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">relationOptions</span><span class="p">.</span><span class="nx">__parents__</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="nx">data</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">relatedModel</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">relationOptions</span><span class="p">);</span>
<span class="k">delete</span> <span class="nx">relationOptions</span><span class="p">.</span><span class="nx">__parents__</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;type attribute must be specified and &#39;</span> <span class="o">+</span>
<span class="s1">&#39;have the values Backbone.One or Backbone.Many&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="nx">attributes</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">]</span> <span class="o">=</span> <span class="nx">data</span><span class="p">;</span>
<span class="nx">relationValue</span> <span class="o">=</span> <span class="nx">data</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>Add proxy events to respective parents.
Only add callback if not defined or new Ctx has been identified.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">newCtx</span> <span class="o">||</span> <span class="p">(</span><span class="nx">relationValue</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">relationValue</span><span class="p">.</span><span class="nx">_proxyCallback</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">relationValue</span><span class="p">.</span><span class="nx">_proxyCallback</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">relationValue</span><span class="p">.</span><span class="nx">_proxyCallback</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_BUBBLE</span> <span class="o">&amp;&amp;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_bubbleEvent</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">relationKey</span><span class="p">,</span> <span class="nx">relationValue</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
<span class="p">};</span>
<span class="p">}</span>
<span class="nx">relationValue</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">&quot;all&quot;</span><span class="p">,</span> <span class="nx">relationValue</span><span class="p">.</span><span class="nx">_proxyCallback</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <p>Distinguish between the value of undefined versus a set no-op</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">attributes</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">relationKey</span><span class="p">))</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_setupParents</span><span class="p">(</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">],</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">]);</span>
<span class="p">},</span> <span class="k">this</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">&#182;</a> </div> <p>Return results for <code>BackboneModel.set</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">ModelProto</span><span class="p">.</span><span class="nx">set</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">&#182;</a> </div> <p>Bubble-up event to <code>parent</code> Model</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_bubbleEvent</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">relationKey</span><span class="p">,</span> <span class="nx">relationValue</span><span class="p">,</span> <span class="nx">eventArguments</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nx">eventArguments</span><span class="p">,</span>
<span class="nx">opt</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">split</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">),</span>
<span class="nx">eventType</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
<span class="nx">catch_all</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;nested-change&quot;</span><span class="p">,</span>
<span class="nx">isChangeEvent</span> <span class="o">=</span> <span class="nx">eventType</span> <span class="o">===</span> <span class="s2">&quot;change&quot;</span><span class="p">,</span>
<span class="nx">eventObject</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
<span class="nx">indexEventObject</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
<span class="nx">_proxyCalls</span> <span class="o">=</span> <span class="nx">relationValue</span><span class="p">.</span><span class="nx">_proxyCalls</span><span class="p">,</span>
<span class="nx">cargs</span><span class="p">,</span>
<span class="nx">eventPath</span> <span class="o">=</span> <span class="nx">opt</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
<span class="nx">eSrc</span> <span class="o">=</span> <span class="o">!</span><span class="nx">eventPath</span> <span class="o">||</span> <span class="p">(</span><span class="nx">eventPath</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">pathSeparator</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span>
<span class="nx">basecolEventPath</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">&#182;</a> </div> <p>Short circuit the listen in to the nested-graph event</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">catch_all</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">&#182;</a> </div> <p>Record the source of the event</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">eSrc</span><span class="p">)</span> <span class="nx">sourceKey</span> <span class="o">=</span> <span class="p">(</span><span class="nx">getPathEndPoint</span><span class="p">(</span><span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">||</span> <span class="nx">relationKey</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">&#182;</a> </div> <p>Short circuit the event bubbling as there are no listeners for this end point</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_NC</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">endPoints</span><span class="p">[</span><span class="nx">sourceKey</span><span class="p">])</span> <span class="k">return</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">&#182;</a> </div> <p>Short circuit the listen in to the wild-card event</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_WILDCARD</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="sr">/\[\*\]/g</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">eventPath</span><span class="p">))</span> <span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">relationValue</span> <span class="k">instanceof</span> <span class="nx">BackboneCollection</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nx">isChangeEvent</span> <span class="o">||</span> <span class="nx">eventPath</span><span class="p">))</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">&#182;</a> </div> <p>O(n) search :(</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">indexEventObject</span> <span class="o">=</span> <span class="nx">relationValue</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">sourceModel</span> <span class="o">||</span> <span class="nx">eventObject</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="k">this</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>A quicker way to identify the model which caused an update inside the collection (while bubbling up)</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">sourceModel</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">&#182;</a> </div> <p>Manipulate <code>eventPath</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">eventPath</span> <span class="o">=</span> <span class="nx">relationKey</span> <span class="o">+</span> <span class="p">((</span><span class="nx">indexEventObject</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nx">isChangeEvent</span> <span class="o">||</span> <span class="nx">eventPath</span><span class="p">))</span> <span class="o">?</span>
<span class="s2">&quot;[&quot;</span> <span class="o">+</span> <span class="nx">indexEventObject</span> <span class="o">+</span> <span class="s2">&quot;]&quot;</span> <span class="o">:</span> <span class="s2">&quot;&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="nx">eventPath</span> <span class="o">?</span> <span class="nx">pathSeparator</span> <span class="o">+</span> <span class="nx">eventPath</span> <span class="o">:</span> <span class="s2">&quot;&quot;</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>Short circuit collection * events</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_WILDCARD</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">basecolEventPath</span> <span class="o">=</span> <span class="nx">eventPath</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\[\d+\]/g</span><span class="p">,</span> <span class="s1">&#39;[*]&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">cargs</span> <span class="o">=</span> <span class="p">[];</span>
<span class="nx">cargs</span><span class="p">.</span><span class="nx">push</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">cargs</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
<span class="nx">cargs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nx">eventType</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</span> <span class="o">+</span> <span class="nx">eventPath</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>Create a collection modified event with wild-card</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_WILDCARD</span> <span class="o">&amp;&amp;</span> <span class="nx">eventPath</span> <span class="o">!==</span> <span class="nx">basecolEventPath</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">cargs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nx">cargs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="nx">eventType</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</span> <span class="o">+</span> <span class="nx">basecolEventPath</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">&#182;</a> </div> <p>If event has been already triggered as result of same source <code>eventPath</code>,
no need to re-trigger event to prevent cycle.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_proxyCalls</span> <span class="o">=</span> <span class="nx">relationValue</span><span class="p">.</span><span class="nx">_proxyCalls</span> <span class="o">=</span> <span class="p">(</span><span class="nx">_proxyCalls</span> <span class="o">||</span> <span class="p">{});</span>
<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_isEventAvailable</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">_proxyCalls</span><span class="p">,</span> <span class="nx">eventPath</span><span class="p">))</span> <span class="k">return</span> <span class="k">this</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">&#182;</a> </div> <p>Add <code>eventPath</code> in <code>_proxyCalls</code> to keep track of already triggered <code>event</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_proxyCalls</span><span class="p">[</span><span class="nx">eventPath</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>Set up previous attributes correctly.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">isChangeEvent</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_previousAttributes</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">]</span> <span class="o">=</span> <span class="nx">relationValue</span><span class="p">.</span><span class="nx">_previousAttributes</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">changed</span><span class="p">[</span><span class="nx">relationKey</span><span class="p">]</span> <span class="o">=</span> <span class="nx">relationValue</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">&#182;</a> </div> <p>Bubble up event to parent <code>model</code> with new changed arguments.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">trigger</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="nx">cargs</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">&#182;</a> </div> <p>Only fire for change. Not change:attribute</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">EVENTS_NC</span> <span class="o">&amp;&amp;</span> <span class="nx">isChangeEvent</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">eventPath</span><span class="p">)</span> <span class="o">!=</span> <span class="nx">args</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">ncargs</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;nested-change&quot;</span><span class="p">,</span> <span class="nx">eventPath</span><span class="p">,</span> <span class="nx">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]];</span>
<span class="nx">args</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">&amp;&amp;</span> <span class="nx">ncargs</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">args</span><span class="p">[</span><span class="mi">2</span><span class="p">]);</span> <span class="c1">//args[2] will be options if present</span>
<span class="k">this</span><span class="p">.</span><span class="nx">trigger</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="nx">ncargs</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">&#182;</a> </div> <p>Remove <code>eventPath</code> from <code>_proxyCalls</code>,
if <code>eventPath</code> and <code>_proxyCalls</code> are available,
which allow event to be triggered on for next operation of <code>set</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">_proxyCalls</span> <span class="o">&amp;&amp;</span> <span class="nx">eventPath</span><span class="p">)</span> <span class="k">delete</span> <span class="nx">_proxyCalls</span><span class="p">[</span><span class="nx">eventPath</span><span class="p">];</span>
<span class="nx">sourceModel</span> <span class="o">=</span> <span class="kc">undefined</span><span class="p">;</span>
<span class="k">return</span> <span class="k">this</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">&#182;</a> </div> <p>Has event been fired from this source. Used to prevent event recursion in cyclic graphs</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_isEventAvailable</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">_proxyCalls</span><span class="p">,</span> <span class="nx">path</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">_proxyCalls</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">eventKey</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">path</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">eventKey</span><span class="p">,</span> <span class="nx">path</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="nx">eventKey</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span>
<span class="p">});</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">&#182;</a> </div> <p>Maintain reverse pointers - a.k.a parents</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_setupParents</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">updated</span><span class="p">,</span> <span class="nx">original</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">&#182;</a> </div> <p>Set new parent for updated</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">updated</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">updated</span><span class="p">.</span><span class="nx">parents</span> <span class="o">=</span> <span class="nx">updated</span><span class="p">.</span><span class="nx">parents</span> <span class="o">||</span> <span class="p">[];</span>
<span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">updated</span><span class="p">.</span><span class="nx">parents</span><span class="p">,</span> <span class="k">this</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nx">updated</span><span class="p">.</span><span class="nx">parents</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">&#182;</a> </div> <p>Remove <code>this</code> from the earlier set value's parents (if the new value is different).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">original</span> <span class="o">&amp;&amp;</span> <span class="nx">original</span><span class="p">.</span><span class="nx">parents</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">original</span> <span class="o">!=</span> <span class="nx">updated</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">original</span><span class="p">.</span><span class="nx">parents</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">difference</span><span class="p">(</span><span class="nx">original</span><span class="p">.</span><span class="nx">parents</span><span class="p">,</span> <span class="p">[</span><span class="k">this</span><span class="p">]);</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">&#182;</a> </div> <p>Don't bubble to this parent anymore</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">original</span><span class="p">.</span><span class="nx">_proxyCallback</span> <span class="o">&amp;&amp;</span> <span class="nx">original</span><span class="p">.</span><span class="nx">off</span><span class="p">(</span><span class="s2">&quot;all&quot;</span><span class="p">,</span> <span class="nx">original</span><span class="p">.</span><span class="nx">_proxyCallback</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">&#182;</a> </div> <p>Returns new <code>collection</code> (or derivatives) of type <code>options.model</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_createCollection</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">type</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">options</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">defaults</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="p">{</span><span class="nx">model</span><span class="o">:</span> <span class="nx">type</span><span class="p">.</span><span class="nx">model</span><span class="p">});</span>
<span class="kd">var</span> <span class="nx">c</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">type</span><span class="p">([],</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="o">?</span> <span class="nx">options</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">)</span> <span class="o">:</span> <span class="nx">options</span><span class="p">);</span>
<span class="nx">c</span><span class="p">.</span><span class="nx">parents</span> <span class="o">=</span> <span class="p">[</span><span class="k">this</span><span class="p">];</span>
<span class="k">return</span> <span class="nx">c</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">&#182;</a> </div> <p>Process all pending events after the entire object graph has been updated</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_processPendingEvents</span><span class="o">:</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<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">_processedEvents</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_processedEvents</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_deferEvents</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">&#182;</a> </div> <p>Trigger all pending events</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">e</span><span class="p">.</span><span class="nx">c</span><span class="p">.</span><span class="nx">trigger</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">c</span><span class="p">,</span> <span class="nx">e</span><span class="p">.</span><span class="nx">a</span><span class="p">);</span>
<span class="p">});</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span> <span class="o">=</span> <span class="p">[];</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">&#182;</a> </div> <p>Traverse down the object graph and call process pending events on sub-trees</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">relations</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">relation</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">val</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">relation</span><span class="p">.</span><span class="nx">key</span><span class="p">];</span>
<span class="nx">val</span> <span class="o">&amp;&amp;</span> <span class="nx">val</span><span class="p">.</span><span class="nx">_processPendingEvents</span> <span class="o">&amp;&amp;</span> <span class="nx">val</span><span class="p">.</span><span class="nx">_processPendingEvents</span><span class="p">();</span>
<span class="p">},</span> <span class="k">this</span><span class="p">);</span>
<span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_processedEvents</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">&#182;</a> </div> <p>Process the raw <code>relatedModel</code> value set in a relation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_transformRelatedModel</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">relation</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">relatedModel</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">relatedModel</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">activationContext</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">scope</span> <span class="o">||</span> <span class="nx">root</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">&#182;</a> </div> <p>Call function if relatedModel is implemented as a function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">relatedModel</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">(</span><span class="nx">relatedModel</span><span class="p">.</span><span class="nx">prototype</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">))</span>
<span class="nx">relatedModel</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">relatedModel</span><span class="p">)</span> <span class="o">?</span>
<span class="nx">relatedModel</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">relation</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">)</span> <span class="o">:</span>
<span class="nx">relatedModel</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">&#182;</a> </div> <p>Get class if relation and map is stored as a string.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">relatedModel</span> <span class="o">&amp;&amp;</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">relatedModel</span><span class="p">))</span> <span class="p">{</span>
<span class="nx">relatedModel</span> <span class="o">=</span> <span class="p">(</span><span class="nx">relatedModel</span> <span class="o">===</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Self</span><span class="p">)</span> <span class="o">?</span>
<span class="k">this</span><span class="p">.</span><span class="nx">constructor</span> <span class="o">:</span>
<span class="nx">map2Scope</span><span class="p">(</span><span class="nx">relatedModel</span><span class="p">,</span> <span class="nx">activationContext</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">&#182;</a> </div> <p>Error checking</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">relation</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">One</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">relatedModel</span><span class="p">)</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;specify a relatedModel for Backbone.One type&#39;</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="nx">relatedModel</span><span class="p">.</span><span class="nx">prototype</span> <span class="k">instanceof</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Model</span><span class="p">))</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;specify an AssociatedModel or Backbone.Model for Backbone.One type&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">relatedModel</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">&#182;</a> </div> <p>Process the raw <code>collectionType</code> value set in a relation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_transformCollectionType</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">relation</span><span class="p">,</span> <span class="nx">relatedModel</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">collectionType</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">collectionType</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">activationContext</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">scope</span> <span class="o">||</span> <span class="nx">root</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">collectionType</span> <span class="o">&amp;&amp;</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">collectionType</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
<span class="p">(</span><span class="nx">collectionType</span><span class="p">.</span><span class="nx">prototype</span> <span class="k">instanceof</span> <span class="nx">BackboneModel</span><span class="p">))</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;type is of Backbone.Model. Specify derivatives of Backbone.Collection&#39;</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">&#182;</a> </div> <p>Call function if collectionType is implemented as a function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">collectionType</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">(</span><span class="nx">collectionType</span><span class="p">.</span><span class="nx">prototype</span> <span class="k">instanceof</span> <span class="nx">BackboneCollection</span><span class="p">))</span>
<span class="nx">collectionType</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">collectionType</span><span class="p">)</span> <span class="o">?</span>
<span class="nx">collectionType</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">relation</span><span class="p">,</span> <span class="nx">attributes</span><span class="p">)</span> <span class="o">:</span> <span class="nx">collectionType</span><span class="p">;</span>
<span class="nx">collectionType</span> <span class="o">&amp;&amp;</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">collectionType</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
<span class="p">(</span><span class="nx">collectionType</span> <span class="o">=</span> <span class="nx">map2Scope</span><span class="p">(</span><span class="nx">collectionType</span><span class="p">,</span> <span class="nx">activationContext</span><span class="p">));</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">&#182;</a> </div> <p><code>collectionType</code> of defined <code>relation</code> should be instance of <code>Backbone.Collection</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">collectionType</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">collectionType</span><span class="p">.</span><span class="nx">prototype</span> <span class="k">instanceof</span> <span class="nx">BackboneCollection</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;collectionType must inherit from Backbone.Collection&#39;</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">&#182;</a> </div> <p>Error checking</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">relation</span><span class="p">.</span><span class="nx">type</span> <span class="o">===</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Many</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">relatedModel</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="nx">collectionType</span><span class="p">))</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;specify either a relatedModel or collectionType&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">collectionType</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">&#182;</a> </div> <p>Override trigger to defer events in the object graph.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">trigger</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></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">&#182;</a> </div> <p>Defer event processing</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_deferEvents</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span> <span class="o">||</span> <span class="p">[];</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">&#182;</a> </div> <p>Maintain a queue of pending events to trigger after the entire object graph is updated.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span><span class="p">.</span><span class="nx">push</span><span class="p">({</span><span class="nx">c</span><span class="o">:</span><span class="k">this</span><span class="p">,</span> <span class="nx">a</span><span class="o">:</span><span class="nx">arguments</span><span class="p">});</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">ModelProto</span><span class="p">.</span><span class="nx">trigger</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="nx">arguments</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-89">&#182;</a> </div> <p>The JSON representation of the model.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">toJSON</span><span class="o">:</span><span class="kd">function</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">json</span> <span class="o">=</span> <span class="p">{},</span> <span class="nx">aJson</span><span class="p">;</span>
<span class="nx">json</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">idAttribute</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">id</span><span class="p">;</span>
<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">visited</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">visited</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-90">&#182;</a> </div> <p>Get json representation from <code>BackboneModel.toJSON</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">json</span> <span class="o">=</span> <span class="nx">ModelProto</span><span class="p">.</span><span class="nx">toJSON</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="nx">arguments</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-91">&#182;</a> </div> <p>Pick up only the keys you want to serialize</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">serialize_keys</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">json</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">pick</span><span class="p">(</span><span class="nx">json</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">serialize_keys</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-92">&#182;</a> </div> <p>If <code>this.relations</code> is defined, iterate through each <code>relation</code>
and added it's json representation to parents' json representation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">relations</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="k">this</span><span class="p">.</span><span class="nx">relations</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">relation</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">key</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">key</span><span class="p">,</span>
<span class="nx">remoteKey</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">remoteKey</span><span class="p">,</span>
<span class="nx">attr</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">key</span><span class="p">],</span>
<span class="nx">serialize</span> <span class="o">=</span> <span class="o">!</span><span class="nx">relation</span><span class="p">.</span><span class="nx">isTransient</span><span class="p">,</span>
<span class="nx">serialize_keys</span> <span class="o">=</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">serialize</span> <span class="o">||</span> <span class="p">[],</span>
<span class="nx">_options</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">options</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-93">&#182;</a> </div> <p>Remove default Backbone serialization for associations.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">delete</span> <span class="nx">json</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-94">&#182;</a> </div> <p>Assign to remoteKey if specified. Otherwise use the default key.
Only for non-transient relationships</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">serialize</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-95">&#182;</a> </div> <p>Pass the keys to serialize as options to the toJSON method.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">serialize_keys</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">_options</span> <span class="o">?</span>
<span class="p">(</span><span class="nx">_options</span><span class="p">.</span><span class="nx">serialize_keys</span> <span class="o">=</span> <span class="nx">serialize_keys</span><span class="p">)</span> <span class="o">:</span>
<span class="p">(</span><span class="nx">_options</span> <span class="o">=</span> <span class="p">{</span><span class="nx">serialize_keys</span><span class="o">:</span> <span class="nx">serialize_keys</span><span class="p">})</span>
<span class="p">}</span>
<span class="nx">aJson</span> <span class="o">=</span> <span class="nx">attr</span> <span class="o">&amp;&amp;</span> <span class="nx">attr</span><span class="p">.</span><span class="nx">toJSON</span> <span class="o">?</span> <span class="nx">attr</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">(</span><span class="nx">_options</span><span class="p">)</span> <span class="o">:</span> <span class="nx">attr</span><span class="p">;</span>
<span class="nx">json</span><span class="p">[</span><span class="nx">remoteKey</span> <span class="o">||</span> <span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">aJson</span><span class="p">)</span> <span class="o">?</span> <span class="nx">_</span><span class="p">.</span><span class="nx">compact</span><span class="p">(</span><span class="nx">aJson</span><span class="p">)</span> <span class="o">:</span> <span class="nx">aJson</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">},</span> <span class="k">this</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">visited</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">json</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-96"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-96">&#182;</a> </div> <p>Create a new model with identical attributes to this one.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">clone</span><span class="o">:</span> <span class="kd">function</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="k">new</span> <span class="k">this</span><span class="p">.</span><span class="nx">constructor</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">(</span><span class="nx">options</span><span class="p">));</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-97">&#182;</a> </div> <p>Call this if you want to set an <code>AssociatedModel</code> to a falsy value like undefined/null directly.
Not calling this will leak memory and have wrong parents.
See test case "parent relations"</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">cleanup</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="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="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">relations</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">relation</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">val</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">relation</span><span class="p">.</span><span class="nx">key</span><span class="p">];</span>
<span class="k">if</span><span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">val</span><span class="p">.</span><span class="nx">_proxyCallback</span> <span class="o">&amp;&amp;</span> <span class="nx">val</span><span class="p">.</span><span class="nx">off</span><span class="p">(</span><span class="s2">&quot;all&quot;</span><span class="p">,</span> <span class="nx">val</span><span class="p">.</span><span class="nx">_proxyCallback</span><span class="p">,</span> <span class="k">this</span><span class="p">);</span>
<span class="nx">val</span><span class="p">.</span><span class="nx">parents</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">difference</span><span class="p">(</span><span class="nx">val</span><span class="p">.</span><span class="nx">parents</span><span class="p">,</span> <span class="p">[</span><span class="k">this</span><span class="p">]);</span>
<span class="p">}</span>
<span class="p">},</span> <span class="k">this</span><span class="p">);</span>
<span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">listen</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="p">.</span><span class="nx">off</span><span class="p">();</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-98">&#182;</a> </div> <p>Override destroy to perform house-keeping on <code>parents</code> collection</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">destroy</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">options</span><span class="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="nx">_</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="o">:</span> <span class="p">{};</span>
<span class="nx">options</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">defaults</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="p">{</span><span class="nx">remove_references</span><span class="o">:</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">listen</span><span class="o">:</span> <span class="kc">true</span><span class="p">});</span>
<span class="kd">var</span> <span class="nx">model</span> <span class="o">=</span> <span class="k">this</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">remove_references</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">wait</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-99">&#182;</a> </div> <p>Proxy success implementation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">success</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">success</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-100">&#182;</a> </div> <p>Substitute with an implementation which will remove references to <code>model</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">options</span><span class="p">.</span><span class="nx">success</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">resp</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">success</span><span class="p">)</span> <span class="nx">success</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">resp</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
<span class="nx">model</span><span class="p">.</span><span class="nx">cleanup</span><span class="p">(</span><span class="nx">options</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-101">&#182;</a> </div> <p>Call the base implementation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">xhr</span> <span class="o">=</span> <span class="nx">ModelProto</span><span class="p">.</span><span class="nx">destroy</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="p">[</span><span class="nx">options</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">remove_references</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">wait</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">model</span><span class="p">.</span><span class="nx">cleanup</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">xhr</span><span class="p">;</span>
<span class="p">},</span></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-102">&#182;</a> </div> <p>Navigate the path to the leaf object in the path to query for the attribute value</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_getAttr</span><span class="o">:</span><span class="kd">function</span> <span class="p">(</span><span class="nx">path</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
<span class="nx">cache</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">__attributes__</span><span class="p">,</span></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-103">&#182;</a> </div> <p>Tokenize the path</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">attrs</span> <span class="o">=</span> <span class="nx">getPathArray</span><span class="p">(</span><span class="nx">path</span><span class="p">),</span>
<span class="nx">key</span><span class="p">,</span>
<span class="nx">i</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">size</span><span class="p">(</span><span class="nx">attrs</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">attrs</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">key</span> <span class="o">=</span> <span class="nx">attrs</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">result</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-104">&#182;</a> </div> <p>Navigate the path to get to the result</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">result</span> <span class="o">=</span> <span class="nx">result</span> <span class="k">instanceof</span> <span class="nx">BackboneCollection</span>
<span class="o">?</span> <span class="p">(</span><span class="nb">isNaN</span><span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="o">?</span> <span class="kc">undefined</span> <span class="o">:</span> <span class="nx">result</span><span class="p">.</span><span class="nx">at</span><span class="p">(</span><span class="nx">key</span><span class="p">))</span>
<span class="o">:</span> <span class="p">(</span><span class="nx">cache</span> <span class="o">?</span>
<span class="p">(</span><span class="nx">isValuePresent</span><span class="p">(</span><span class="nx">result</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="o">?</span> <span class="nx">result</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">:</span> <span class="nx">cache</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="o">:</span>
<span class="nx">result</span><span class="p">.</span><span class="nx">attributes</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span>
<span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">result</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-105">&#182;</a> </div> <p>Tokenize the fully qualified event path</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">getPathArray</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">path</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">path</span> <span class="o">===</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">];</span>
<span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">path</span><span class="p">)</span> <span class="o">?</span> <span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">delimiters</span><span class="p">))</span> <span class="o">:</span> <span class="nx">path</span> <span class="o">||</span> <span class="p">[];</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">&#182;</a> </div> <p>Get the end point of the path.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">getPathEndPoint</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">path</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">path</span><span class="p">)</span> <span class="k">return</span> <span class="nx">path</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-107"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-107">&#182;</a> </div> <p>event_type:<path></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">tokens</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">tokens</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">path</span> <span class="o">=</span> <span class="nx">tokens</span><span class="p">[</span><span class="nx">tokens</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span>
<span class="nx">tokens</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">pathSeparator</span><span class="p">);</span>
<span class="k">return</span> <span class="nx">tokens</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">?</span> <span class="nx">tokens</span><span class="p">[</span><span class="nx">tokens</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">].</span><span class="nx">split</span><span class="p">(</span><span class="s1">&#39;[&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">:</span> <span class="nx">tokens</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">split</span><span class="p">(</span><span class="s1">&#39;[&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">];</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-108"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-108">&#182;</a> </div> <p>path of 0 depth</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="s2">&quot;&quot;</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="kd">var</span> <span class="nx">map2Scope</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">target</span><span class="p">,</span>
<span class="nx">scopes</span> <span class="o">=</span> <span class="p">[</span><span class="nx">context</span><span class="p">];</span></pre></div> </td> </tr> <tr id="section-109"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-109">&#182;</a> </div> <p>Check global scopes after passed-in context</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">scopes</span><span class="p">.</span><span class="nx">push</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">scopes</span><span class="p">,</span> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">Associations</span><span class="p">.</span><span class="nx">scopes</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">ctx</span><span class="p">,</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">scopes</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">l</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">ctx</span> <span class="o">=</span> <span class="nx">scopes</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span> <span class="p">{</span>
<span class="nx">target</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="nx">pathSeparator</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">memo</span><span class="p">,</span> <span class="nx">elem</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">memo</span><span class="p">[</span><span class="nx">elem</span><span class="p">];</span>
<span class="p">},</span> <span class="nx">ctx</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">target</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">target</span><span class="p">;</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-110"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-110">&#182;</a> </div> <p>Infer the relation from the collection's parents and find the appropriate map for the passed in <code>models</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">map2models</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">parents</span><span class="p">,</span> <span class="nx">target</span><span class="p">,</span> <span class="nx">models</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">relation</span><span class="p">,</span> <span class="nx">surrogate</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-111"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-111">&#182;</a> </div> <p>Iterate over collection's parents</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">parents</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">parent</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-112"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-112">&#182;</a> </div> <p>Iterate over relations</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">relation</span> <span class="o">=</span> <span class="nx">_</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">parent</span><span class="p">.</span><span class="nx">relations</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">rel</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">parent</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">rel</span><span class="p">.</span><span class="nx">key</span><span class="p">)</span> <span class="o">===</span> <span class="nx">target</span><span class="p">;</span>
<span class="p">},</span> <span class="k">this</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">relation</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">surrogate</span> <span class="o">=</span> <span class="nx">parent</span><span class="p">;</span><span class="c1">//surrogate for transformation</span>
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span><span class="c1">//break;</span>
<span class="p">}</span>
<span class="p">},</span> <span class="k">this</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-113"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-113">&#182;</a> </div> <p>If we found a relation and it has a mapping function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">relation</span> <span class="o">&amp;&amp;</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">map</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">relation</span><span class="p">.</span><span class="nx">map</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">surrogate</span><span class="p">,</span> <span class="nx">models</span><span class="p">,</span> <span class="nx">target</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">models</span><span class="p">;</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-114"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-114">&#182;</a> </div> <p>Utility method to check for null or undefined value</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">isValuePresent</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isUndefined</span><span class="p">(</span><span class="nx">value</span><span class="p">))</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span><span class="p">.</span><span class="nx">isNull</span><span class="p">(</span><span class="nx">value</span><span class="p">));</span>
<span class="p">};</span>
<span class="kd">var</span> <span class="nx">proxies</span> <span class="o">=</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-115"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-115">&#182;</a> </div> <p>Proxy Backbone collection methods</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">_</span><span class="p">.</span><span class="nx">each</span><span class="p">([</span><span class="s1">&#39;set&#39;</span><span class="p">,</span> <span class="s1">&#39;remove&#39;</span><span class="p">,</span> <span class="s1">&#39;reset&#39;</span><span class="p">],</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">method</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">proxies</span><span class="p">[</span><span class="nx">method</span><span class="p">]</span> <span class="o">=</span> <span class="nx">BackboneCollection</span><span class="p">.</span><span class="nx">prototype</span><span class="p">[</span><span class="nx">method</span><span class="p">];</span>
<span class="nx">CollectionProto</span><span class="p">[</span><span class="nx">method</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</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></pre></div> </td> </tr> <tr id="section-116"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-116">&#182;</a> </div> <p>Short-circuit if this collection doesn't hold <code>AssociatedModels</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">model</span><span class="p">.</span><span class="nx">prototype</span> <span class="k">instanceof</span> <span class="nx">AssociatedModel</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="p">.</span><span class="nx">parents</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-117"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-117">&#182;</a> </div> <p>Find a map function if available and perform a transformation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">arguments</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nx">map2models</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">parents</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="nx">models</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">proxies</span><span class="p">[</span><span class="nx">method</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="nx">arguments</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span></pre></div> </td> </tr> <tr id="section-118"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-118">&#182;</a> </div> <p>Override trigger to defer events in the object graph.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">proxies</span><span class="p">[</span><span class="s1">&#39;trigger&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nx">CollectionProto</span><span class="p">[</span><span class="s1">&#39;trigger&#39;</span><span class="p">];</span>
<span class="nx">CollectionProto</span><span class="p">[</span><span class="s1">&#39;trigger&#39;</span><span class="p">]</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">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_deferEvents</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span> <span class="o">||</span> <span class="p">[];</span></pre></div> </td> </tr> <tr id="section-119"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-119">&#182;</a> </div> <p>Maintain a queue of pending events to trigger after the entire object graph is updated.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">_pendingEvents</span><span class="p">.</span><span class="nx">push</span><span class="p">({</span><span class="nx">c</span><span class="o">:</span><span class="k">this</span><span class="p">,</span> <span class="nx">a</span><span class="o">:</span><span class="nx">arguments</span><span class="p">});</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">proxies</span><span class="p">[</span><span class="s1">&#39;trigger&#39;</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="nx">arguments</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-120"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-120">&#182;</a> </div> <p>Attach process pending event functionality on collections as well. Re-use from <code>AssociatedModel</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">CollectionProto</span><span class="p">.</span><span class="nx">_processPendingEvents</span> <span class="o">=</span> <span class="nx">AssociatedModel</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">_processPendingEvents</span><span class="p">;</span>
<span class="nx">CollectionProto</span><span class="p">.</span><span class="nx">on</span> <span class="o">=</span> <span class="nx">AssociatedModel</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">on</span><span class="p">;</span>
<span class="nx">CollectionProto</span><span class="p">.</span><span class="nx">off</span> <span class="o">=</span> <span class="nx">AssociatedModel</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">off</span><span class="p">;</span>
<span class="k">return</span> <span class="nx">Backbone</span><span class="p">;</span>
<span class="p">}));</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
Jump to Line
Something went wrong with that request. Please try again.