File tree Expand file tree Collapse file tree 4 files changed +62
-11
lines changed Expand file tree Collapse file tree 4 files changed +62
-11
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ var arrayMethods = Object.create(arrayProto)
28
28
}
29
29
var result = original . apply ( this , args )
30
30
var ob = this . __ob__
31
- var inserted
31
+ var inserted , removed
32
32
switch ( method ) {
33
33
case 'push' :
34
34
inserted = args
@@ -38,11 +38,17 @@ var arrayMethods = Object.create(arrayProto)
38
38
break
39
39
case 'splice' :
40
40
inserted = args . slice ( 2 )
41
+ removed = result
42
+ break
43
+ case 'pop' :
44
+ case 'shift' :
45
+ removed = [ result ]
41
46
break
42
47
}
43
48
if ( inserted ) ob . observeArray ( inserted )
49
+ if ( removed ) ob . unobserveArray ( removed )
44
50
// notify change
45
- ob . dep . notify ( )
51
+ ob . notify ( )
46
52
return result
47
53
} )
48
54
} )
Original file line number Diff line number Diff line change @@ -111,7 +111,42 @@ Observer.prototype.observe = function (val) {
111
111
Observer . prototype . observeArray = function ( items ) {
112
112
var i = items . length
113
113
while ( i -- ) {
114
- this . observe ( items [ i ] )
114
+ var ob = this . observe ( items [ i ] )
115
+ if ( ob ) {
116
+ ( ob . parents || ( ob . parents = [ ] ) ) . push ( this )
117
+ }
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Remove self from the parent list of removed objects.
123
+ *
124
+ * @param {Array } items
125
+ */
126
+
127
+ Observer . prototype . unobserveArray = function ( items ) {
128
+ var i = items . length
129
+ while ( i -- ) {
130
+ var ob = items [ i ] && items [ i ] . __ob__
131
+ if ( ob ) {
132
+ ob . parents . $remove ( this )
133
+ }
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Notify self dependency, and also parent Array dependency
139
+ * if any.
140
+ */
141
+
142
+ Observer . prototype . notify = function ( ) {
143
+ this . dep . notify ( )
144
+ var parents = this . parents
145
+ if ( parents ) {
146
+ var i = parents . length
147
+ while ( i -- ) {
148
+ parents [ i ] . notify ( )
149
+ }
115
150
}
116
151
}
117
152
@@ -136,12 +171,6 @@ Observer.prototype.convert = function (key, val) {
136
171
if ( childOb ) {
137
172
childOb . dep . depend ( )
138
173
}
139
- if ( _ . isArray ( val ) ) {
140
- for ( var e , i = 0 , l = val . length ; i < l ; i ++ ) {
141
- e = val [ i ]
142
- e && e . __ob__ && e . __ob__ . dep . depend ( )
143
- }
144
- }
145
174
}
146
175
return val
147
176
} ,
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ _.define(
21
21
return
22
22
}
23
23
ob . convert ( key , val )
24
- ob . dep . notify ( )
24
+ ob . notify ( )
25
25
if ( ob . vms ) {
26
26
var i = ob . vms . length
27
27
while ( i -- ) {
@@ -69,7 +69,7 @@ _.define(
69
69
if ( ! ob || _ . isReserved ( key ) ) {
70
70
return
71
71
}
72
- ob . dep . notify ( )
72
+ ob . notify ( )
73
73
if ( ob . vms ) {
74
74
var i = ob . vms . length
75
75
while ( i -- ) {
Original file line number Diff line number Diff line change @@ -332,4 +332,20 @@ describe('Misc', function () {
332
332
Vue . config . strict = false
333
333
} )
334
334
335
+ it ( 'nested object $set should trigger parent array notify' , function ( done ) {
336
+ var vm = new Vue ( {
337
+ el : document . createElement ( 'div' ) ,
338
+ template : '{{items | json}}{{items[0].a}}' ,
339
+ data : {
340
+ items : [ { } ]
341
+ }
342
+ } )
343
+ expect ( vm . $el . textContent ) . toBe ( JSON . stringify ( vm . items , null , 2 ) )
344
+ vm . items [ 0 ] . $set ( 'a' , 123 )
345
+ Vue . nextTick ( function ( ) {
346
+ expect ( vm . $el . textContent ) . toBe ( JSON . stringify ( vm . items , null , 2 ) + '123' )
347
+ done ( )
348
+ } )
349
+ } )
350
+
335
351
} )
You can’t perform that action at this time.
0 commit comments