Selaa lähdekoodia

fire watcher on object when properties are added/deleted in non-deep mode (close #2036)

Evan You 10 vuotta sitten
vanhempi
commit
e1d6b3ede8
2 muutettua tiedostoa jossa 16 lisäystä ja 2 poistoa
  1. 2 2
      src/watcher.js
  2. 14 0
      test/unit/specs/watcher_spec.js

+ 2 - 2
src/watcher.js

@@ -233,11 +233,11 @@ Watcher.prototype.run = function () {
     var value = this.get()
     if (
       value !== this.value ||
-      // Deep watchers and Array watchers should fire even
+      // Deep watchers and watchers on Object/Arrays should fire even
       // when the value is the same, because the value may
       // have mutated; but only do so if this is a
       // non-shallow update (caused by a vm digest).
-      ((isArray(value) || this.deep) && !this.shallow)
+      ((isObject(value) || this.deep) && !this.shallow)
     ) {
       // set new value
       var oldValue = this.value

+ 14 - 0
test/unit/specs/watcher_spec.js

@@ -289,6 +289,20 @@ describe('Watcher', function () {
     })
   })
 
+  it('fire change for prop addition/deletion in non-deep mode', function (done) {
+    new Watcher(vm, 'b', spy)
+    Vue.set(vm.b, 'e', 123)
+    nextTick(function () {
+      expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
+      expect(spy.calls.count()).toBe(1)
+      Vue.delete(vm.b, 'e')
+      nextTick(function () {
+        expect(spy.calls.count()).toBe(2)
+        done()
+      })
+    })
+  })
+
   it('watch function', function (done) {
     var watcher = new Watcher(vm, function () {
       return this.a + this.b.d