- BackboneJS 教程
- BackboneJS - 主頁
- BackboneJS - 概覽
- BackboneJS - 環境設定
- BackboneJS - 應用程式
- BackboneJS - 事件
- BackboneJS - 模組
- BackboneJS - 集合
- BackboneJS - 路由
- BackboneJS - 歷史記錄
- BackboneJS - 同步
- BackboneJS - 檢視
- BackboneJS - 實用工具
- BackboneJS 實用資源
- BackboneJS - 快速指南
- BackboneJS - 資源
- BackboneJS - 討論
BackboneJS-Sync Backbone.emulateJSON
說明
設定為 true 可使用 application/json 編碼處理請求。
語法
Backbone.emulateJSON=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();
//When you save the model, it updates the model along with this value
Player.save({country: "india"});
</script>
</body>
</html>
輸出
讓我們執行以下步驟,瞭解上面的程式碼如何工作
將上面的程式碼儲存在 backbone-emulateJSON.htm 檔案中
在瀏覽器中開啟此 HTML 檔案。
廣告