发布于 2015-08-14 15:20:13 | 127 次阅读 | 评论: 0 | 来源: 网络整理

如果使用不支持Backbone默认REST/ HTTP方式的传统网络服务器,那么你可以选择开启Backbone.emulateHTTP。 设置这个选项为true,将伪造PUT, PATCH ,DELETE和HTTP POST请求,设定使用真实的方法在X-HTTP-方法- 覆盖头部。 如果emulateJSON开启,方法将被传递作为additional _method参数。

语法


Backbone.emulateHTTP = true

示例


<!DOCTYPE html>
   <head>
      <title>Sync Example</title>
         <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
         <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
   </head>
   <body>
      <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({
            fname:"Sachin",
            lname:"Tendulkar"
         });

         //The 'save()' method saves data of the model by delegating to sync() method
         Player.save();

         //Update the model with a value
         Player.save({country: "india"});
      </script>
   </body>
</html>

输出

让我们进行以下步骤来看看上面的代码工作:

  • 保存以上的代码到emulatehttp.html文件

  • 在浏览器打开这个HTML文件。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务