[Pkg-javascript-commits] [backbone] 09/09: Issue #77, don't call toJSON unless you need to.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:19 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.3.1
in repository backbone.

commit 86c3089379676279d14489fc92780d605ecafb03
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Mon Nov 15 10:21:01 2010 -0500

    Issue #77, don't call toJSON unless you need to.
---
 backbone-min.js    | 4 ++--
 backbone.js        | 8 ++++----
 docs/backbone.html | 8 ++++----
 index.html         | 1 +
 4 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/backbone-min.js b/backbone-min.js
index 46d9a8e..f52dea0 100644
--- a/backbone-min.js
+++ b/backbone-min.js
@@ -22,5 +22,5 @@ this.route(a,b,this[b])}},_routeToRegExp:function(a){a=a.replace(o,"([^/]*)").re
 this.getFragment();return f.any(this.handlers,function(b){if(b.route.test(a)){b.callback(a);return true}})},saveLocation:function(a){a=(a||"").replace(k,"");if(this.fragment!=a){window.location.hash=this.fragment=a;if(this.iframe&&a!=this.getFragment(this.iframe.location)){this.iframe.document.open().close();this.iframe.location.hash=a}}}});e.View=function(a){this._configure(a||{});this._ensureElement();this.delegateEvents();this.initialize&&this.initialize(a)};var l=function(a){return h [...]
 q=/^(\w+)\s*(.*)$/;f.extend(e.View.prototype,e.Events,{tagName:"div",$:l,jQuery:l,render:function(){return this},remove:function(){h(this.el).remove();return this},make:function(a,b,c){a=document.createElement(a);b&&h(a).attr(b);c&&h(a).html(c);return a},delegateEvents:function(a){if(a||(a=this.events)){h(this.el).unbind();for(var b in a){var c=a[b],d=b.match(q),g=d[1];d=d[2];c=f.bind(this[c],this);d===""?h(this.el).bind(g,c):h(this.el).delegate(d,g,c)}}},_configure:function(a){if(this.o [...]
 f.extend({},this.options,a);if(a.model)this.model=a.model;if(a.collection)this.collection=a.collection;if(a.el)this.el=a.el;if(a.id)this.id=a.id;if(a.className)this.className=a.className;if(a.tagName)this.tagName=a.tagName;this.options=a},_ensureElement:function(){if(!this.el){var a={};if(this.id)a.id=this.id;if(this.className)a.className=this.className;this.el=this.make(this.tagName,a)}}});var m=function(a,b){var c=r(this,a,b);c.extend=m;return c};e.Model.extend=e.Collection.extend=e.Co [...]
-e.View.extend=m;var s={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};e.sync=function(a,b,c,d){var g=a==="create"||a==="update",i=s[a];a=JSON.stringify(b.toJSON());b={url:j(b),type:i,contentType:"application/json",data:g?a:null,dataType:"json",processData:false,success:c,error:d};if(e.emulateJSON){b.contentType="application/x-www-form-urlencoded";b.processData=true;b.data=g?{model:a}:{}}if(e.emulateHTTP)if(i==="PUT"||i==="DELETE"){if(e.emulateJSON)b.data._method=i;b.type="POST" [...]
-function(t){t.setRequestHeader("X-HTTP-Method-Override",i)}}h.ajax(b)};var n=function(){},r=function(a,b,c){var d;d=b&&b.hasOwnProperty("constructor")?b.constructor:function(){return a.apply(this,arguments)};n.prototype=a.prototype;d.prototype=new n;b&&f.extend(d.prototype,b);c&&f.extend(d,c);d.prototype.constructor=d;d.__super__=a.prototype;return d},j=function(a){if(!(a&&a.url))throw Error("A 'url' property or function must be specified");return f.isFunction(a.url)?a.url():a.url}})();
+e.View.extend=m;var s={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};e.sync=function(a,b,c,d){var g=s[a];a=a==="create"||a==="update"?JSON.stringify(b.toJSON()):null;b={url:j(b),type:g,contentType:"application/json",data:a,dataType:"json",processData:false,success:c,error:d};if(e.emulateJSON){b.contentType="application/x-www-form-urlencoded";b.processData=true;b.data=a?{model:a}:{}}if(e.emulateHTTP)if(g==="PUT"||g==="DELETE"){if(e.emulateJSON)b.data._method=g;b.type="POST";b.b [...]
+function(i){i.setRequestHeader("X-HTTP-Method-Override",g)}}h.ajax(b)};var n=function(){},r=function(a,b,c){var d;d=b&&b.hasOwnProperty("constructor")?b.constructor:function(){return a.apply(this,arguments)};n.prototype=a.prototype;d.prototype=new n;b&&f.extend(d.prototype,b);c&&f.extend(d,c);d.prototype.constructor=d;d.__super__=a.prototype;return d},j=function(a){if(!(a&&a.url))throw Error("A 'url' property or function must be specified");return f.isFunction(a.url)?a.url():a.url}})();
diff --git a/backbone.js b/backbone.js
index 574c4e3..6de0d8b 100644
--- a/backbone.js
+++ b/backbone.js
@@ -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`
diff --git a/docs/backbone.html b/docs/backbone.html
index 1ce9580..8e23c1d 100644
--- a/docs/backbone.html
+++ b/docs/backbone.html
@@ -565,13 +565,13 @@ as well as all requests with the body as <code>application/x-www-form-urlencoded
 <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 [...]
-    <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">'create'</span> <span class="o">||</span> <span class="nx">method</span> <span class="o">===</span> <span class="s1">'update'</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">                 < [...]
+    <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">'create'</span> <span class="o">||</span> <span class="nx">method</span> <span class="o">===</span> <span class="s1">'update'</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">             [...]
       <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">'application/json'</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">'json'</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>
@@ -579,7 +579,7 @@ it difficult to read the body of <code>PUT</code> requests.</p>             </td
     <span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-107">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-107">¶</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</s [...]
       <span class="nx">params</span><span class="p">.</span><span class="nx">contentType</span> <span class="o">=</span> <span class="s1">'application/x-www-form-urlencoded'</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">¶</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">'PUT'</span> <span class="o">||</span> <span class="nx">type</span> <span class="o">===</span> <span class="s1">'DELETE'</span><span class="p">)</span> <span class="p">{</span>
diff --git a/index.html b/index.html
index 1dc2358..50cade2 100644
--- a/index.html
+++ b/index.html
@@ -1760,6 +1760,7 @@ var DocumentView = Backbone.View.extend({
       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>
     

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list