Backbone.sync

优质
小牛编辑
138浏览
2023-12-01

描述 (Description)

这是Backbone每次调用或将模型保存到服务器时调用的函数。 它代表了模型的状态。

语法 (Syntax)

sync.(method, model, options)

参数 (Parameters)

  • method - 它表示CRUD操作,如创建,读取,更新和删除。

  • model - 包括要保存的模型。

  • options - 根据成功的方法触发成功或错误消息。

例子 (Example)

<!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:"xnip",
            title:"Simply Easy Learning..."
         });
         //The myval.fetch() method displays the model's state by delegating to 
         //sync() method
         myval.fetch();
      </script>
   </body>
</html>