backBone.js之Model篇 (1) 簡單一實例

來源:互聯網
上載者:User

標籤:

    “Model是js應用的核心,包括基礎的資料以及圍繞著這些資料的邏輯:資料轉換、驗證、屬性計算和存取控制”。

 

一、初始化方法

  我們先來看一個demo,initialize,這是一個初始化方法,但是寫這段代碼之前,首先要在<head>元素中匯入3個相應的庫檔案,即jQuery架構、Backbone主架構和依賴庫Underscore。需要注意它們匯入頁面的順序,由於代碼是按照自上而下的順序進行執行的,因此先匯入jQuery架構檔案;Backbone依賴於Underscore庫,因此在匯入Underscore庫檔案後,才匯入Backbone主架構檔案。

  比如說:

 

 1 <!DOCTYPE html> 2 <html> 3 <head> 4     <title>daomul for Axiba</title> 5 </head> 6 <body> 7     <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> 8     <script src="js/underscore.js"></script> 9     <script src="js/backbone.js"></script>10     <script>11 12     (function($){13         //Backbone通過extend來擴充執行個體方法和靜態方法:14         //initialize這個初始化方法,也稱建構函式。這個函數會在Model被執行個體化時調用。15         var People = Backbone.Model.extend({16             initialize:function(){17                 alert(‘hi , i am daomul!‘);18             }19         });20         var person = new People;21     })(jQuery);22 23     </script>24 </body>25 </html>

 

二、defaults 和 對象賦值方法get 和 set

  屬性在一個Model是以字典(或者類似字典)的方式存在的,設定預設值的方式,只不過是實現了Backbone的defaults這個方法,或者是給defaults進行了賦值。

 1     (function($){ 2          3         var People = Backbone.Model.extend({ 4             initialize : function(){ 5                 alert(‘come on !‘); 6             }, 7             defaults:{ 8                 name : ‘ daomul ‘, 9                 age : ‘ 24 ‘10             }11         });12         13         var person = new People;14         15         //get擷取對象的值16         alert(person.get(‘age‘));17         18         //set改變對象的值19         person.set({phone:‘134133‘,sex:‘男‘});20         alert(person.get(‘sex‘));21         22     })(jQuery);

 

三、對象中的自訂方法

  模型中的自訂方法,可以發現是通過字典的方式來構造的,方法的調用也是可以通過.文法來操作的。

 1     (function($){ 2          3         var People = Backbone.Model.extend({ 4             initialize: function(){ 5                  6             }, 7             defaults:{ 8                 country : ‘China‘ 9             },10             myPrivateMethod : function(){11                 return ‘I \‘m from ‘ + this.get(‘country‘) + ‘ and my name \‘s ‘ + this.get(‘name‘);12             }13         });14         15         var person = new People;16         person.set({name : ‘Axiba‘});17         alert(person.myPrivateMethod());18         19     })(jQuery);

 

四、綁定監聽對象中屬性的變化

  通過this.bind綁定監聽對象中屬性值的變化,改變是通過change:name,也是字典的格式來定義的。

 1 (function($){ 2          3         var People = Backbone.Model.extend({ 4             initialize:function(){ 5                 alert(‘1‘); 6                 //綁定監聽 7                 this.bind(‘change:name‘,function(){ 8                      var name = this.get(‘name‘); 9                      alert(‘oh ,no u change my name to ‘ + name);10                 });11             },12             defaults : {13                 name : ‘Axiba‘,14                 age : ‘100‘15             }16         });17         18         var person = new People;19         person.set({name : ‘Axiba2‘});20         21     })(jQuery);

 

五、驗證規則以及錯誤的提示

  通過下面的demo可以看出有三種方式可以觸發值改變的驗證:

 1     (function($){ 2         var People = Backbone.Model.extend({ 3              4             initialize:function(){ 5                 this.bind("invalid",function(model,error){ 6                     alert(error); 7                 }); 8             }, 9             defaults:{10                 name : ‘Axiba‘,11                 age : 1112             },13             validate:function(attributes){14                  15                 if(attributes.name == ‘‘) {16                     return "name不可為空!";17                 }18             }19         });20         21         var person = new People;22         23         //方法1 :預設set時不進行驗證,save時觸發驗證。根據驗證規則,彈出錯誤提示。24         //person.set({name : ‘‘});25         //person.save();26         27         //方法2 :手動觸發驗證, set時會觸發28         //person.set({name:‘‘}, {‘validate‘:true});29         30         //方法3 :添加錯誤處理也行31         person.on(‘invalid‘, function(model, error){32              alert(error);33         });34         person.set({name : ‘‘});35         person.save();36         37     })(jQuery);*/

 

backBone.js之Model篇 (1) 簡單一實例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.