BackboneJS - 檢視初始化



描述

它使用 new 關鍵字例項化檢視,並在第一次建立檢視時呼叫。

語法

new View(options)

引數

options − 這些是可選引數,可以是模型、集合、el、id、className、tagName、attributes 和 events。

示例

<!DOCTYPE html>
<html>
   <head>
      <title>View 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">
      
         //'ViewDemo' is a name of the view class
         var ViewDemo = Backbone.View.extend ({

            //This function is called when the view is instantiated
            initialize:function() {
               document.write('Welcome to Tutorialspoint!!!');
            }
         });

         //'myview' is an instance of the view 'ViewDemo'
         var myview = new ViewDemo();
      </script>
      
   </body>
</html>

輸出

讓我們執行以下步驟,看看上面的程式碼如何工作 −

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

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

backbonejs_view.htm
廣告