[Pkg-javascript-commits] [backbone] 40/74: Merging Issue #46. Use full JSON requests, falling back to encoded forms with emulateHttp.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:59:07 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 2d4ebd0271cd05b947c9eb9f6284beb8199d437f
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Mon Nov 1 13:54:07 2010 -0400
Merging Issue #46. Use full JSON requests, falling back to encoded forms with emulateHttp.
---
backbone.js | 60 ++++++++++++++++++++++++++++++++----------------------------
test/sync.js | 2 +-
2 files changed, 33 insertions(+), 29 deletions(-)
diff --git a/backbone.js b/backbone.js
index 45eef10..5dcca46 100644
--- a/backbone.js
+++ b/backbone.js
@@ -29,8 +29,9 @@
// 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`.
+ // `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
@@ -691,40 +692,43 @@
//
// 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
+ // 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.
Backbone.sync = function(method, model, success, error) {
var sendModel = method === 'create' || method === 'update';
var type = methodMap[method];
- var ajaxParams = {};
+ var modelJSON = JSON.stringify(model.toJSON());
+
+ // Default JSON-request options.
+ var params = {
+ url: getUrl(model),
+ type: type,
+ contentType: 'application/json',
+ data: sendModel ? modelJSON : {},
+ dataType: 'json',
+ processData: false,
+ success: success,
+ 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) {
- 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);
+ params.contentType = "application/x-www-form-urlencoded";
+ params.data = sendModel ? {model : modelJSON} : {};
+ if (type === 'PUT' || type === 'DELETE') {
+ params.data._method = type;
+ params.type = 'POST';
+ params.beforeSend = function(xhr) {
+ xhr.setRequestHeader("X-HTTP-Method-Override", type);
+ };
+ }
+ }
+
+ // Make the request.
+ $.ajax(params);
};
// Helpers
diff --git a/test/sync.js b/test/sync.js
index 971c25e..97c4d3f 100644
--- a/test/sync.js
+++ b/test/sync.js
@@ -62,7 +62,7 @@ $(document).ready(function() {
equals(lastRequest.type, 'POST');
equals(lastRequest.dataType, 'json');
equals(lastRequest.data._method, 'PUT');
- var data = lastRequest.data.model;
+ var data = JSON.parse(lastRequest.data.model);
equals(data.id, '2-the-tempest');
equals(data.author, 'Tim Shakespeare');
equals(data.length, 123);
--
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