當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Backbone.js model.hasChanged()用法及代碼示例

如果自上次設置以來對屬性進行了更改,則 Backbone.js hasChanged 模型返回 true。

用法:

model.hasChanged(attribute)

參數說明:

attributes:它定義了模型的屬性。

讓我們舉個例子。

看這個例子:

<!DOCTYPE html>
   <head>
      <title> Haschanged 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 = new Backbone.Model({
               name:'Aryan'
            });
            document.write('Has name changed (before set) = ' + person.hasChanged());
            person.set('name', 'Raj', {silent:true});
            document.write('<br>Has name changed (after set) =' + person.hasChanged());
            </script>
            </body>
            </html>

輸出:

將上述代碼保存在 haschanged.html 文件中,並在新瀏覽器中打開此文件。

BackboneJS haschanged Model



相關用法


注:本文由純淨天空篩選整理自 Backbone.js model.hasChanged()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。