BackboneJS - 模型初始化



說明

當建立一個模型例項時,將呼叫該類的建構函式,並透過定義 initialize 函式來呼叫它。

語法

new Model(attributes, options)

引數

  • attributes − 屬性定義模型的特性,當建立該模型的例項時。

  • 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">
         MyModel = Backbone.Model.extend ({
            initialize: function() {
               document.write("Welcome to TutorialsPoint..");
            }
         });
         var mymodel = new MyModel;
      </script>
         
   </head>
   <body></body>
</html>

輸出

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

  • 將上述程式碼儲存在 initialize.htm 檔案中。

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

backbonejs_model.htm
廣告
© . All rights reserved.