Permalink
Browse files

For #342 - updates validation in internals

  • Loading branch information...
1 parent 7e69a99 commit 5b51682900e6d32f850b1aef897e69c96b2273f4 @addyosmani committed Mar 29, 2013
Showing with 30 additions and 12 deletions.
  1. BIN backbone-fundamentals.epub
  2. +8 −3 backbone-fundamentals.md
  3. +6 −3 backbone-fundamentals.rtf
  4. +8 −3 chapters/03-internals.md
  5. +8 −3 index.html
View
BIN backbone-fundamentals.epub
Binary file not shown.
View
11 backbone-fundamentals.md
@@ -812,11 +812,16 @@ console.log('Todo set as completed: ' + myTodo.get('completed'));
#### Validation
-Backbone supports model validation through `Model.validate()`, which allows checking the attribute values for a model prior to setting them.
+Backbone supports model validation through `model.validate()`, which allows checking the attribute values for a model prior to setting them. Validation by default occurs when the model is persisted using the `.save()` method or when `.set()` is called if `{validate:true}` is passed as an argument.
-Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from `.validate()`. If they are invalid, an error value should be returned instead. If an error is returned, an `invalid` event will occur and listeners will be passed the unmodified model and the error value.
+Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from `.validate()`. If they are invalid, an error value should be returned instead.
-Validation automatically occurs when the model is persisted using the `.save()` method or when `.set()` is called with the validate option set to true. A basic validation example can be seen below:
+Should an error be returned:
+
+* An `invalid` event will triggered, setting the `validationError` property on the model with the value which is returned by this method.
+* `.save()` will not continue and the attributes of the model will not be modified on the server
+
+A basic validation example can be seen below:
```javascript
var Todo = Backbone.Model.extend({
View
9 backbone-fundamentals.rtf
@@ -636,9 +636,12 @@ console.log('Todo set as completed: ' + myTodo.get('completed'));\line
// Title value for this model has changed.\line
// Todo set as completed: true\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs24 Validation\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 Backbone supports model validation through {\f1 Model.validate()}, which allows checking the attribute values for a model prior to setting them.\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from {\f1 .validate()}. If they are invalid, an error value should be returned instead. If an error is returned, an {\f1 invalid} event will occur and listeners will be passed the unmodified model and the error value.\par}
-{\pard \ql \f0 \sa180 \li0 \fi0 Validation automatically occurs when the model is persisted using the {\f1 .save()} method or when {\f1 .set()} is called with the validate option set to true. A basic validation example can be seen below:\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 Backbone supports model validation through {\f1 model.validate()}, which allows checking the attribute values for a model prior to setting them. Validation by default occurs when the model is persisted using the {\f1 .save()} method or when {\f1 .set()} is called if {\f1 \{validate:true\}} is passed as an argument.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from {\f1 .validate()}. If they are invalid, an error value should be returned instead.\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 Should an error be returned:\par}
+{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab An {\f1 invalid} event will triggered, setting the {\f1 validationError} property on the model with the value which is returned by this method.\par}
+{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\f1 .save()} will not continue and the attributes of the model will not be modified on the server\sa180\par}
+{\pard \ql \f0 \sa180 \li0 \fi0 A basic validation example can be seen below:\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \f1 var Todo = Backbone.Model.extend(\{\line
defaults: \{\line
completed: false\line
View
11 chapters/03-internals.md
@@ -295,11 +295,16 @@ console.log('Todo set as completed: ' + myTodo.get('completed'));
#### Validation
-Backbone supports model validation through `Model.validate()`, which allows checking the attribute values for a model prior to setting them.
+Backbone supports model validation through `model.validate()`, which allows checking the attribute values for a model prior to setting them. Validation by default occurs when the model is persisted using the `.save()` method or when `.set()` is called if `{validate:true}` is passed as an argument.
-Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from `.validate()`. If they are invalid, an error value should be returned instead. If an error is returned, an `invalid` event will occur and listeners will be passed the unmodified model and the error value.
+Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from `.validate()`. If they are invalid, an error value should be returned instead.
-Validation automatically occurs when the model is persisted using the `.save()` method or when `.set()` is called with the validate option set to true. A basic validation example can be seen below:
+Should an error be returned:
+
+* An `invalid` event will triggered, setting the `validationError` property on the model with the value which is returned by this method.
+* `.save()` will not continue and the attributes of the model will not be modified on the server
+
+A basic validation example can be seen below:
```javascript
var Todo = Backbone.Model.extend({
View
11 index.html
@@ -969,9 +969,14 @@ <h4 id="listening-for-changes-to-your-model"><a href="#TOC">Listening for change
<span class="co">// Title value for this model has changed.</span>
<span class="co">// Todo set as completed: true</span></code></pre>
<h4 id="validation"><a href="#TOC">Validation</a></h4>
-<p>Backbone supports model validation through <code>Model.validate()</code>, which allows checking the attribute values for a model prior to setting them.</p>
-<p>Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from <code>.validate()</code>. If they are invalid, an error value should be returned instead. If an error is returned, an <code>invalid</code> event will occur and listeners will be passed the unmodified model and the error value.</p>
-<p>Validation automatically occurs when the model is persisted using the <code>.save()</code> method or when <code>.set()</code> is called with the validate option set to true. A basic validation example can be seen below:</p>
+<p>Backbone supports model validation through <code>model.validate()</code>, which allows checking the attribute values for a model prior to setting them. Validation by default occurs when the model is persisted using the <code>.save()</code> method or when <code>.set()</code> is called if <code>{validate:true}</code> is passed as an argument.</p>
+<p>Validation functions can be as simple or complex as necessary. If the attributes provided are valid, nothing should be returned from <code>.validate()</code>. If they are invalid, an error value should be returned instead.</p>
+<p>Should an error be returned:</p>
+<ul>
+<li>An <code>invalid</code> event will triggered, setting the <code>validationError</code> property on the model with the value which is returned by this method.</li>
+<li><code>.save()</code> will not continue and the attributes of the model will not be modified on the server</li>
+</ul>
+<p>A basic validation example can be seen below:</p>
<pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Todo = <span class="kw">Backbone.Model</span>.<span class="fu">extend</span>({
<span class="dt">defaults</span>: {
<span class="dt">completed</span>: <span class="kw">false</span>

0 comments on commit 5b51682

Please sign in to comment.