Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

For #342 - adding a note about the backbone validation plugin

  • Loading branch information...
commit 9fc1642ef0f4631ac214a7a93ca5c865912de45c 1 parent be68f22
@addyosmani authored
View
BIN  backbone-fundamentals.epub
Binary file not shown
View
19 backbone-fundamentals.md
@@ -5809,6 +5809,21 @@ That's it!
The Backbone.validateAll logic doesn't override the default Backbone logic by default and so it's perfectly capable of being used for scenarios where you might care more about field-validation [performance](http://jsperf.com/backbone-validateall) as well as those where you don't. Both solutions presented in this section should work fine however.
+##### Backbone.Validation
+
+As we've seen, the `validate` method Backbone offers is `undefined` by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.
+
+A helpful plugin for Backbone called [Backbone.Validation](https://github.com/thedersen/backbone.validation) attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the `validate` method behind the scenes.
+
+One of the useful methods this plugin includes is (pseudo) live validation via a `preValidate` method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the `preValidate` method, passing it the name of the attribute as well as the value you would like validated.
+
+```javascript
+// If the value of the attribute is invalid, a truthy error message is returned
+// if not, it returns a falsy value
+
+var errorMsg = myModel.preValidate('attribute', 'value');
+```
+
#### Avoiding Conflicts With Multiple Backbone Versions
**Problem**
@@ -6692,7 +6707,7 @@ define(['jquery', 'backbone', 'underscore'].concat(models),
will be ignored. This is by design as it ensures that dynamic dependency/module loading can still take place even after optimization.
-Although the RequireJS optimizer works fine in both Node and Java environments, it's strongly recommended to run it under Node as it executes significantly faster there.
+Although the RequireJS optimizer works fine in both Node and Java environments, it's strongly recommended to run it under Node as it executes significantly faster there.
To get started with r.js, grab it from the [RequireJS download page](http://requirejs.org/docs/download.html#rjs) or [through NPM](http://requirejs.org/docs/optimization.html#download). To begin getting our project to build with r.js, we will need to create a new build profile.
@@ -6704,7 +6719,7 @@ Assuming the code for our application and external dependencies are in `app/libs
out: 'dist/main.js',
```
-The paths above are relative to the `baseUrl` for our project and in our case it would make sense to make this the `app` folder. The `out` parameter informs r.js that we want to concatenate everything into a single file.
+The paths above are relative to the `baseUrl` for our project and in our case it would make sense to make this the `app` folder. The `out` parameter informs r.js that we want to concatenate everything into a single file, that should be called `main.js` and be created within the `dist/` directory. Note that here, we do need to add the `.js` extension to the filename. Earlier we saw that when referencing modules by filenames, you don't need to use the `.js` extension, however this is one case in which you do.
Alternatively, we can specify `dir`, which will ensure the contents of our `app` directory are copied into this directory. e.g:
View
13 backbone-fundamentals.rtf
@@ -4532,6 +4532,17 @@ user.set(\{ 'firstname': 'Greg' \}, \{validate: true, validateAll: false\});\par
performance
}}}
as well as those where you don\u8217't. Both solutions presented in this section should work fine however.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs20 Backbone.Validation\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 As we\u8217've seen, the {\f1 validate} method Backbone offers is {\f1 undefined} by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 A helpful plugin for Backbone called {\field{\*\fldinst{HYPERLINK "https://github.com/thedersen/backbone.validation"}}{\fldrslt{\ul
+Backbone.Validation
+}}}
+ attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the {\f1 validate} method behind the scenes.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 One of the useful methods this plugin includes is (pseudo) live validation via a {\f1 preValidate} method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the {\f1 preValidate} method, passing it the name of the attribute as well as the value you would like validated.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 \f1 // If the value of the attribute is invalid, a truthy error message is returned\line
+// if not, it returns a falsy value\line
+\line
+var errorMsg = myModel.preValidate('attribute', 'value');\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs24 Avoiding Conflicts With Multiple Backbone Versions\par}
{\pard \ql \f0 \sa180 \li0 \fi0 {\b Problem}\par}
{\pard \ql \f0 \sa180 \li0 \fi0 In instances out of your control, you may have to work around having more than one version of Backbone in the same page. How do you work around this without causing conflicts?\par}
@@ -5193,7 +5204,7 @@ through NPM
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 (\{\line
baseUrl: 'app',\line
out: 'dist/main.js',\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 The paths above are relative to the {\f1 baseUrl} for our project and in our case it would make sense to make this the {\f1 app} folder. The {\f1 out} parameter informs r.js that we want to concatenate everything into a single file.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 The paths above are relative to the {\f1 baseUrl} for our project and in our case it would make sense to make this the {\f1 app} folder. The {\f1 out} parameter informs r.js that we want to concatenate everything into a single file, that should be called {\f1 main.js} and be created within the {\f1 dist/} directory. Note that here, we do need to add the {\f1 .js} extension to the filename. Earlier we saw that when referencing modules by filenames, you don\u8217't need to use the {\f1 .js} extension, however this is one case in which you do.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 Alternatively, we can specify {\f1 dir}, which will ensure the contents of our {\f1 app} directory are copied into this directory. e.g:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 (\{\line
baseUrl: 'app',\line
View
15 chapters/07-common-problems.md
@@ -625,6 +625,21 @@ That's it!
The Backbone.validateAll logic doesn't override the default Backbone logic by default and so it's perfectly capable of being used for scenarios where you might care more about field-validation [performance](http://jsperf.com/backbone-validateall) as well as those where you don't. Both solutions presented in this section should work fine however.
+##### Backbone.Validation
+
+As we've seen, the `validate` method Backbone offers is `undefined` by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.
+
+A helpful plugin for Backbone called [Backbone.Validation](https://github.com/thedersen/backbone.validation) attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the `validate` method behind the scenes.
+
+One of the useful methods this plugin includes is (pseudo) live validation via a `preValidate` method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the `preValidate` method, passing it the name of the attribute as well as the value you would like validated.
+
+```javascript
+// If the value of the attribute is invalid, a truthy error message is returned
+// if not, it returns a falsy value
+
+var errorMsg = myModel.preValidate('attribute', 'value');
+```
+
#### Avoiding Conflicts With Multiple Backbone Versions
**Problem**
View
14 index.html
@@ -184,7 +184,9 @@
<li><a href="#disposing-view-hierarchies">Disposing View Hierarchies</a></li>
<li><a href="#rendering-view-hierarchies">Rendering View Hierarchies</a></li>
<li><a href="#working-with-nested-models-or-collections">Working With Nested Models Or Collections</a></li>
-<li><a href="#better-model-property-validation">Better Model Property Validation</a></li>
+<li><a href="#better-model-property-validation">Better Model Property Validation</a><ul>
+<li><a href="#backbone.validation">Backbone.Validation</a></li>
+</ul></li>
<li><a href="#avoiding-conflicts-with-multiple-backbone-versions">Avoiding Conflicts With Multiple Backbone Versions</a></li>
<li><a href="#building-model-and-view-hierarchies">Building Model And View Hierarchies</a></li>
<li><a href="#event-aggregators-and-mediators">Event Aggregators And Mediators</a><ul>
@@ -4802,6 +4804,14 @@ <h4 id="better-model-property-validation"><a href="#TOC">Better Model Property V
<span class="kw">user</span>.<span class="fu">set</span>({ <span class="ch">&#39;firstname&#39;</span>: <span class="ch">&#39;Greg&#39;</span> }, {<span class="dt">validate</span>: <span class="kw">true</span>, <span class="dt">validateAll</span>: <span class="kw">false</span>});</code></pre>
<p>That’s it!</p>
<p>The Backbone.validateAll logic doesn’t override the default Backbone logic by default and so it’s perfectly capable of being used for scenarios where you might care more about field-validation <a href="http://jsperf.com/backbone-validateall">performance</a> as well as those where you don’t. Both solutions presented in this section should work fine however.</p>
+<h5 id="backbone.validation"><a href="#TOC">Backbone.Validation</a></h5>
+<p>As we’ve seen, the <code>validate</code> method Backbone offers is <code>undefined</code> by default and you need to override it with your own custom validation logic to get model validation in place. Often developers run into the issue of implementing this validation as nested ifs and elses, which can become unmaintainable when things get complicated.</p>
+<p>A helpful plugin for Backbone called <a href="https://github.com/thedersen/backbone.validation">Backbone.Validation</a> attempts to solve this problem by offering an extensible way to declare validation rules on the model and override the <code>validate</code> method behind the scenes.</p>
+<p>One of the useful methods this plugin includes is (pseudo) live validation via a <code>preValidate</code> method. This can be used to check on key-press if the input for a model is valid without changing the model itself. You can run any validators for a model attribute by calling the <code>preValidate</code> method, passing it the name of the attribute as well as the value you would like validated.</p>
+<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="co">// If the value of the attribute is invalid, a truthy error message is returned</span>
+<span class="co">// if not, it returns a falsy value</span>
+
+<span class="kw">var</span> errorMsg = <span class="kw">myModel</span>.<span class="fu">preValidate</span>(<span class="ch">&#39;attribute&#39;</span>, <span class="ch">&#39;value&#39;</span>);</code></pre>
<h4 id="avoiding-conflicts-with-multiple-backbone-versions"><a href="#TOC">Avoiding Conflicts With Multiple Backbone Versions</a></h4>
<p><strong>Problem</strong></p>
<p>In instances out of your control, you may have to work around having more than one version of Backbone in the same page. How do you work around this without causing conflicts?</p>
@@ -5404,7 +5414,7 @@ <h3 id="optimizing-backbone-apps-for-production-with-the-requirejs-optimizer"><a
<pre><code>({
baseUrl: &#39;app&#39;,
out: &#39;dist/main.js&#39;,</code></pre>
-<p>The paths above are relative to the <code>baseUrl</code> for our project and in our case it would make sense to make this the <code>app</code> folder. The <code>out</code> parameter informs r.js that we want to concatenate everything into a single file.</p>
+<p>The paths above are relative to the <code>baseUrl</code> for our project and in our case it would make sense to make this the <code>app</code> folder. The <code>out</code> parameter informs r.js that we want to concatenate everything into a single file, that should be called <code>main.js</code> and be created within the <code>dist/</code> directory. Note that here, we do need to add the <code>.js</code> extension to the filename. Earlier we saw that when referencing modules by filenames, you don’t need to use the <code>.js</code> extension, however this is one case in which you do.</p>
<p>Alternatively, we can specify <code>dir</code>, which will ensure the contents of our <code>app</code> directory are copied into this directory. e.g:</p>
<pre><code>({
baseUrl: &#39;app&#39;,

0 comments on commit 9fc1642

Please sign in to comment.
Something went wrong with that request. Please try again.