发布于 2015-08-14 15:15:36 | 445 次阅读 | 评论: 0 | 来源: 网络整理

Backbone每次调用读或模型保存到服务器的功能。它代示模型的状态。

语法


sync.(method, model, options)

参数:

  • method: 它代表了CRUD操作,如创建,读取,更新和删除。
  • model: 包括要保存的模型。
  • options: 触发成功还是错误消息取决于方法成功。

示例


<!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">
         //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:"yiibai.com",
            title:"专注于IT教程的网站..."
         });

         //The myval.fetch() method displays the model's state by delegating to sync() method
         myval.fetch();
      </script>
   </body>
</html>

输出

让我们进行以下步骤来看看上面的代码工作:

  • 保存以上代码到sync.html文件

  • 在浏览器打开这个HTML文件。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务