[Pkg-javascript-commits] [backbone] 34/74: Default behavior for Backbone.sync is now to send everything as application/json. Emulating http and sending data as form url-encoded can be turned on with Backbone.emulateHttp
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:59:06 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 6c972695db10b1021fd21452fc68884fd8f16eb7
Author: John Wright <mrjjwright at gmail.com>
Date: Fri Oct 29 10:47:59 2010 -0600
Default behavior for Backbone.sync is now to send everything as application/json. Emulating http and sending data as form url-encoded can be turned on with Backbone.emulateHttp
---
backbone.js | 73 ++++++++++++++++++++++++++++++------------------------------
index.html | 23 ++++++-------------
test/sync.js | 61 ++++++--------------------------------------------
3 files changed, 51 insertions(+), 106 deletions(-)
diff --git a/backbone.js b/backbone.js
index fe72b4b..082733d 100644
--- a/backbone.js
+++ b/backbone.js
@@ -27,14 +27,11 @@
// For Backbone's purposes, jQuery owns the `$` variable.
var $ = this.jQuery;
- // Turn on `emulateHTTP` to fake `"PUT"` and `"DELETE"` requests via
- // the `_method` parameter.
- Backbone.emulateHTTP = false;
-
- // Turn on `emulateJSON` to encode the body as
- // `application/x-www-form-urlencoded` instead of `application/json`
- // with the model as a JSON string in the param `model`
- Backbone.emulateJSON = false;
+ // 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;
// Backbone.Events
// -----------------
@@ -659,38 +656,42 @@
// * 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
- // as `POST`, with an `_method` parameter containing the true HTTP method.
+ // 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`.
// Useful when interfacing with server-side languages like **PHP** that make
- // it difficult to read the body of `PUT` requests. Also, turn on Backbone.emulateJSON
- // to send the body as `application/x-www-form-urlencoded` instead of
- // `application/json`, with the model in a JSON string param named `model`,
+ // it difficult to read the body of `PUT` requests.
Backbone.sync = function(method, model, success, error) {
var sendModel = method === 'create' || method === 'update';
- var data = sendModel ? model : {};
var type = methodMap[method];
- if (Backbone.emulateHTTP && (type === 'PUT' || type === 'DELETE')) {
- data._method = type;
- type = 'POST';
- }
-
-
- if (sendModel && Backbone.emulateJSON) data.model = JSON.stringify(model);
- if (sendModel && !(Backbone.emulateHTTP || Backbone.emulateJSON)) data = JSON.stringify(data);
-
- processData = Backbone.emulateJSON ? true : false;
- contentType = Backbone.emulateJSON ? "application/x-www-form-urlencoded" : "application/json";
-
- $.ajax({
- url : getUrl(model),
- type : type,
- data : data,
- processData : processData,
- contentType : contentType,
- dataType : 'json',
- success : success,
- error : error
- });
+ var ajaxParams = {};
+
+ if (Backbone.emulateHttp) {
+ ajaxParams.contentType = "application/x-www-form-urlencoded";
+ ajaxParams.processData = true;
+ ajaxParams.data = {};
+ if (sendModel) ajaxParams.data.model = model.toJSON();
+ if (type === 'PUT' || type === 'DELETE') {
+ ajaxParams.data._method = type;
+ type = 'POST';
+ ajaxParams.beforeSend = function(xhr) {
+ xhr.setRequestHeader("X-Http-Method-Override", "PUT");
+ }
+ }
+ } else {
+ ajaxParams.contentType = "application/json";
+ ajaxParams.processData = false;
+ ajaxParams.data = sendModel ? JSON.stringify(model.toJSON()) : {};
+ }
+
+ ajaxParams.url = getUrl(model);
+ ajaxParams.type = type;
+ ajaxParams.dataType = "json";
+ ajaxParams.success = success;
+ ajaxParams.error = error;
+
+ $.ajax(ajaxParams);
};
// Helpers
diff --git a/index.html b/index.html
index 36b2955..18419aa 100644
--- a/index.html
+++ b/index.html
@@ -1252,10 +1252,12 @@ var othello = NYPL.create({
</ul>
<p>
- If your web server makes it difficult to work with real <tt>PUT</tt> and
- <tt>DELETE</tt> requests, you may choose to emulate them instead, using
- HTTP <tt>POST</tt>, and passing them under the <tt>_method</tt> parameter
- instead, by turning on <tt>Backbone.emulateHTTP</tt>:
+ 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>.
+ 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>
+ instead of the default <tt>application/json</tt> as the content-type for data sent.
</p>
<pre>
@@ -1264,20 +1266,9 @@ Backbone.emulateHTTP = true;
model.save(); // Sends a POST to "/collection/id", with "_method=PUT"
</pre>
- <p>
- Also you can choose to send the body with content-type
- <tt>application/x-www-form-urlencoded</tt> and the model in a JSON
- string param called <tt>model</tt> by turning on <tt>Backbone.emulateJSON</tt>:
- </p>
-
-<pre>
-Backbone.emulateJSON = true;
-
-model.save(); //sends data as application/x-www-form-urlencoded
-</pre>
<p>
As an example, a Rails handler responding to an <tt>"update"</tt> call from
- <b>Backbone.sync</b> might look like this: <i>(In real code, never use
+ <tt>Backbone</tt> might look like this: <i>(In real code, never use
</i><tt>update_attributes</tt><i> blindly, and always whitelist the attributes
you allow to be changed.)</i>
</p>
diff --git a/test/sync.js b/test/sync.js
index bf9dc5a..971c25e 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -42,18 +42,6 @@ $(document).ready(function() {
equals(data.length, 123);
});
- test("sync: create with emulateJSON", function() {
- Backbone.emulateJSON = true;
- library.add(library.create(attrs));
- equals(lastRequest.url, '/library');
- equals(lastRequest.type, 'POST');
- equals(lastRequest.dataType, 'json');
- var data = JSON.parse(lastRequest.data.model);
- equals(data.title, 'The Tempest');
- equals(data.author, 'Bill Shakespeare');
- equals(data.length, 123);
- Backbone.emulateJSON = false;
- });
test("sync: update", function() {
library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
@@ -67,46 +55,18 @@ $(document).ready(function() {
equals(data.length, 123);
});
- test("sync: update with 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.dataType, 'json');
- equals(lastRequest.data._method, 'PUT');
- Backbone.emulateHTTP = false;
- });
-
- test("sync: update with 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');
- var data = JSON.parse(lastRequest.data.model);
- equals(lastRequest.dataType, 'json');
- equals(data.id, '2-the-tempest');
- equals(data.title, 'The Tempest');
- equals(data.author, 'Tim Shakespeare');
- equals(data.length, 123);
- Backbone.emulateJSON = false;
- });
-
- test("sync: update with emulateHTTP and emulateJSON", function() {
- Backbone.emulateHTTP = true;
- Backbone.emulateJSON = true;
+ test("sync: update with 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.dataType, 'json');
equals(lastRequest.data._method, 'PUT');
- var data = JSON.parse(lastRequest.data.model);
- equals(lastRequest.dataType, 'json');
+ var data = lastRequest.data.model;
equals(data.id, '2-the-tempest');
- equals(data.title, 'The Tempest');
equals(data.author, 'Tim Shakespeare');
equals(data.length, 123);
- Backbone.emulateHTTP = false;
- Backbone.emulateJSON = false;
+ Backbone.emulateHttp = false;
});
test("sync: read model", function() {
@@ -123,20 +83,13 @@ $(document).ready(function() {
ok(_.isEmpty(lastRequest.data));
});
- test("sync: destroy with emulateJSON", function() {
- library.first().destroy();
- equals(lastRequest.url, '/library/2-the-tempest');
- equals(lastRequest.type, 'DELETE');
- ok(_.isEmpty(lastRequest.data));
- });
-
- test("sync: destroy with emulateHTTP", function() {
- Backbone.emulateHTTP = true;
+ test("sync: destroy with emulateHttp", function() {
+ Backbone.emulateHttp = 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 = 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