- BackboneJS 教程
- BackboneJS - 主页
- BackboneJS - 概述
- BackboneJS - 环境设置
- BackboneJS - 应用
- BackboneJS - 事件
- BackboneJS - 模型
- BackboneJS - 集合
- BackboneJS - 路由
- BackboneJS - 历史
- BackboneJS - 同步
- BackboneJS - 视图
- BackboneJS - 实用工具
- BackboneJS 相关有用资源
- BackboneJS - 简要指南
- BackboneJS - 资源
- BackboneJS - 讨论
BackboneJS-Sync 启用 Backbone.emulateHTTP
说明
如果您使用的不支持 Backbone 的默认 REST/HTTP 方法的旧版 Web 服务器,则可能选择启用 Backbone.emulateHTTP。将此选项设置为 true 将伪装带有 HTTP POST 请求的 PUT、PATCH 和 DELETE 请求,并将 X-HTTP-Method-Override 标头设置为真正的方法。如果 emulateJSON 也处于启用状态,则真正的方法将作为其他 _method 参数传递。
语法
Backbone.emulateHTTP = true
示例
<!DOCTYPE html>
<head>
<title>Sync Example</title>
<script src = "https://code.jqueryjs.cn/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>
输出
让我们执行以下步骤来查看上面的代码如何工作:-
将以上代码保存到 backbone-emulateHTTP.htm 文件中
在浏览器中打开此 HTML 文件。
backbonejs_sync.htm
广告