Skip to content

Commit 171fdc7

Browse files
committed
fix $add() not triggering deep watchers for nested array objects
1 parent 17c3726 commit 171fdc7

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

src/instance/scope.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ exports._defineMeta = function (key, value) {
248248
enumerable: true,
249249
configurable: true,
250250
get: function metaGetter () {
251-
dep.depend()
251+
if (Dep.target) {
252+
dep.depend()
253+
}
252254
return value
253255
},
254256
set: function metaSetter (val) {

src/observer/dep.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ p.removeSub = function (sub) {
4343
*/
4444

4545
p.depend = function () {
46-
if (Dep.target) {
47-
Dep.target.addDep(this)
48-
}
46+
Dep.target.addDep(this)
4947
}
5048

5149
/**

src/observer/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require('./object')
1717

1818
function Observer (value) {
1919
this.value = value
20-
this.active = true
2120
this.dep = new Dep()
2221
_.define(value, '__ob__', this)
2322
if (_.isArray(value)) {
@@ -142,11 +141,17 @@ p.convert = function (key, val) {
142141
enumerable: true,
143142
configurable: true,
144143
get: function () {
145-
if (ob.active) {
144+
if (Dep.target) {
146145
dep.depend()
147-
}
148-
if (childOb) {
149-
childOb.dep.depend()
146+
if (childOb) {
147+
childOb.dep.depend()
148+
}
149+
if (_.isArray(val)) {
150+
for (var e, i = 0, l = val.length; i < l; i++) {
151+
e = val[i]
152+
e && e.__ob__ && e.__ob__.dep.depend()
153+
}
154+
}
150155
}
151156
return val
152157
},

test/unit/specs/observer/observer_spec.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var Observer = require('../../../../src/observer')
22
var config = require('../../../../src/config')
3-
var Dep = require('../../../../src/observer/dep')
43
var _ = require('../../../../src/util')
54

65
describe('Observer', function () {
@@ -25,7 +24,6 @@ describe('Observer', function () {
2524
}
2625
var ob = Observer.create(obj)
2726
expect(ob instanceof Observer).toBe(true)
28-
expect(ob.active).toBe(true)
2927
expect(ob.value).toBe(obj)
3028
expect(obj.__ob__).toBe(ob)
3129
// should've walked children
@@ -41,7 +39,6 @@ describe('Observer', function () {
4139
var arr = [{}, {}]
4240
var ob = Observer.create(arr)
4341
expect(ob instanceof Observer).toBe(true)
44-
expect(ob.active).toBe(true)
4542
expect(ob.value).toBe(arr)
4643
expect(arr.__ob__).toBe(ob)
4744
// should've walked children
@@ -69,7 +66,6 @@ describe('Observer', function () {
6966
obj.a.b = 3
7067
expect(watcher.update.calls.count()).toBe(1)
7168
// swap object
72-
var oldA = obj.a
7369
obj.a = { b: 4 }
7470
expect(watcher.update.calls.count()).toBe(2)
7571
watcher.deps = []

0 commit comments

Comments
 (0)