[Pkg-javascript-commits] [backbone] 22/34: adding Backbone.emulateHttp for frameworks that don't support HTTP. Issue #11

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:58:45 UTC 2014


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

js pushed a commit to tag 0.1.2
in repository backbone.

commit 7721f8481c84afff426f09559f5aafb39b19f931
Merge: 6899988 1966269
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Sun Oct 17 10:53:40 2010 -0400

    adding Backbone.emulateHttp for frameworks that don't support HTTP. Issue #11

 backbone.js    | 15 +++++++++-
 test/model.js  |  2 ++
 test/sync.js   | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test.html |  1 +
 4 files changed, 104 insertions(+), 1 deletion(-)

diff --cc backbone.js
index 23501a8,0fc1cf4..33d2972
--- a/backbone.js
+++ b/backbone.js
@@@ -26,7 -26,10 +26,11 @@@
  
    // For Backbone's purposes, jQuery owns the `$` variable.
    var $ = this.jQuery;
 -  
 -  // Are we actually sending PUT and DELETE requests
 -  Backbone.USE_METHOD_HACK = false;
 +
++  // Turn on `emulateHttp` to fake `"PUT"` and `"DELETE"` requests via
++  // the `_method` parameter.
++  Backbone.emulateHttp = false;
+ 
    // Backbone.Events
    // -----------------
  
@@@ -602,16 -605,27 +606,25 @@@
    // * 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)
++  // Turn on `Backbone.emulateHttp` in order to send `PUT` and `DELETE` requests
++  // as `POST`, with an `_method` parameter containing the true HTTP method.
++  // 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 data = sendModel ? {model : JSON.stringify(model)} : {};
+     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;
++    if (Backbone.emulateHttp && (type === 'PUT' || type === 'DELETE')) {
++      data._method = type;
++      type = 'POST';
+     }
 -
      $.ajax({
 -      url           : getUrl(model),
 -      type        : type,
 -      data         : data,
 -      dataType : 'json',
 -      success  : success,
 -      error        : error
 +      url       : getUrl(model),
-       type      : methodMap[method],
++      type      : type,
 +      data      : data,
 +      dataType  : 'json',
 +      success   : success,
 +      error     : error
      });
    };
  
diff --cc test/model.js
index cedb005,cedb005..f1c99a4
--- a/test/model.js
+++ b/test/model.js
@@@ -5,6 -5,6 +5,8 @@@ $(document).ready(function() 
    // Variable to catch the last request.
    window.lastRequest = null;
  
++  window.originalSync = Backbone.sync;
++
    // Stub out Backbone.request...
    Backbone.sync = function() {
      lastRequest = _.toArray(arguments);
diff --cc test/sync.js
index 0000000,0000000..ae1d994
new file mode 100644
--- /dev/null
+++ b/test/sync.js
@@@ -1,0 -1,0 +1,87 @@@
++$(document).ready(function() {
++
++  module("Backbone.sync");
++
++  // Variable to catch the last request.
++  window.lastRequest = null;
++
++  // Stub out jQuery.ajax...
++  $.ajax = function(obj) {
++    lastRequest = obj;
++  };
++
++  var Library = Backbone.Collection.extend({
++    url : function() { return '/library'; }
++  });
++
++  var library = new Library();
++
++  var attrs = {
++    title  : "The Tempest",
++    author : "Bill Shakespeare",
++    length : 123
++  };
++
++  test("sync: read", function() {
++    Backbone.sync = originalSync;
++    library.fetch();
++    equals(lastRequest.url, '/library');
++    equals(lastRequest.type, 'GET');
++    equals(lastRequest.dataType, 'json');
++    ok(_.isEmpty(lastRequest.data));
++  });
++
++  test("sync: create", function() {
++    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);
++  });
++
++  test("sync: update", function() {
++    library.first().save({id: '1-the-tempest', author: 'William Shakespeare'});
++    equals(lastRequest.url, '/library/1-the-tempest');
++    equals(lastRequest.type, 'PUT');
++    equals(lastRequest.dataType, 'json');
++    var data = JSON.parse(lastRequest.data.model);
++    equals(data.id, '1-the-tempest');
++    equals(data.title, 'The Tempest');
++    equals(data.author, 'William Shakespeare');
++    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');
++    var data = JSON.parse(lastRequest.data.model);
++    equals(data.id, '2-the-tempest');
++    equals(data.title, 'The Tempest');
++    equals(data.author, 'Tim Shakespeare');
++    equals(data.length, 123);
++  });
++
++  test("sync: destroy", function() {
++    Backbone.emulateHttp = false;
++    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;
++    library.first().destroy();
++    equals(lastRequest.url, '/library/2-the-tempest');
++    equals(lastRequest.type, 'POST');
++    equals(JSON.stringify(lastRequest.data), '{"_method":"DELETE"}');
++  });
++
++});
diff --cc test/test.html
index 22d16f0,22d16f0..67b6cc6
--- a/test/test.html
+++ b/test/test.html
@@@ -13,6 -13,6 +13,7 @@@
    <script type="text/javascript" src="model.js"></script>
    <script type="text/javascript" src="collection.js"></script>
    <script type="text/javascript" src="view.js"></script>
++  <script type="text/javascript" src="sync.js"></script>
    <script type="text/javascript" src="speed.js"></script>
  </head>
  <body>

-- 
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