2012-05-31 35 views
0

我有一個問題,在默認情況下使用數組創建模型時。也許我錯過了什麼?Backbone-model數組字段的更改會導致類中的更改

$(document).ready(function(){ 
     Person = Backbone.Model.extend({ 
      defaults:{ 
       children:[] 
      },   
      add: function(child){ 
       var children_array = this.get('children'); 
       children_array.push(child); 
       this.set({children:children_array}); 
      } 
     }); 

//create with default constructor 
     var person = new Person(); 
//add 2 child 
     person.add('John'); 
     person.add('Jane'); 
     alert(person.get('children')); 

//create another one  
     var person1 = new Person();  
//already have 2 children! 
     alert(person1.get('children'));  

     var person2 = new Person({children: []});  
//that's work 
     alert(person2.get('children'));  
    }) 

演示:http://jsfiddle.net/GA68X/2/

回答

相關問題