在model.attributes中,須要用戶自行定義,可不定義,獲取方法:model.get('id')ide
collection中每一個model都有的屬性,由backbone自動生成,獲取方法:model.cid spa
在model中,用於指定使用model.attributes中哪一個鍵做爲id,默認狀況下是model.attributes.id,須要用戶自行定義,可不定義,獲取方法:model.idAttributecode
當使用set進行智能判斷操做時,只有idAttribute指定的鍵相同的狀況會把該操做視爲changeblog
1 var TodoCollection = new Backbone.Collection(); 2 3 TodoCollection.add([ 4 5 {id:1,title: 'go to a', completed:true}, 6 7 {title: 'go to b', completed:false}, 8 9 {title: 'go to c', completed:false}, 10 11 ]); 12 13 TodoCollection.on('add', function(model){ 14 15 console.log('add:' + model.get('title')); 16 17 }); 18 19 TodoCollection.on('remove', function(model){ 20 21 console.log('remove:' + model.get('title')); 22 23 }); 24 25 TodoCollection.on('change', function(model){ 26 27 console.log('change:' + model.get('title')); 28 29 }); 30 31 TodoCollection.set([ 32 33 {id:1,title: 'go to a', completed:false}, 34 35 {title: 'go to b', completed:false}, 36 37 {title: 'go to c', completed:false}, 38 39 ]);
該代碼的執行結果是backbone
1 change:go to a 2 remove:go to b 3 remove:go to c 4 add:go to b 5 add:go to c