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

fragment attach hook should check if child is inDoc (fix #2445)

Evan You 10 лет назад
Родитель
Сommit
d8f0ce92a6
2 измененных файлов с 42 добавлено и 2 удалено
  1. 2 2
      src/fragment/fragment.js
  2. 40 0
      test/unit/specs/misc_spec.js

+ 2 - 2
src/fragment/fragment.js

@@ -192,7 +192,7 @@ Fragment.prototype.destroy = function () {
  */
 
 function attach (child) {
-  if (!child._isAttached) {
+  if (!child._isAttached && inDoc(child.$el)) {
     child._callHook('attached')
   }
 }
@@ -204,7 +204,7 @@ function attach (child) {
  */
 
 function detach (child) {
-  if (child._isAttached) {
+  if (child._isAttached && !inDoc(child.$el)) {
     child._callHook('detached')
   }
 }

+ 40 - 0
test/unit/specs/misc_spec.js

@@ -485,4 +485,44 @@ describe('Misc', function () {
       })
     }).not.toThrow()
   })
+
+  // #2445
+  it('fragment attach hook should check if child is inDoc', function (done) {
+    var el = document.createElement('div')
+    document.body.appendChild(el)
+    var spyParent = jasmine.createSpy('attached parent')
+    var spyChild = jasmine.createSpy('attached child')
+
+    new Vue({
+      el: el,
+      template: '<comp v-for="n in 1"></comp>',
+      components: {
+        comp: {
+          template: '<div><child></child></div>',
+          attached: function () {
+            expect(_.inDoc(this.$el)).toBe(true)
+            spyParent()
+          },
+          activate: function (next) {
+            setTimeout(function () {
+              next()
+              check()
+            }, 100)
+          },
+          components: {
+            child: {
+              template: 'yo',
+              attached: spyChild
+            }
+          }
+        }
+      }
+    })
+
+    function check () {
+      expect(spyParent).toHaveBeenCalled()
+      expect(spyChild).toHaveBeenCalled()
+      done()
+    }
+  })
 })