Backbone的id

id

在model.attributes中,需要用户自行定义,可不定义,获取方法:model.get('id')

cid

collection中每个model都有的属性,由backbone自动生成,获取方法:model.cid 

idAttribute

在model中,用于指定使用model.attributes中哪个键作为id,默认情况下是model.attributes.id,需要用户自行定义,可不定义,获取方法:model.idAttribute

 

当使用set进行智能判断操作时,只有idAttribute指定的键相同的情况会把该操作视为change

 

DEMO

               

 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                 ]);
View Code

 

该代码的执行结果是

 

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 
View Code

 

posted @ 2013-10-24 23:56  will_kan  阅读(624)  评论(0编辑  收藏  举报