Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes #127 - adding reference to shallow copy of objects in validate

  • Loading branch information...
commit 3f2f2374ecfaa70e5797c2954adf4d83697d09b9 1 parent 1c6cb82
@addyosmani authored
View
BIN  backbone-fundamentals.epub
Binary file not shown
View
5 backbone-fundamentals.rtf
@@ -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}
View
2  index.html
@@ -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>
View
3  index.md
@@ -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>
Please sign in to comment.
Something went wrong with that request. Please try again.