Просмотр исходного кода

fix $add() not triggering deep watchers for nested array objects

Evan You 11 лет назад
Родитель
Сommit
171fdc7ad5
4 измененных файлов с 14 добавлено и 13 удалено
  1. 3 1
      src/instance/scope.js
  2. 1 3
      src/observer/dep.js
  3. 10 5
      src/observer/index.js
  4. 0 4
      test/unit/specs/observer/observer_spec.js

+ 3 - 1
src/instance/scope.js

@@ -248,7 +248,9 @@ exports._defineMeta = function (key, value) {
     enumerable: true,
     configurable: true,
     get: function metaGetter () {
-      dep.depend()
+      if (Dep.target) {
+        dep.depend()
+      }
       return value
     },
     set: function metaSetter (val) {

+ 1 - 3
src/observer/dep.js

@@ -43,9 +43,7 @@ p.removeSub = function (sub) {
  */
 
 p.depend = function () {
-  if (Dep.target) {
-    Dep.target.addDep(this)
-  }
+  Dep.target.addDep(this)
 }
 
 /**

+ 10 - 5
src/observer/index.js

@@ -17,7 +17,6 @@ require('./object')
 
 function Observer (value) {
   this.value = value
-  this.active = true
   this.dep = new Dep()
   _.define(value, '__ob__', this)
   if (_.isArray(value)) {
@@ -142,11 +141,17 @@ p.convert = function (key, val) {
     enumerable: true,
     configurable: true,
     get: function () {
-      if (ob.active) {
+      if (Dep.target) {
         dep.depend()
-      }
-      if (childOb) {
-        childOb.dep.depend()
+        if (childOb) {
+          childOb.dep.depend()
+        }
+        if (_.isArray(val)) {
+          for (var e, i = 0, l = val.length; i < l; i++) {
+            e = val[i]
+            e && e.__ob__ && e.__ob__.dep.depend()
+          }
+        }
       }
       return val
     },

+ 0 - 4
test/unit/specs/observer/observer_spec.js

@@ -1,6 +1,5 @@
 var Observer = require('../../../../src/observer')
 var config = require('../../../../src/config')
-var Dep = require('../../../../src/observer/dep')
 var _ = require('../../../../src/util')
 
 describe('Observer', function () {
@@ -25,7 +24,6 @@ describe('Observer', function () {
     }
     var ob = Observer.create(obj)
     expect(ob instanceof Observer).toBe(true)
-    expect(ob.active).toBe(true)
     expect(ob.value).toBe(obj)
     expect(obj.__ob__).toBe(ob)
     // should've walked children
@@ -41,7 +39,6 @@ describe('Observer', function () {
     var arr = [{}, {}]
     var ob = Observer.create(arr)
     expect(ob instanceof Observer).toBe(true)
-    expect(ob.active).toBe(true)
     expect(ob.value).toBe(arr)
     expect(arr.__ob__).toBe(ob)
     // should've walked children
@@ -69,7 +66,6 @@ describe('Observer', function () {
     obj.a.b = 3
     expect(watcher.update.calls.count()).toBe(1)
     // swap object
-    var oldA = obj.a
     obj.a = { b: 4 }
     expect(watcher.update.calls.count()).toBe(2)
     watcher.deps = []