Ver código fonte

test for shared observe and unobserve

Evan You 12 anos atrás
pai
commit
d9c463855c
2 arquivos alterados com 21 adições e 0 exclusões
  1. 1 0
      src/observe/observer.js
  2. 20 0
      test/unit/specs/observer_spec.js

+ 1 - 0
src/observe/observer.js

@@ -230,6 +230,7 @@ p.propagate = function (event, path, val, mutation) {
   if (!this.parents) return
   for (var id in this.parents) {
     var parent = this.parents[id]
+    if (!parent) continue
     var key = parent.key
     var parentPath = path
       ? key + Observer.pathDelimiter + path

+ 20 - 0
test/unit/specs/observer_spec.js

@@ -390,4 +390,24 @@ describe('Observer', function () {
     expect(spy).toHaveBeenCalledWith('0.a', 3, u)
   })
 
+  it('shared observe', function () {
+    var obj = { a: 1 }
+    var parentA = { child1: obj }
+    var parentB = { child2: obj }
+    var obA = Observer.create(parentA)
+    var obB = Observer.create(parentB)
+    obA.on('set', spy)
+    obB.on('set', spy)
+    obj.a = 2
+    expect(spy.callCount).toBe(2)
+    expect(spy).toHaveBeenCalledWith('child1.a', 2, u)
+    expect(spy).toHaveBeenCalledWith('child2.a', 2, u)
+    // test unobserve
+    parentA.child1 = null
+    obj.a = 3
+    expect(spy.callCount).toBe(4)
+    expect(spy).toHaveBeenCalledWith('child1', null, u)
+    expect(spy).toHaveBeenCalledWith('child2.a', 3, u)
+  })
+
 })