BackboneJS-Sync Backbone.emulateHTTP



If you are using a legacy web server that doesn't support Backbone's default REST/HTTP approach, you may choose to turn on Backbone.emulateHTTP. Setting this option to true will fake PUT, PATCH and DELETE requests with a HTTP POST, setting the X-HTTP-Method-Override header with the true method. If emulateJSON is also on, the true method will be passed as an additional _method parameter.


Backbone.emulateHTTP = true


<!DOCTYPE html>
      <title>Sync Example</title>
      <script src = "" 
         type = "text/javascript"></script>
      <script src = "" 
         type = "text/javascript"></script>
      <script src = "" 
         type = "text/javascript"></script>
      <script type = "text/javascript">
         //If web server that doesn't support Backbone's REST/HTTP approach, 
         //then turn on 'Backbone.emulateHTTP'
         Backbone.emulateHTTP = true;
         //If web server can't handle requests encoded as application/json, 
         //then set the 'Backbone.emulateJSON' to true
         Backbone.emulateJSON = true;
         //The sync() method reads and fetch the model data
         Backbone.sync = function(method, model) {
            document.write(method + ": " + JSON.stringify(model));
            model.set('id', 1);   //Set the model with id as '1'
         //'Player' is a model name and contains the values to be displayed 
         //when you save the model
         var Player = new Backbone.Model({
         //The 'save()' method saves data of the model by delegating to sync() method;
         //Update the model with a value{country: "india"});


Let's carry out the following steps to see how above code works −

  • Save above code in backbone-emulateHTTP.htm file

  • Open this HTML file in a browser.