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

Commit

Permalink
Fixes #127 - adding reference to shallow copy of objects in validate
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Jul 7, 2012
1 parent 1c6cb82 commit 3f2f237
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
Binary file modified backbone-fundamentals.epub
Binary file not shown.
5 changes: 5 additions & 0 deletions backbone-fundamentals.rtf
Expand Up @@ -646,6 +646,11 @@ myPhoto.setTitle('Fishing at sea'); \line
var myPhoto = new Photo();\line
myPhoto.set(\{ title: "On the beach" \});\line
//logs Remember to set a source for your image!\par}
{\pard \ql \f0 \sa180 \li0 \fi0 {\b Note}: Backbone passes the {\f1 attributes} object by shallow copy to the {\f1 validate} function using the Underscore {\f1 _.extend} method. This means that it is not possible to change any {\f1 Number}, {\f1 String} or {\f1 Boolean} attribute by reference in the way that one might expect a JavaScript object to behave. As shallow copy doesn\u8217't copy objects by implicitly copying them, but rather, by reference, one ca change the attributes on those objects.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 An example of this (by @fivetanley) is available {\field{\*\fldinst{HYPERLINK "http://jsfiddle.net/2NdDY/7/"}}{\fldrslt{\ul
here
}}}
.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs28 Views\par}
{\pard \ql \f0 \sa180 \li0 \fi0 Views in Backbone don\u8217't contain the markup for your application, but rather they are there to support models by defining the logic for how they should be represented to the user. This is usually achieved using JavaScript templating (e.g.\u160?Mustache, jQuery-tmpl, etc.). A view\u8217's {\f1 render()} function can be bound to a model\u8217's {\f1 change()} event, allowing the view to always be up to date without requiring a full page refresh.\par}
{\pard \ql \f0 \sa180 \li0 \fi0 \b \fs24 Creating new views\par}
Expand Down
2 changes: 2 additions & 0 deletions index.html
Expand Up @@ -560,6 +560,8 @@ <h4 id="model.set">Model.set()</h4>
<span class="kw">var</span> myPhoto = <span class="kw">new</span> Photo();
<span class="kw">myPhoto</span>.<span class="fu">set</span>({ <span class="dt">title</span>: <span class="st">&quot;On the beach&quot;</span> });
<span class="co">//logs Remember to set a source for your image!</span></code></pre>
<p><strong>Note</strong>: Backbone passes the <code>attributes</code> object by shallow copy to the <code>validate</code> function using the Underscore <code>_.extend</code> method. This means that it is not possible to change any <code>Number</code>, <code>String</code> or <code>Boolean</code> attribute by reference in the way that one might expect a JavaScript object to behave. As shallow copy doesn't copy objects by implicitly copying them, but rather, by reference, one ca change the attributes on those objects.</p>
<p>An example of this (by @fivetanley) is available <a href="http://jsfiddle.net/2NdDY/7/">here</a>.</p>
<h3 id="views-1"><a name="thebasics-views" id="thebasics-views">Views</a></h3>
<p>Views in Backbone don't contain the markup for your application, but rather they are there to support models by defining the logic for how they should be represented to the user. This is usually achieved using JavaScript templating (e.g. Mustache, jQuery-tmpl, etc.). A view's <code>render()</code> function can be bound to a model's <code>change()</code> event, allowing the view to always be up to date without requiring a full page refresh.</p>
<h4 id="creating-new-views">Creating new views</h4>
Expand Down
3 changes: 3 additions & 0 deletions index.md
Expand Up @@ -739,6 +739,9 @@ myPhoto.set({ title: "On the beach" });
//logs Remember to set a source for your image!
```

**Note**: Backbone passes the `attributes` object by shallow copy to the `validate` function using the Underscore `_.extend` method. This means that it is not possible to change any `Number`, `String` or `Boolean` attribute by reference in the way that one might expect a JavaScript object to behave. As shallow copy doesn't copy objects by implicitly copying them, but rather, by reference, one ca change the attributes on those objects.

An example of this (by @fivetanley) is available [here](http://jsfiddle.net/2NdDY/7/).


###<a name="thebasics-views" id="thebasics-views">Views</a>
Expand Down

0 comments on commit 3f2f237

Please sign in to comment.