Browse Source

cancel acitvate hook callback if component switches before activation (fix #1994)

Evan You 10 years ago
parent
commit
55a56a61e5

+ 3 - 0
src/directives/internal/component.js

@@ -144,6 +144,9 @@ export default {
     if (activateHook && !cached) {
       this.waitingFor = newComponent
       activateHook.call(newComponent, function () {
+        if (self.waitingFor !== newComponent) {
+          return
+        }
         self.waitingFor = null
         self.transition(newComponent, cb)
       })

+ 5 - 0
test/unit/specs/directives/internal/component_spec.js

@@ -320,11 +320,16 @@ describe('Component', function () {
       // cleans up the component being waited on.
       // see #1152
       vm.view = 'view-a'
+      // store the ready callback for view-a
+      var callback = next
       _.nextTick(function () {
         vm.view = 'view-b'
         _.nextTick(function () {
           expect(vm.$children.length).toBe(1)
           expect(vm.$children[0].$el.textContent).toBe('BBB')
+          // calling view-a's ready callback here should not throw
+          // because it should've been cancelled (#1994)
+          expect(callback).not.toThrow()
           done()
         })
       })