Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
For #342 - updates validation in internals
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Mar 29, 2013
1 parent 7e69a99 commit 5b51682
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
Binary file modified backbone-fundamentals.epub
Binary file not shown.
11 changes: 8 additions & 3 deletions backbone-fundamentals.md
Expand Up @@ -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({
Expand Down
9 changes: 6 additions & 3 deletions backbone-fundamentals.rtf
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions chapters/03-internals.md
Expand Up @@ -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({
Expand Down
11 changes: 8 additions & 3 deletions index.html
Expand Up @@ -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>
Expand Down

0 comments on commit 5b51682

Please sign in to comment.