Permalink
Browse files

Updated annotated source

  • Loading branch information...
1 parent 63c66eb commit 769c357c3cca9479980f0a7e6965826e9997b39f @thedersen committed Dec 27, 2012
Showing with 38 additions and 40 deletions.
  1. +38 −40 docs/backbone-validation.html
View
78 docs/backbone-validation.html
@@ -1,29 +1,28 @@
-<!DOCTYPE html> <html> <head> <title>backbone-validation.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-12325477-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> backbone-validation.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.Validation v0.7.0</p>
+<!DOCTYPE html> <html> <head> <title>backbone-validation.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-12325477-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> backbone-validation.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.Validation v0.7.1</p>
<p>Copyright (c) 2011-2012 Thomas Pedersen
Distributed under MIT License</p>
<p>Documentation and full license available at:
http://thedersen.com/projects/backbone-validation</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Backbone</span><span class="p">.</span><span class="nx">Validation</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">_</span><span class="p">){</span>
<span class="s1">&#39;use strict&#39;</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> <h2>Default options</h2> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">defaultOptions</span> <span class="o">=</span> <span class="p">{</span>
- <span class="nx">forceUpdate</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
- <span class="nx">selector</span><span class="o">:</span> <span class="s1">&#39;name&#39;</span><span class="p">,</span>
- <span class="nx">labelFormatter</span><span class="o">:</span> <span class="s1">&#39;sentenceCase&#39;</span><span class="p">,</span>
- <span class="nx">valid</span><span class="o">:</span> <span class="nb">Function</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span>
- <span class="nx">invalid</span><span class="o">:</span> <span class="nb">Function</span><span class="p">.</span><span class="nx">prototype</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> <h2>Helper functions</h2> </td> <td class="code"> <div class="highlight"><pre> </pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Formatting functions used for formatting error messages</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">formatFunctions</span> <span class="o">=</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>Uses the configured label formatter to format the attribute name
-to make it more readable for the user</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">formatLabel</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attrName</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">return</span> <span class="nx">defaultLabelFormatters</span><span class="p">[</span><span class="nx">defaultOptions</span><span class="p">.</span><span class="nx">labelFormatter</span><span class="p">](</span><span class="nx">attrName</span><span class="p">,</span> <span class="nx">model</span><span class="p">);</span>
- <span class="p">},</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Replaces nummeric placeholders like {0} in a string with arguments
-passed to the function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">format</span><span class="o">:</span> <span class="kd">function</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="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">);</span>
- <span class="kd">var</span> <span class="nx">text</span> <span class="o">=</span> <span class="nx">args</span><span class="p">.</span><span class="nx">shift</span><span class="p">();</span>
- <span class="k">return</span> <span class="nx">text</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="kd">function</span><span class="p">(</span><span class="nx">match</span><span class="p">,</span> <span class="nx">number</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">return</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="nx">number</span><span class="p">]</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="nx">args</span><span class="p">[</span><span class="nx">number</span><span class="p">]</span> <span class="o">:</span> <span class="nx">match</span><span class="p">;</span>
- <span class="p">});</span>
- <span class="p">}</span>
- <span class="p">};</span>
- </pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Flattens an object
+ <span class="nx">forceUpdate</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
+ <span class="nx">selector</span><span class="o">:</span> <span class="s1">&#39;name&#39;</span><span class="p">,</span>
+ <span class="nx">labelFormatter</span><span class="o">:</span> <span class="s1">&#39;sentenceCase&#39;</span><span class="p">,</span>
+ <span class="nx">valid</span><span class="o">:</span> <span class="nb">Function</span><span class="p">.</span><span class="nx">prototype</span><span class="p">,</span>
+ <span class="nx">invalid</span><span class="o">:</span> <span class="nb">Function</span><span class="p">.</span><span class="nx">prototype</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> <h2>Helper functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Formatting functions used for formatting error messages</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">formatFunctions</span> <span class="o">=</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>Uses the configured label formatter to format the attribute name
+to make it more readable for the user</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">formatLabel</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attrName</span><span class="p">,</span> <span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">defaultLabelFormatters</span><span class="p">[</span><span class="nx">defaultOptions</span><span class="p">.</span><span class="nx">labelFormatter</span><span class="p">](</span><span class="nx">attrName</span><span class="p">,</span> <span class="nx">model</span><span class="p">);</span>
+ <span class="p">},</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Replaces nummeric placeholders like {0} in a string with arguments
+passed to the function</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">format</span><span class="o">:</span> <span class="kd">function</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="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
+ <span class="nx">text</span> <span class="o">=</span> <span class="nx">args</span><span class="p">.</span><span class="nx">shift</span><span class="p">();</span>
+ <span class="k">return</span> <span class="nx">text</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="kd">function</span><span class="p">(</span><span class="nx">match</span><span class="p">,</span> <span class="nx">number</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="nx">number</span><span class="p">]</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">?</span> <span class="nx">args</span><span class="p">[</span><span class="nx">number</span><span class="p">]</span> <span class="o">:</span> <span class="nx">match</span><span class="p">;</span>
+ <span class="p">});</span>
+ <span class="p">}</span>
+ <span class="p">};</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Flattens an object
eg:</p>
<pre><code>var o = {
@@ -89,7 +88,7 @@
the first error message is returned.
If the attribute is valid, an empty string is returned.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">validateAttr</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">computed</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Reduces the array of validators to an error message by
applying all the validators and returning the first error
-message, if any.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">getValidators</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">attr</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">validator</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>Pass the format functions plus the default
+message, if any.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">getValidators</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">attr</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">validator</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>Pass the format functions plus the default
validators as the context to the validator</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">ctx</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">formatFunctions</span><span class="p">,</span> <span class="nx">defaultValidators</span><span class="p">),</span>
<span class="nx">result</span> <span class="o">=</span> <span class="nx">validator</span><span class="p">.</span><span class="nx">fn</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">ctx</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">validator</span><span class="p">.</span><span class="nx">val</span><span class="p">,</span> <span class="nx">model</span><span class="p">,</span> <span class="nx">computed</span><span class="p">);</span>
@@ -135,12 +134,9 @@
<span class="k">return</span> <span class="o">!</span><span class="nx">validateAttr</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">option</span><span class="p">,</span> <span class="nx">flattened</span><span class="p">[</span><span class="nx">option</span><span class="p">],</span> <span class="nx">_</span><span class="p">.</span><span class="nx">extend</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="p">}</span>
<span class="k">if</span><span class="p">(</span><span class="nx">_</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">option</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">i</span> <span class="o">&lt;</span> <span class="nx">option</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="k">if</span><span class="p">(</span><span class="nx">validateAttr</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">option</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">flattened</span><span class="p">[</span><span class="nx">option</span><span class="p">[</span><span class="nx">i</span><span class="p">]],</span> <span class="nx">_</span><span class="p">.</span><span class="nx">extend</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="k">return</span> <span class="kc">false</span><span class="p">;</span>
- <span class="p">}</span>
- <span class="p">}</span>
- <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
+ <span class="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">reduce</span><span class="p">(</span><span class="nx">option</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">attr</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">memo</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">validateAttr</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">flattened</span><span class="p">[</span><span class="nx">attr</span><span class="p">],</span> <span class="nx">_</span><span class="p">.</span><span class="nx">extend</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="p">},</span> <span class="kc">true</span><span class="p">,</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">option</span> <span class="o">===</span> <span class="kc">true</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">validate</span><span class="p">();</span>
@@ -155,16 +151,17 @@
<span class="nx">validatedAttrs</span> <span class="o">=</span> <span class="nx">getValidatedAttrs</span><span class="p">(</span><span class="nx">model</span><span class="p">),</span>
<span class="nx">allAttrs</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">validatedAttrs</span><span class="p">,</span> <span class="nx">model</span><span class="p">.</span><span class="nx">attributes</span><span class="p">,</span> <span class="nx">attrs</span><span class="p">),</span>
<span class="nx">changedAttrs</span> <span class="o">=</span> <span class="nx">flatten</span><span class="p">(</span><span class="nx">attrs</span> <span class="o">||</span> <span class="nx">allAttrs</span><span class="p">),</span>
+
<span class="nx">result</span> <span class="o">=</span> <span class="nx">validateModel</span><span class="p">(</span><span class="nx">model</span><span class="p">,</span> <span class="nx">allAttrs</span><span class="p">);</span>
<span class="nx">model</span><span class="p">.</span><span class="nx">_isValid</span> <span class="o">=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">isValid</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>After validation is performed, loop through all changed attributes
-and call the valid callbacks so the view is updated.</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="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">validatedAttrs</span><span class="p">),</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">){</span>
+and call the valid callbacks so the view is updated.</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="nx">validatedAttrs</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">attr</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">invalid</span> <span class="o">=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">invalidAttrs</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">attr</span><span class="p">);</span>
<span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">invalid</span><span class="p">){</span>
<span class="nx">opt</span><span class="p">.</span><span class="nx">valid</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">opt</span><span class="p">.</span><span class="nx">selector</span><span class="p">);</span>
<span class="p">}</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>After validation is performed, loop through all changed attributes
-and call the invalid callback so the view is updated.</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="nx">_</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">validatedAttrs</span><span class="p">),</span> <span class="kd">function</span><span class="p">(</span><span class="nx">attr</span><span class="p">){</span>
+and call the invalid callback so the view is updated.</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="nx">validatedAttrs</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">attr</span><span class="p">){</span>
<span class="kd">var</span> <span class="nx">invalid</span> <span class="o">=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">invalidAttrs</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">attr</span><span class="p">),</span>
<span class="nx">changed</span> <span class="o">=</span> <span class="nx">changedAttrs</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">attr</span><span class="p">);</span>
@@ -195,12 +192,13 @@
<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>Remove validation from a model whenever a model is
removed from a collection</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">collectionRemove</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">unbindModel</span><span class="p">(</span><span class="nx">model</span><span class="p">);</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>Returns the public methods on Backbone.Validation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</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>Current version of the library</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">version</span><span class="o">:</span> <span class="s1">&#39;0.7.0&#39;</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>Called to configure the default options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">configure</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="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>Returns the public methods on Backbone.Validation</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</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>Current version of the library</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">version</span><span class="o">:</span> <span class="s1">&#39;0.7.1&#39;</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>Called to configure the default options</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">configure</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">_</span><span class="p">.</span><span class="nx">extend</span><span class="p">(</span><span class="nx">defaultOptions</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-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <p>Hooks up validation on a view with a model
or collection</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">bind</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">view</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">model</span> <span class="o">=</span> <span class="nx">view</span><span class="p">.</span><span class="nx">model</span><span class="p">,</span>
<span class="nx">collection</span> <span class="o">=</span> <span class="nx">view</span><span class="p">.</span><span class="nx">collection</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">defaultOptions</span><span class="p">,</span> <span class="nx">defaultCallbacks</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="k">typeof</span> <span class="nx">model</span> <span class="o">===</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">collection</span> <span class="o">===</span> <span class="s1">&#39;undefined&#39;</span><span class="p">){</span>
@@ -211,7 +209,7 @@
<span class="k">if</span><span class="p">(</span><span class="nx">model</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">bindModel</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">model</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
<span class="p">}</span>
- <span class="k">if</span><span class="p">(</span><span class="nx">collection</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">collection</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">collection</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">model</span><span class="p">){</span>
<span class="nx">bindModel</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">model</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>
<span class="p">});</span>
@@ -239,13 +237,13 @@
<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> <h2>Callbacks</h2> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">defaultCallbacks</span> <span class="o">=</span> <span class="nx">Validation</span><span class="p">.</span><span class="nx">callbacks</span> <span class="o">=</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>Gets called when a previously invalid field in the
view becomes valid. Removes any error message.
Should be overridden with custom functionality.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">valid</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">selector</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nx">selector</span> <span class="o">+</span> <span class="s1">&#39;~=&#39;</span> <span class="o">+</span> <span class="nx">attr</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">)</span>
+ <span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nx">selector</span> <span class="o">+</span> <span class="s1">&#39;~=&quot;&#39;</span> <span class="o">+</span> <span class="nx">attr</span> <span class="o">+</span> <span class="s1">&#39;&quot;]&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">removeClass</span><span class="p">(</span><span class="s1">&#39;invalid&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">removeAttr</span><span class="p">(</span><span class="s1">&#39;data-error&#39;</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>Gets called when a field in the view becomes invalid.
Adds a error message.
Should be overridden with custom functionality.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">invalid</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">view</span><span class="p">,</span> <span class="nx">attr</span><span class="p">,</span> <span class="nx">error</span><span class="p">,</span> <span class="nx">selector</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nx">selector</span> <span class="o">+</span> <span class="s1">&#39;~=&#39;</span> <span class="o">+</span> <span class="nx">attr</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="p">)</span>
+ <span class="nx">view</span><span class="p">.</span><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nx">selector</span> <span class="o">+</span> <span class="s1">&#39;~=&quot;&#39;</span> <span class="o">+</span> <span class="nx">attr</span> <span class="o">+</span> <span class="s1">&#39;&quot;]&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">addClass</span><span class="p">(</span><span class="s1">&#39;invalid&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">attr</span><span class="p">(</span><span class="s1">&#39;data-error&#39;</span><span class="p">,</span> <span class="nx">error</span><span class="p">);</span>
<span class="p">}</span>
@@ -295,15 +293,15 @@
<span class="k">return</span> <span class="p">(</span><span class="nx">model</span><span class="p">.</span><span class="nx">labels</span> <span class="o">&amp;&amp;</span> <span class="nx">model</span><span class="p">.</span><span class="nx">labels</span><span class="p">[</span><span class="nx">attrName</span><span class="p">])</span> <span class="o">||</span> <span class="nx">defaultLabelFormatters</span><span class="p">.</span><span class="nx">sentenceCase</span><span class="p">(</span><span class="nx">attrName</span><span class="p">,</span> <span class="nx">model</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <h2>Built in validators</h2> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">defaultValidators</span> <span class="o">=</span> <span class="nx">Validation</span><span class="p">.</span><span class="nx">validators</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</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>Use native trim when defined</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">trim</span> <span class="o">=</span> <span class="nb">String</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">trim</span> <span class="o">?</span>
- <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">return</span> <span class="nx">text</span> <span class="o">===</span> <span class="kc">null</span> <span class="o">?</span> <span class="s1">&#39;&#39;</span> <span class="o">:</span> <span class="nb">String</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">trim</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">text</span><span class="p">);</span>
- <span class="p">}</span> <span class="o">:</span>
- <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">trimLeft</span> <span class="o">=</span> <span class="sr">/^\s+/</span><span class="p">,</span>
- <span class="nx">trimRight</span> <span class="o">=</span> <span class="sr">/\s+$/</span><span class="p">;</span>
+ <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="nx">text</span> <span class="o">===</span> <span class="kc">null</span> <span class="o">?</span> <span class="s1">&#39;&#39;</span> <span class="o">:</span> <span class="nb">String</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">trim</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">text</span><span class="p">);</span>
+ <span class="p">}</span> <span class="o">:</span>
+ <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">trimLeft</span> <span class="o">=</span> <span class="sr">/^\s+/</span><span class="p">,</span>
+ <span class="nx">trimRight</span> <span class="o">=</span> <span class="sr">/\s+$/</span><span class="p">;</span>
- <span class="k">return</span> <span class="nx">text</span> <span class="o">===</span> <span class="kc">null</span> <span class="o">?</span> <span class="s1">&#39;&#39;</span> <span class="o">:</span> <span class="nx">text</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">replace</span><span class="p">(</span><span class="nx">trimLeft</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="nx">trimRight</span><span class="p">,</span> <span class="s1">&#39;&#39;</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>Determines whether or not a value is a number</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">isNumber</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="k">return</span> <span class="nx">text</span> <span class="o">===</span> <span class="kc">null</span> <span class="o">?</span> <span class="s1">&#39;&#39;</span> <span class="o">:</span> <span class="nx">text</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">replace</span><span class="p">(</span><span class="nx">trimLeft</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">).</span><span class="nx">replace</span><span class="p">(</span><span class="nx">trimRight</span><span class="p">,</span> <span class="s1">&#39;&#39;</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>Determines whether or not a value is a number</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">isNumber</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="k">return</span> <span class="nx">_</span><span class="p">.</span><span class="nx">isNumber</span><span class="p">(</span><span class="nx">value</span><span class="p">)</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">value</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nx">value</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">defaultPatterns</span><span class="p">.</span><span class="nx">number</span><span class="p">));</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>Determines whether or not not a value is empty</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">hasValue</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="o">!</span><span class="p">(</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="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">||</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">value</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nx">trim</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">===</span> <span class="s1">&#39;&#39;</span><span class="p">));</span>

0 comments on commit 769c357

Please sign in to comment.