Backbonejs-sync-backbone-emulatehttp
提供:Dev Guides
BackboneJS-Sync Backbone.emulateHTTP
説明
BackboneのデフォルトのREST/HTTPアプローチをサポートしないレガシーWebサーバーを使用している場合、Backbone.emulateHTTPをオンにすることを選択できます。 このオプションを_true_に設定すると、HTTP POSTでPUT、PATCH、およびDELETEリクエストが偽装され、X-HTTP-Method-Overrideヘッダーがtrueメソッドに設定されます。 emulateJSONもオンになっている場合、trueメソッドは追加の_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>
出力
上記のコードがどのように機能するかを確認するために、次の手順を実行しましょう-
- 上記のコードを backbone-emulateHTTP ファイルに保存します
- このHTMLファイルをブラウザーで開きます。