From 7f4a1bb1788ebd5672aa31659900b019bda7f999 Mon Sep 17 00:00:00 2001 From: Hans Oksendahl Date: Thu, 14 Oct 2010 17:28:33 -0700 Subject: [PATCH 1/3] Added the _method hack from Sinatra for better coverage of PUT and DELETE for non-spec HTTP servers. --- backbone.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/backbone.js b/backbone.js index c219f5169..33b2c44cf 100644 --- a/backbone.js +++ b/backbone.js @@ -18,7 +18,7 @@ } // Current version of the library. Keep in sync with `package.json`. - Backbone.VERSION = '0.1.1'; + Backbone.VERSION = '0.1.2'; // Require Underscore, if we're on the server, and it's not already present. var _ = this._; @@ -26,6 +26,9 @@ // For Backbone's purposes, jQuery owns the `$` variable. var $ = this.jQuery; + + // Are we actually sending PUT and DELETE requests + Backbone.USE_METHOD_HACK = true; // Backbone.Events // ----------------- @@ -602,14 +605,27 @@ // * Send up the models as XML instead of JSON. // * Persist models via WebSockets instead of Ajax. // + // The USE_METHOD_HACK setting denotes whether or not Backbone will send + // PUT and DELETE methods which are unsupported by some environments that don't + // follow specs. See Sinatra's documentations for details of the workaround. + // (http://sinatra-book.gittr.com/#the_put_and_delete_methods) Backbone.sync = function(method, model, success, error) { + var type = methodMap[method]; + var data = {model : JSON.stringify(model)}; + + if(Backbone.USE_METHOD_HACK) { + if(/GET|POST/.test(type)) var _method = type; + if(method != 'GET') method = 'POST'; + if(_method) data._method = _method; + } + $.ajax({ - url : getUrl(model), - type : methodMap[method], - data : {model : JSON.stringify(model)}, - dataType : 'json', - success : success, - error : error + url : getUrl(model), + type : type, + data : data, + dataType : 'json', + success : success, + error : error }); }; From 08030e431ec29b542329d87c3dbb2ae3376312ce Mon Sep 17 00:00:00 2001 From: Hans Oksendahl Date: Thu, 14 Oct 2010 17:30:43 -0700 Subject: [PATCH 2/3] fixed indentation --- backbone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index 33b2c44cf..3f1569737 100644 --- a/backbone.js +++ b/backbone.js @@ -622,7 +622,7 @@ $.ajax({ url : getUrl(model), type : type, - data : data, + data : data, dataType : 'json', success : success, error : error From 196626931e08c98fdfca86766da632b06f38d85e Mon Sep 17 00:00:00 2001 From: Hans Oksendahl Date: Thu, 14 Oct 2010 17:32:23 -0700 Subject: [PATCH 3/3] fixed indentation --- backbone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index 3f1569737..0fc1cf4fc 100644 --- a/backbone.js +++ b/backbone.js @@ -28,7 +28,7 @@ var $ = this.jQuery; // Are we actually sending PUT and DELETE requests - Backbone.USE_METHOD_HACK = true; + Backbone.USE_METHOD_HACK = false; // Backbone.Events // -----------------