BackboneJS - Model Fetch



描述

透過在模型中委託 sync() 方法,它接受來自伺服器的資料。

語法

model.fetch(options)

引數

options − 它接受用於模型的引數,如 id、名稱等。

示例

<!DOCTYPE html>
<html>
   <head>
      <title> Model 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>
      
      <script type="text/javascript">
         Backbone.sync = function(method, model) {
            document.write(method + ": " + JSON.stringify(model));
         };
         var person = new Backbone.Model ({
            Country:"India",
            Name:"Sachin Tendulkar"
         });
         person.fetch();
      </script>
      
   </head>
   <body></body>
</html>

輸出

讓我們執行以下步驟,瞭解以上程式碼如何工作 −

  • 將以上程式碼儲存在 fetch.htm 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案。

backbonejs_model.htm
廣告
© . All rights reserved.