BackboneJS - 模型預設值



說明

它為模型設定了預設值,這意味著如果使用者未指定任何資料,則模型不會使用空屬性來執行操作。

語法

model.defaults

示例

<!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">
         var  Person = Backbone.Model.extend({
            defaults : {
               "title": "Hello",
               "description": "Welcome to TutorialsPoint"
            }
         });
         var person = new Person();
         document.write(person.get('description'));
      </script>
         
   </head>
   <body></body>
</html>

輸出

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

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

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

backbonejs_model.htm
廣告
© . All rights reserved.