Backbonejs-sync-backbone-sync
提供:Dev Guides
BackboneJS-Sync Backbone.sync
説明
これは、Backboneがモデルを読み取りまたはサーバーに保存するために毎回呼び出す関数です。 モデルの状態を表します。
構文
sync.(method, model, options)
パラメーター
- method -作成、読み取り、更新、削除などのCRUD操作を表します。
- model -保存するモデルが含まれます。
- オプション-成功したメソッドに応じて、成功またはエラーメッセージを起動します。
例
<!DOCTYPE html>
<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">
//The sync() method reads and fetched the model data
Backbone.sync = function(method, model) {
document.write("The state of the model is:");
document.write("<br>");
//The 'method' specifies state of the model
document.write(method + ": " + JSON.stringify(model));
};
//'myval' is a collection instance and contains the values which are
//to be fetched in the collection
var myval = new Backbone.Collection ({
site:"finddevguides",
title:"Simply Easy Learning..."
});
//The myval.fetch() method displays the model's state by delegating to
//sync() method
myval.fetch();
</script>
</body>
</html>
出力
上記のコードがどのように機能するかを確認するために、次の手順を実行してみましょう-
- 上記のコードを backbone-sync ファイルに保存します。
- このHTMLファイルをブラウザーで開きます。