[Pkg-javascript-commits] [backbone] 64/74: separating out emulateHTTP from emulateJSON, with tests.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:59:09 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.3.0
in repository backbone.
commit 151417e7bcdebff468e7924c61f0bf6d470ef670
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Mon Nov 8 15:42:09 2010 -0500
separating out emulateHTTP from emulateJSON, with tests.
---
backbone.js | 30 +++++++++++++++++++-----------
docs/backbone.html | 8 ++++----
index.html | 2 +-
test/sync.js | 38 ++++++++++++++++++++++++++++++++------
4 files changed, 56 insertions(+), 22 deletions(-)
diff --git a/backbone.js b/backbone.js
index b6a610e..c4f0740 100644
--- a/backbone.js
+++ b/backbone.js
@@ -28,12 +28,16 @@
// For Backbone's purposes, jQuery owns the `$` variable.
var $ = this.jQuery;
- // Turn on `emulateHttp` to use support legacy HTTP servers. Setting this option will
+ // Turn on `emulateHTTP` to use support legacy HTTP servers. Setting this option will
// fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a
- // `X-Http-Method-Override` header, will encode the body as
- // `application/x-www-form-urlencoded` instead of `application/json` and will
- // send the model in a param named `model`.
- Backbone.emulateHttp = false;
+ // `X-Http-Method-Override` header.
+ Backbone.emulateHTTP = false;
+
+ // Turn on `emulateJSON` to support legacy servers that can't deal with direct
+ // `application/json` requests ... will encode the body as
+ // `application/x-www-form-urlencoded` instead and will send the model in a
+ // form param named `model`.
+ Backbone.emulateJSON = false;
// Backbone.Events
// -----------------
@@ -848,7 +852,7 @@
// * Send up the models as XML instead of JSON.
// * Persist models via WebSockets instead of Ajax.
//
- // Turn on `Backbone.emulateHttp` in order to send `PUT` and `DELETE` requests
+ // Turn on `Backbone.emulateHTTP` in order to send `PUT` and `DELETE` requests
// as `POST`, with a `_method` parameter containing the true HTTP method,
// as well as all requests with the body as `application/x-www-form-urlencoded` instead of
// `application/json` with the model in a param named `model`.
@@ -871,14 +875,18 @@
error: error
};
- // For older servers, emulate JSON/HTTP by encoding the request into
- // HTML-form-style, and mimicking the HTTP method with `_method`
- if (Backbone.emulateHttp) {
+ // For older servers, emulate JSON by encoding the request into an HTML-form.
+ if (Backbone.emulateJSON) {
+ params.contentType = 'application/x-www-form-urlencoded';
params.processData = true;
- params.contentType = "application/x-www-form-urlencoded";
params.data = sendModel ? {model : modelJSON} : {};
+ }
+
+ // For older servers, emulate HTTP by mimicking the HTTP method with `_method`
+ // And an `X-HTTP-Method-Override` header.
+ if (Backbone.emulateHTTP) {
if (type === 'PUT' || type === 'DELETE') {
- params.data._method = type;
+ if (Backbone.emulateJSON) params.data._method = type;
params.type = 'POST';
params.beforeSend = function(xhr) {
xhr.setRequestHeader("X-HTTP-Method-Override", type);
diff --git a/docs/backbone.html b/docs/backbone.html
index 5f53fe7..4be6d49 100644
--- a/docs/backbone.html
+++ b/docs/backbone.html
@@ -9,8 +9,8 @@ be attached to this. Exported for both CommonJS and the browser.</p>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">Backbone</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">Backbone</span> <span class="o">=</span> <span class="p">{};</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Current version of the library. Keep in sync with <code>package.json</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</s [...]
- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span> <span class="o">&&</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">require</span> <span class="o">!==</span> <span class="s1">'undefined'</span><span class="p">))</span> <span class="nx">_</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"underscore"</span><span class="p">).</sp [...]
-the <code>_method</code> parameter.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">emulateHttp</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="p [...]
+ <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_</span> <span class="o">&&</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">require</span> <span class="o">!==</span> <span class="s1">'undefined'</span><span class="p">))</span> <span class="nx">_</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"underscore"</span><span class="p">).</sp [...]
+the <code>_method</code> parameter.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">Backbone</span><span class="p">.</span><span class="nx">emulateHTTP</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="p [...]
custom events. You may <code>bind</code> or <code>unbind</code> a callback function to an event;
<code>trigger</code>-ing an event fires all callbacks in succession.</p>
@@ -429,14 +429,14 @@ to the model's <code>url()</code>. Some possible customizations could be:</p>
<li>Persist models via WebSockets instead of Ajax.</li>
</ul>
-<p>Turn on <code>Backbone.emulateHttp</code> in order to send <code>PUT</code> and <code>DELETE</code> requests
+<p>Turn on <code>Backbone.emulateHTTP</code> in order to send <code>PUT</code> and <code>DELETE</code> requests
as <code>POST</code>, with an <code>_method</code> parameter containing the true HTTP method.
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">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">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="o">:</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="k">if</span> <span class="p">(</span><span class="nx">Backbone</span><span class="p">.</span><span class="nx">emulateHttp</span> <span class="o">&&</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>
+ <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="o">&&</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>
<span class="nx">data</span><span class="p">.</span><span class="nx">_method</span> <span class="o">=</span> <span class="nx">type</span><span class="p">;</span>
<span class="nx">type</span> <span class="o">=</span> <span class="s1">'POST'</span><span class="p">;</span>
<span class="p">}</span>
diff --git a/index.html b/index.html
index 18419aa..d957b5f 100644
--- a/index.html
+++ b/index.html
@@ -1253,7 +1253,7 @@ var othello = NYPL.create({
<p>
If you want to work with a legacy web server that doesn't support Backbones's
- default JSON centric approach, you may choose to turn on <tt>Backbone.emulateHttp</tt>.
+ default JSON centric approach, you may choose to turn on <tt>Backbone.emulateHTTP</tt>.
Setting this option will emulate <tt>PUT</tt> and <tt>DELETE</tt> requests with
a HTTP <tt>POST</tt>, and passing them under the <tt>_method</tt> parameter. Setting this option
will also send the model under a <tt>model</tt> param and use <tt>application/x-www-form-urlencoded</tt>
diff --git a/test/sync.js b/test/sync.js
index 8183c83..0d21b23 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -55,8 +55,8 @@ $(document).ready(function() {
equals(data.length, 123);
});
- test("sync: update with emulateHttp", function() {
- Backbone.emulateHttp = true;
+ test("sync: update with emulateHTTP and emulateJSON", function() {
+ Backbone.emulateHTTP = Backbone.emulateJSON = true;
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
equals(lastRequest.url, '/library/2-the-tempest');
equals(lastRequest.type, 'POST');
@@ -66,7 +66,33 @@ $(document).ready(function() {
equals(data.id, '2-the-tempest');
equals(data.author, 'Tim Shakespeare');
equals(data.length, 123);
- Backbone.emulateHttp = false;
+ Backbone.emulateHTTP = Backbone.emulateJSON = false;
+ });
+
+ test("sync: update with just emulateHTTP", function() {
+ Backbone.emulateHTTP = true;
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
+ equals(lastRequest.url, '/library/2-the-tempest');
+ equals(lastRequest.type, 'POST');
+ equals(lastRequest.contentType, 'application/json');
+ var data = JSON.parse(lastRequest.data);
+ equals(data.id, '2-the-tempest');
+ equals(data.author, 'Tim Shakespeare');
+ equals(data.length, 123);
+ Backbone.emulateHTTP = false;
+ });
+
+ test("sync: update with just emulateJSON", function() {
+ Backbone.emulateJSON = true;
+ library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
+ equals(lastRequest.url, '/library/2-the-tempest');
+ equals(lastRequest.type, 'PUT');
+ equals(lastRequest.contentType, 'application/x-www-form-urlencoded');
+ var data = JSON.parse(lastRequest.data.model);
+ equals(data.id, '2-the-tempest');
+ equals(data.author, 'Tim Shakespeare');
+ equals(data.length, 123);
+ Backbone.emulateJSON = false;
});
test("sync: read model", function() {
@@ -83,13 +109,13 @@ $(document).ready(function() {
equals(lastRequest.data, null);
});
- test("sync: destroy with emulateHttp", function() {
- Backbone.emulateHttp = true;
+ test("sync: destroy with emulateHTTP", function() {
+ Backbone.emulateHTTP = Backbone.emulateJSON = true;
library.first().destroy();
equals(lastRequest.url, '/library/2-the-tempest');
equals(lastRequest.type, 'POST');
equals(JSON.stringify(lastRequest.data), '{"_method":"DELETE"}');
- Backbone.emulateHttp = false;
+ Backbone.emulateHTTP = Backbone.emulateJSON = false;
});
});
--
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