-
Notifications
You must be signed in to change notification settings - Fork 1
/
Backbone-Reading-Note.js
1603 lines (1494 loc) · 64.1 KB
/
Backbone-Reading-Note.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (root, factory) {
//root == 当前js环境全局,global or window
//amd模式
if (typeof define === 'function' && define.amd) {
define (['underscore', 'jquery', 'exports'], function (_, $, exports) {
root.Backbone = factory (root, exports, _, $);
});
}
//node require or cmd
else if (typeof exports !== 'undefined') {
var _ = require ('underscore');
//这里有点疑惑为什么不直接传入$,反而是使用全局下面的$,技巧?漏写?
factory (root, exports, _);
}
//其余
else {
root.Backbone = factory (root, {}, root._, root.jQuery || root.Zepto || root.ender || root.$);
}
}) (this, function (root, Backbone, _, $) {
var previousBackbone = root.Backbone;
var array = [];
var push = array.push;
var slice = array.slice;
var splice = array.splice;
Backbone.VERSION = '1.1.2';
Backbone.$ = $;
//解决冲突,var _b = Backbone.onConflict();
Backbone.noConflict = function () {
root.Backbone = previousBackbone;
return this;
};
Backbone.emulateHTTP = false;
Backbone.emulateJSON = false;
/*-------------------------------Events start-----------------------------------------*/
var Events = Backbone.Events = {
//下面这些函数的步骤总结起来就是,先用eventsApi去注册jQuery事件,然后再保存/清理/触发_events事件
on: function (name, callback, context) {
//参数缺失的情况,例如 name不符合 obj、string spilt 、 null undefined等
if (! eventsApi (this, 'on', name, [callback, context]) || ! callback) {
return this;
}
//注册事件成功,但为了后期操作,存放在对应的_events属性中
this._events || (this._events = {});
var events = this._events[name] || (this._events[name] = []);
events.push ({
callback: callback,
context: context,
ctx: context || this
});
return this;
},
once: function (name, callback, context) {
if (! eventsApi (this, 'once', name, [callback, context]) || ! callback) {
return this;
}
var self = this;
var once = _.once (function () {
//没啥好说的,包装的常用技巧
self.off (name, once);
callback.apply (this, arguments);
});
once._callback = callback;
return this.on (name, once, context);
},
off: function (name, callback, context) {
var retain,
ev,
events,
names,
i,
l,
j,
k;
//jQuery的事件解绑操作是在这一步完成的,下面是对events进行清理
if (! this._events || ! eventsApi (this, 'off', name, [callback, context])) {
return this;
}
//对应Backbone.Events.off(),删除所有的事件
if (! name && ! callback && ! context) {
this._events = void 0;
return this;
}
//对应Backbone.Events.off("click") or Backbone.Events.off(null,fn?,context?)
names = name ? [name] : _.keys (this._events);
for (i = 0, l = names.length; i < l; i ++) {
name = names[i];
if (events = this._events[name]) {
//retain用来得到过滤之后的事件对象
this._events[name] = retain = [];
if (callback || context) {
for (j = 0, k = events.length; j < k; j ++) {
ev = events[j];
//不符合fn or context条件的进行对应的保留
if (callback && callback !== ev.callback && callback !== ev.callback._callback || context && context !== ev.context) {
retain.push (ev);
}
}
}
//全部都符合fn or context条件,则这个字段直接删除
if (! retain.length) {
delete this._events[name];
}
}
}
return this;
},
trigger: function (name) {
if (! this._events) {
return this;
}
var args = slice.call (arguments, 1);
if (! eventsApi (this, 'trigger', name, args)) {
return this;
}
var events = this._events[name];
var allEvents = this._events.all;
if (events) {
triggerEvents (events, args);
}
if (allEvents) {
triggerEvents (allEvents, arguments);
}
return this;
},
//其实与off很类似,其实就是清理自身的_listeningTo保存的事件对象
stopListening: function (obj, name, callback) {
var listeningTo = this._listeningTo;
if (! listeningTo) {
return this;
}
var remove = ! name && ! callback;
if (! callback && typeof name === 'object') {
callback = this;
}
if (obj) {
(listeningTo = {})[obj._listenId] = obj;
}
for (var id in listeningTo) {
obj = listeningTo[id];
obj.off (name, callback, this);
if (remove || _.isEmpty (obj._events)) {
delete this._listeningTo[id];
}
}
return this;
}
};
var eventSplitter = /\s+/;
//调用jQuery事件函数,如果调用成功则返回false,否则true
var eventsApi = function (obj, action, name, rest) {
if (! name) {
return true;
}
if (typeof name === 'object') {
for (var key in name) {
//调用$obj.on(eventName,fn,context)方法
obj[action].apply (obj, [key, (name[key])].concat (rest));
}
return false;
}
if (eventSplitter.test (name)) {
var names = name.split (eventSplitter);
//"click mouseover mousemove"的形式
for (var i = 0, l = names.length; i < l; i ++) {
obj[action].apply (obj, [(names[i])].concat (rest));
}
return false;
}
return true;
};
//事件触发
var triggerEvents = function (events, args) {
var ev,
i = - 1,
l = events.length,
a1 = args[0],
a2 = args[1],
a3 = args[2];
switch (args.length) {
case 0:
while (++ i < l) {
(ev = events[i]).callback.call (ev.ctx);
}
return;
case 1:
while (++ i < l) {
(ev = events[i]).callback.call (ev.ctx, a1);
}
return;
case 2:
while (++ i < l) {
(ev = events[i]).callback.call (ev.ctx, a1, a2);
}
return;
case 3:
while (++ i < l) {
(ev = events[i]).callback.call (ev.ctx, a1, a2, a3);
}
return;
default:
while (++ i < l) {
(ev = events[i]).callback.apply (ev.ctx, args);
}
return;
}
};
//可以看到,其实ListenTo和ListenToOnce就是on和once,只是进行了一次包装
var listenMethods = {
listenTo: 'on',
listenToOnce: 'once'
};
//underscore的each方法兼容了obj的解析,implementation = on/once , method = listenTo/ListenToOnce
_.each (listenMethods, function (implementation, method) {
Events[method] = function (obj, name, callback) {
var listeningTo = this._listeningTo || (this._listeningTo = {});
//_.uniqueId返回唯一的 前缀+randomNum 的 id , 例如下面可能是 l_201,
//详情参考underscore源码实现(其实就是局部prefix + idContainer自增)
var id = obj._listenId || (obj._listenId = _.uniqueId ('l'));
//添加到listeningTo里面去
listeningTo[id] = obj;
//这种情况下,其实callback = context的作用
if (! callback && typeof name === 'object') {
callback = this;
}
//为什么要使用on/once,而不选择重写一个其他方法呢?
//其实很简单,listenTo的作用是当obj变化的时候能够回掉callback,
//listenTo和on唯一不同的地方在于this的引用,请看
//model.listenTo(view,"click",send); send的this == model 而不是 view
//model.on("click",send); send的this == model
//而on方法是可以顺利改变this指向,因此复用on是合理的
obj[implementation] (name, callback, this);
return this;
};
});
//下面没啥好说的,添加Events对象,并且取别名
Events.bind = Events.on;
Events.unbind = Events.off;
_.extend (Backbone, Events);
/*-------------------------------Events end-----------------------------------------*/
/*---------------------------------Model start---------------------------------------*/
//Model继承自Events,因此具有所有Events的事件方法
var Model = Backbone.Model = function (attributes, options) {
var attrs = attributes || {};
options || (options = {});
this.cid = _.uniqueId ('c');
this.attributes = {};
//指定Model的Collection, new Backbone.Model({},{collection:"xx"});
if (options.collection) {
this.collection = options.collection;
}
//指定Model的parse,parse用来解析参数列表,new Backbone.Model({},{parse:true});
if (options.parse) {
//this.parse从Backbone.Model.extend({parse:fn})中来
attrs = this.parse (attrs, options) || {};
}
//使用_.result调用this的default方法,具体参考underscore手册
attrs = _.defaults ({}, attrs, _.result (this, 'defaults'));
//set是原型方法,参考下面的Model.prototype扩展
this.set (attrs, options);
this.changed = {};
//自定义初始化函数
this.initialize.apply (this, arguments);
};
_.extend (Model.prototype, Events, {
changed: null,
validationError: null,
idAttribute: 'id',
initialize: function () {
},
toJSON: function (options) {
return _.clone (this.attributes);
},
sync: function () {
return Backbone.sync.apply (this, arguments);
},
get: function (attr) {
return this.attributes[attr];
},
escape: function (attr) {
return _.escape (this.get (attr));
},
has: function (attr) {
return this.get (attr) != null;
},
set: function (key, val, options) {
var attr,
attrs,
unset,
changes,
silent,
changing,
prev,
current;
if (key == null) {
return this;
}
//下面都是参数不定的处理情况
if (typeof key === 'object') {
attrs = key;
options = val;
}
else {
(attrs = {})[key] = val;
}
options || (options = {});
//kv验证,if(m.set({name:"xxx"},{validate:true}){}
if (! this._validate (attrs, options)) {
return false;
}
unset = options.unset;
silent = options.silent;
changes = [];
changing = this._changing;
this._changing = true;
//保留kv副本,但是这里的判断似乎有些多余,但这里是考虑了worker/cluster的情况
if (! changing) {
this._previousAttributes = _.clone (this.attributes);
this.changed = {};
}
current = this.attributes, prev = this._previousAttributes;
//如果有修改idAttribute,则this.id应该同步
if (this.idAttribute in attrs) {
this.id = attrs[this.idAttribute];
}
for (attr in attrs) {
val = attrs[attr];
//下面的两个_.isEqual判断其实在非多线程环境下可以合并为一个,因为prev == current
//changes数组的作用参考下面是触发change:attr事件
//而this.changed是用来保存改变的属性及变化值,推荐使用副本changedAttribute查看
if (! _.isEqual (current[attr], val)) {
changes.push (attr);
}
if (! _.isEqual (prev[attr], val)) {
this.changed[attr] = val;
}
else {
//如果此次操作没有变化,则delete
//从上面可知,changed是可能为{}的,但是delete {}[attr]是恒为true
//那这种情况用在哪里呢?明显是多线程
//例如,原值{name:1}
//worker/cluster1 {name:2}进行了修改
//worker/cluster2 {name:1}进行了修改
//这个时候应该是值无变化操作的,所以应该进行deleteworker/cluster1的改变
delete this.changed[attr];
}
//如果设置了unset参数,那么会删除掉attr属性(changed保留),否则直接赋值
//这里主要是给unset方法服务的
unset ? delete current[attr] : current[attr] = val;
}
//触发change:attr事件
if (! silent) {
if (changes.length) {
this._pending = options;
}
for (var i = 0, l = changes.length; i < l; i ++) {
this.trigger ('change:' + changes[i], this, current[changes[i]], options);
}
}
//非主线程直接return了,下面的操作交给主线程进行操作
if (changing) {
return this;
}
//change事件冒泡,与change:attr不同,多线程情况下,change应该只触发一次
if (! silent) {
while (this._pending) {
options = this._pending;
this._pending = false;
this.trigger ('change', this, options);
}
}
this._pending = false;
this._changing = false;
return this;
},
unset: function (attr, options) {
return this.set (attr, void 0, _.extend ({}, options, {
unset: true
}));
},
clear: function (options) {
var attrs = {};
for (var key in this.attributes) {
attrs[key] = void 0;
}
return this.set (attrs, _.extend ({}, options, {
unset: true
}));
},
hasChanged: function (attr) {
if (attr == null) {
return ! _.isEmpty (this.changed);
}
return _.has (this.changed, attr);
},
changedAttributes: function (diff) {
if (! diff) {
return this.hasChanged () ? _.clone (this.changed) : false;
}
var val,
changed = false;
//不用想了,通过分析set方法可知,_changing就是多线程的原子量,下面很好理解了
var old = this._changing ? this._previousAttributes : this.attributes;
//这里提一下,官方推荐用法为model.changedAttributes(["name"])
//但是从这里分析可知,还可以用成model.changedAttribute({"name":123})
//用法一的含义是指,name属性被改变了么?
//用法二的含义是指,原值123的name值被改变了么?
for (var attr in diff) {
if (_.isEqual (old[attr], val = diff[attr])) {
continue;
}
(changed || (changed = {}))[attr] = val;
}
return changed;
},
previous: function (attr) {
if (attr == null || ! this._previousAttributes) {
return null;
}
return this._previousAttributes[attr];
},
previousAttributes: function () {
return _.clone (this._previousAttributes);
},
fetch: function (options) {
options = options ? _.clone (options) : {};
if (options.parse === void 0) {
options.parse = true;
}
var model = this;
//重新包装success回调
var success = options.success;
options.success = function (resp) {
//parse方法不做修改则只是return resp,当然,Backbone允许你进行覆盖自定义parse
if (! model.set (model.parse (resp, options), options)) {
return false;
}
if (success) {
success (model, resp, options);
}
//如果注册了sync事件的话,我就触发
model.trigger ('sync', model, resp, options);
};
//wrapError在代码最末尾,其实就是包装一下error回调而已
//因为多处使用了,所以进行了封装
wrapError (this, options);
//sync方法其实是调用了Backbone.sync,
//根据文档描述,里面的'read'之类的关键字只是用来确认GET POST DELETE PUT等
//其实质仍然是调用了jQuery.ajax方法
return this.sync ('read', this, options);
},
save: function (key, val, options) {
var attrs,
method,
xhr,
attributes = this.attributes;
//参数的包装,其实save从参数来看,其实能猜到是调用了set方法的(不信你看下面)
//save需要注意一下wait的使用,wait == 错误则回滚,成功则覆盖
if (key == null || typeof key === 'object') {
attrs = key;
options = val;
}
else {
(attrs = {})[key] = val;
}
//没有通过验证,则直接return false了
options = _.extend ({
validate: true
}, options);
if (attrs && ! options.wait) {
if (! this.set (attrs, options)) {
return false;
}
}
else {
if (! this._validate (attrs, options)) {
return false;
}
}
if (attrs && options.wait) {
this.attributes = _.extend ({}, attributes, attrs);
}
//下面的操作与fetch相差不大,对回调的封装
if (options.parse === void 0) {
options.parse = true;
}
var model = this;
var success = options.success;
options.success = function (resp) {
model.attributes = attributes;
var serverAttrs = model.parse (resp, options);
if (options.wait) {
serverAttrs = _.extend (attrs || {}, serverAttrs);
}
//这里实现了wait的回滚操作,验证不通过,则set不成功,return false
//总结起来就是,前后两次validate的数据验证,实现了wait的作用
if (_.isObject (serverAttrs) && ! model.set (serverAttrs, options)) {
return false;
}
if (success) {
success (model, resp, options);
}
model.trigger ('sync', model, resp, options);
};
wrapError (this, options);
//下面其实是针对restful来说的,
//因为如果是新创建的model,则save相当于后端的create操作
//如果是旧model,则只是后端的update
method = this.isNew () ? 'create' : options.patch ? 'patch' : 'update';
if (method === 'patch') {
options.attrs = attrs;
}
xhr = this.sync (method, this, options);
//为什么会插这么一句?因为从520-523行可以看到,
//通过validate校验之后对this.attributes进行覆盖,因为你要发ajax了嘛,肯定需要新数据
//而最开始的保存就是拿在这里使用,目的是success里面的wait回滚操作
if (attrs && options.wait) {
this.attributes = attributes;
}
return xhr;
},
destroy: function (options) {
options = options ? _.clone (options) : {};
var model = this;
var success = options.success;
var destroy = function () {
model.trigger ('destroy', model, model.collection, options);
};
options.success = function (resp) {
if (options.wait || model.isNew ()) {
destroy ();
}
if (success) {
success (model, resp, options);
}
if (! model.isNew ()) {
model.trigger ('sync', model, resp, options);
}
};
//如果是新创建的,则后端是不存在这个数据的,直接销毁就好了
if (this.isNew ()) {
options.success ();
return false;
}
wrapError (this, options);
var xhr = this.sync ('delete', this, options);
//提醒一下,destory的触发顺序总是在sync事件之前
if (! options.wait) {
destroy ();
}
return xhr;
},
url: function () {
var base = _.result (this, 'urlRoot') || _.result (this.collection, 'url') || urlError ();
if (this.isNew ()) {
return base;
}
//下面这个reg就是给url加末尾的斜杠的,原因是符合restful API
//1.http://www.index.php ---> http://www.index.php/
//2.http://www.index.php?kw=1 ---> http://www.index.php?kw=1/
//这里的返回值主要是给fetch及save后使用的,
//因为fetch及save后,则数据会有对应的id了呢
return base.replace (/([^\/])$/, '$1/') + encodeURIComponent (this.id);
},
parse: function (resp, options) {
return resp;
},
clone: function () {
//对象都有constructor函数,backbone里面支持你重写
//但是initlize方法可不是会被重写的哟
return new this.constructor (this.attributes);
},
isNew: function () {
return ! this.has (this.idAttribute);
},
isValid: function (options) {
return this._validate ({}, _.extend (options || {}, {
validate: true
}));
},
_validate: function (attrs, options) {
//这里说明了,在set的时候可以设定是否校正
//例如: m.set({},{validate:true}),
if (! options.validate || ! this.validate) {
return true;
}
attrs = _.extend ({}, this.attributes, attrs);
var error = this.validationError = this.validate (attrs, options) || null;
//返回任何为true的条件,则会触发invalid事件
if (! error) {
return true;
}
this.trigger ('invalid', this, error, _.extend (options, {
validationError: error
}));
return false;
}
});
//下面就是复制了一下underscore的方法给Model对象原型而已,没啥好说的
var modelMethods = ['keys', 'values', 'pairs', 'invert', 'pick', 'omit'];
_.each (modelMethods, function (method) {
Model.prototype[method] = function () {
var args = slice.call (arguments);
args.unshift (this.attributes);
return _[method].apply (_, args);
};
});
/*-------------------------------Model end-----------------------------------------*/
/*-------------------------------Collection start-----------------------------------------*/
var Collection = Backbone.Collection = function (models, options) {
options || (options = {});
//new Backbone.Collection([],{model:Model,comparator:fn})
if (options.model) {
this.model = options.model;
}
if (options.comparator !== void 0) {
this.comparator = options.comparator;
}
//collection初始化
this._reset ();
this.initialize.apply (this, arguments);
//直接创建,new Backbone.Collection([models]?)
if (models) {
this.reset (models, _.extend ({
silent: true
}, options));
}
};
var setOptions = {
add: true,
remove: true,
merge: true
};
var addOptions = {
add: true,
remove: false
};
_.extend (Collection.prototype, Events, {
model: Model,
initialize: function () {
},
toJSON: function (options) {
return this.map (function (model) {
return model.toJSON (options);
});
},
sync: function () {
return Backbone.sync.apply (this, arguments);
},
add: function (models, options) {
return this.set (models, _.extend ({
merge: false
}, options, addOptions));
},
remove: function (models, options) {
//代码太简单,没啥好说的
var singular = ! _.isArray (models);
models = singular ? [models] : _.clone (models);
options || (options = {});
var i,
l,
index,
model;
for (i = 0, l = models.length; i < l; i ++) {
model = models[i] = this.get (models[i]);
if (! model) {
continue;
}
delete this._byId[model.id];
delete this._byId[model.cid];
index = this.indexOf (model);
this.models.splice (index, 1);
this.length --;
if (! options.silent) {
options.index = index;
model.trigger ('remove', model, this, options);
}
this._removeReference (model, options);
}
return singular ? models[0] : models;
},
set: function (models, options) {
options = _.defaults ({}, options, setOptions);
//调用扩展的parse方法
if (options.parse) {
models = this.parse (models, options);
}
var singular = ! _.isArray (models);
models = singular ? models ? [models] : [] : _.clone (models);
var i,
l,
id,
model,
attrs,
existing,
sort;
var at = options.at;
//is model , not models
var targetModel = this.model;
var sortable = this.comparator && at == null && options.sort !== false;
var sortAttr = _.isString (this.comparator) ? this.comparator : null;
//从这里其实可以猜到,set方法是会将需要删除和新增的model进行分类的
//因此后面的remove和add事件触发就是根据你所在remove和add的数组来进行的
var toAdd = [],
toRemove = [],
modelMap = {};
//对于初始化来说,合并merge=false,remove=false,add=true
//需要注意的是,使用Backbone的set方法,是可以触发两个事件的remove和add
var add = options.add,
merge = options.merge,
remove = options.remove;
var order = ! sortable && add && remove ? [] : false;
for (i = 0, l = models.length; i < l; i ++) {
attrs = models[i] || {};
//get一个小技巧,Backbone.Collection的Model不一定是需要继承自Backbone.Model的
//在某些需要自定义Model的特殊情况下,如果一个对象继承了Backbone.Events即可
if (attrs instanceof Model) {
id = model = attrs;
}
else {
id = attrs[targetModel.prototype.idAttribute || 'id'];
}
//从下面可以看出,existing应该是一个model,因此this._byId其实一个model|id|cid => model的一个kv映射
if (existing = this.get (id)) {
//remove=true时旧的model是需要删除的,
//因为_.clone(model)进行了深度复制的话,cid新旧model相同,但属性不一定相等
if (remove) {
modelMap[existing.cid] = true;
}
if (merge) {
//更改attrs指向model.attributes,当然attrs也可能为{}
attrs = attrs === model ? model.attributes : attrs;
if (options.parse) {
attrs = existing.parse (attrs, options);
}
//model的attribute进行extend(cid才是判断两个model是否相同的标志,不是内存地址)
existing.set (attrs, options);
if (sortable && ! sort && existing.hasChanged (sortAttr)) {
sort = true;
}
}
//保存model对象(主要是针对{}属性的情况)
models[i] = existing;
}
else if (add) {
model = models[i] = this._prepareModel (attrs, options);
if (! model) {
continue;
}
toAdd.push (model);
this._addReference (model, options);
}
//结合_addReference的代码可以看到,当model.set({id:"_id"})的时候
//尽管cid在不断的增加,但是model永远都是在取existing的model(及第一次的model)
//为什么这么去实现呢?其实很简单,collection相当于数据库表
//而model相当于表单元,我们知道model.id是与数据库主键对应的
//主键不允许重复,因此model.id是不允许重复的
model = existing || model;
if (order && (model.isNew () || ! modelMap[model.id])) {
order.push (model);
}
modelMap[model.id] = true;
}
//删除原有不在新的models里的model
if (remove) {
for (i = 0, l = this.length; i < l; ++ i) {
//这里为什么用cid了,请看之前的789-791
if (! modelMap[(model = this.models[i]).cid]) {
toRemove.push (model);
}
}
if (toRemove.length) {
this.remove (toRemove, options);
}
}
//增加新的model,下面一堆没啥好说的,代码都很简单
if (toAdd.length || order && order.length) {
if (sortable) {
sort = true;
}
this.length += toAdd.length;
if (at != null) {
for (i = 0, l = toAdd.length; i < l; i ++) {
this.models.splice (at + i, 0, toAdd[i]);
}
}
else {
if (order) {
this.models.length = 0;
}
var orderedModels = order || toAdd;
for (i = 0, l = orderedModels.length; i < l; i ++) {
this.models.push (orderedModels[i]);
}
}
}
if (sort) {
this.sort ({
silent: true
});
}
if (! options.silent) {
for (i = 0, l = toAdd.length; i < l; i ++) {
(model = toAdd[i]).trigger ('add', model, this, options);
}
if (sort || order && order.length) {
this.trigger ('sort', this, options);
}
}
return singular ? models[0] : models;
},
reset: function (models, options) {
options || (options = {});
//下面干的事情其实是删除collection引用及model的all事件
for (var i = 0, l = this.models.length; i < l; i ++) {
this._removeReference (this.models[i], options);
}
options.previousModels = this.models;
this._reset ();
models = this.add (models, _.extend ({
silent: true
}, options));
if (! options.silent) {
this.trigger ('reset', this, options);
}
return models;
},
push: function (model, options) {
return this.add (model, _.extend ({
at: this.length
}, options));
},
pop: function (options) {
var model = this.at (this.length - 1);
this.remove (model, options);
return model;
},
unshift: function (model, options) {
return this.add (model, _.extend ({
at: 0
}, options));
},
shift: function (options) {
var model = this.at (0);
this.remove (model, options);
return model;
},
slice: function () {
return slice.apply (this.models, arguments);
},
get: function (obj) {
if (obj == null) {
return void 0;
}
return this._byId[obj] || this._byId[obj.id] || this._byId[obj.cid];
},
at: function (index) {
return this.models[index];
},
where: function (attrs, first) {
if (_.isEmpty (attrs)) {
return first ? void 0 : [];
}
return this[first ? 'find' : 'filter'] (function (model) {
for (var key in attrs) {
if (attrs[key] !== model.get (key)) {
return false;
}
}
return true;
});
},
findWhere: function (attrs) {
return this.where (attrs, true);
},
sort: function (options) {
if (! this.comparator) {
throw new Error ('Cannot sort a set without a comparator');
}
options || (options = {});
if (_.isString (this.comparator) || this.comparator.length === 1) {
this.models = this.sortBy (this.comparator, this);
}
else {
this.models.sort (_.bind (this.comparator, this));
}
if (! options.silent) {
this.trigger ('sort', this, options);
}
return this;
},
pluck: function (attr) {
return _.invoke (this.models, 'get', attr);
},
fetch: function (options) {
options = options ? _.clone (options) : {};
if (options.parse === void 0) {
options.parse = true;
}
var success = options.success;
var collection = this;
options.success = function (resp) {
var method = options.reset ? 'reset' : 'set';
collection[method] (resp, options);
if (success) {
success (collection, resp, options);
}
collection.trigger ('sync', collection, resp, options);
};
wrapError (this, options);
return this.sync ('read', this, options);
},
create: function (model, options) {
options = options ? _.clone (options) : {};
if (! (model = this._prepareModel (model, options))) {
return false;
}
if (! options.wait) {
this.add (model, options);
}
var collection = this;
var success = options.success;
options.success = function (model, resp) {
if (options.wait) {
collection.add (model, options);
}
if (success) {
success (model, resp, options);
}
};