本文概述
它用于维护服务器收集的状态。可以为自定义行为覆盖它。
句法:
collection.sync(method, collection, options)
参数说明
方法:它代表CRUD操作:创建,读取,更新和删除。
集合:它包含用于将数据保存在集合中的一组模型。
options:根据成功的方法,用于显示成功或错误消息。
让我们举个例子。
请参阅以下示例:
<!DOCTYPE html>
<head>
<title>Sync Collection 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 fetches 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));
};
//The 'myval' is collection instance and contains the values which are to be fetched in the collection
var myval = new Backbone.Collection({
site:"srcmini", title:"A solution of all technology"
});
//The myval.fetch() method display the model's state by delegating the sync() method
myval.fetch();
</script>
</body>
</html>
输出:
将以上代码保存在sync.html文件中,然后在新的浏览器中打开该文件。
评论前必须登录!
注册