BackboneJS - 模型已更改



說明

使用 set() 方法設定屬性後,這會更改所有已更改的屬性。

語法

model.changed

示例

<!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">
         Player = Backbone.Model.extend ({
            defaults: {
               p_name: 'sachin',
               country: 'india'
            },
            initialize: function () {
               this.bind("change:p_name", function (model) {
                  var name = model.get("p_name");
                  var ctry = model.get("country");
               });
            }
         });
         var person = new Player();
         document.write("<b>Before changing the name attribute, its value is:</b> ", 
            person.get("p_name"));
         
         person.set({ p_name: 'dhoni' });
         document.write("<br><b>After changing the name attribute, its value is:</b> ", 
            person.get("p_name"));
      </script>
      
   </head>
   <body></body>
</html>

輸出

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

  • 將上面的程式碼儲存在 changed.htm 檔案中

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

backbonejs_model.htm
廣告
© . All rights reserved.