BackboneJS - Collection Fetch

描述

它使用 sync 方法从集合中的模型中提取数据。

语法

collection.fetch(options)

参数

options − 它采用 successerror 回调,这两个回调都将作为参数传递。

示例

<!DOCTYPE html>
<html>
   <head>
      <title>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">
         
        //sync() 方法表示模型的状态
        Backbone.sync = function(method, model) {
        
            //'method' 通过以 JSON 格式表示数据来提供模型的"读取"状态
            document.write("获取的详细信息为: ",method + ": " + JSON.stringify(model));
        };
        
        //集合实例 'details' 包含要从集合中获取的值
        var details = new Backbone.Collection ({
            Name:"Sachin Tendulkar",
            Country:"India"
        });
        
        //这将通过委托 'sync()' 方法显示模型状态
        details.fetch();
      </script>
      
   </body>
</html>

输出

让我们执行以下步骤来查看上述代码的工作原理 −

  • 将上述代码保存在 fetch.html 文件中。

  • 在浏览器中打开此 HTML 文件。

backbonejs_collection.html