Permalink
Browse files

Issue #77, don't call toJSON unless you need to.

  • Loading branch information...
1 parent 78cf070 commit 86c3089379676279d14489fc92780d605ecafb03 @jashkenas jashkenas committed Nov 15, 2010
Showing with 11 additions and 10 deletions.
  1. +2 −2 backbone-min.js
  2. +4 −4 backbone.js
  3. +4 −4 docs/backbone.html
  4. +1 −0 index.html
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -875,16 +875,16 @@
// Useful when interfacing with server-side languages like **PHP** that make
// it difficult to read the body of `PUT` requests.
Backbone.sync = function(method, model, success, error) {
- var sendModel = method === 'create' || method === 'update';
var type = methodMap[method];
- var modelJSON = JSON.stringify(model.toJSON());
+ var modelJSON = (method === 'create' || method === 'update') ?
+ JSON.stringify(model.toJSON()) : null;
// Default JSON-request options.
var params = {
url: getUrl(model),
type: type,
contentType: 'application/json',
- data: sendModel ? modelJSON : null,
+ data: modelJSON,
dataType: 'json',
processData: false,
success: success,
@@ -895,7 +895,7 @@
if (Backbone.emulateJSON) {
params.contentType = 'application/x-www-form-urlencoded';
params.processData = true;
- params.data = sendModel ? {model : modelJSON} : {};
+ params.data = modelJSON ? {model : modelJSON} : {};
}
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
View
@@ -565,21 +565,21 @@
<code>application/json</code> with the model in a param named <code>model</code>.
Useful when interfacing with server-side languages like <strong>PHP</strong> that make
it difficult to read the body of <code>PUT</code> requests.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">sync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">method</span><span class="p">,</span> <span class="nx">model</span><span class="p">,</span> <span class="nx">success</span><span class="p">,</span> <span class="nx">error</span><span class="p">)</span> <span class="p">{</span>
- <span class="kd">var</span> <span class="nx">sendModel</span> <span class="o">=</span> <span class="nx">method</span> <span class="o">===</span> <span class="s1">&#39;create&#39;</span> <span class="o">||</span> <span class="nx">method</span> <span class="o">===</span> <span class="s1">&#39;update&#39;</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">type</span> <span class="o">=</span> <span class="nx">methodMap</span><span class="p">[</span><span class="nx">method</span><span class="p">];</span>
- <span class="kd">var</span> <span class="nx">modelJSON</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">model</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">());</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">&#182;</a> </div> <p>Default JSON-request options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">modelJSON</span> <span class="o">=</span> <span class="p">(</span><span class="nx">method</span> <span class="o">===</span> <span class="s1">&#39;create&#39;</span> <span class="o">||</span> <span class="nx">method</span> <span class="o">===</span> <span class="s1">&#39;update&#39;</span><span class="p">)</span> <span class="o">?</span>
+ <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">model</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">())</span> <span class="o">:</span> <span class="kc">null</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">&#182;</a> </div> <p>Default JSON-request options.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">params</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">url</span><span class="o">:</span> <span class="nx">getUrl</span><span class="p">(</span><span class="nx">model</span><span class="p">),</span>
<span class="nx">type</span><span class="o">:</span> <span class="nx">type</span><span class="p">,</span>
<span class="nx">contentType</span><span class="o">:</span> <span class="s1">&#39;application/json&#39;</span><span class="p">,</span>
- <span class="nx">data</span><span class="o">:</span> <span class="nx">sendModel</span> <span class="o">?</span> <span class="nx">modelJSON</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span>
+ <span class="nx">data</span><span class="o">:</span> <span class="nx">modelJSON</span><span class="p">,</span>
<span class="nx">dataType</span><span class="o">:</span> <span class="s1">&#39;json&#39;</span><span class="p">,</span>
<span class="nx">processData</span><span class="o">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nx">success</span><span class="o">:</span> <span class="nx">success</span><span class="p">,</span>
<span class="nx">error</span><span class="o">:</span> <span class="nx">error</span>
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-107"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-107">&#182;</a> </div> <p>For older servers, emulate JSON by encoding the request into an HTML-form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">emulateJSON</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">params</span><span class="p">.</span><span class="nx">contentType</span> <span class="o">=</span> <span class="s1">&#39;application/x-www-form-urlencoded&#39;</span><span class="p">;</span>
<span class="nx">params</span><span class="p">.</span><span class="nx">processData</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
- <span class="nx">params</span><span class="p">.</span><span class="nx">data</span> <span class="o">=</span> <span class="nx">sendModel</span> <span class="o">?</span> <span class="p">{</span><span class="nx">model</span> <span class="o">:</span> <span class="nx">modelJSON</span><span class="p">}</span> <span class="o">:</span> <span class="p">{};</span>
+ <span class="nx">params</span><span class="p">.</span><span class="nx">data</span> <span class="o">=</span> <span class="nx">modelJSON</span> <span class="o">?</span> <span class="p">{</span><span class="nx">model</span> <span class="o">:</span> <span class="nx">modelJSON</span><span class="p">}</span> <span class="o">:</span> <span class="p">{};</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-108"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-108">&#182;</a> </div> <p>For older servers, emulate HTTP by mimicking the HTTP method with <code>_method</code>
And an <code>X-HTTP-Method-Override</code> header.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">emulateHTTP</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">type</span> <span class="o">===</span> <span class="s1">&#39;PUT&#39;</span> <span class="o">||</span> <span class="nx">type</span> <span class="o">===</span> <span class="s1">&#39;DELETE&#39;</span><span class="p">)</span> <span class="p">{</span>
View
@@ -1760,6 +1760,7 @@ <h2 id="changelog">Change Log</h2>
All <tt>"add"</tt> and <tt>"remove"</tt> events are now sent through the
model, so that views can listen for them without having to know about the
collection. Added a <tt>remove</tt> method to <a href="#View">Backbone.View</a>.
+ <tt>toJSON</tt> is no longer called at all for <tt>'read'</tt> and <tt>'delete'</tt> requests.
Backbone routes are now able to load empty URL fragments.
</p>

0 comments on commit 86c3089

Please sign in to comment.