ソースを参照

fix attach/detach hooks for transcluded components

Evan You 11 年 前
コミット
3f943c6f8a
3 ファイル変更35 行追加12 行削除
  1. 3 0
      src/instance/compile.js
  2. 30 12
      src/instance/events.js
  3. 2 0
      src/instance/init.js

+ 3 - 0
src/instance/compile.js

@@ -39,10 +39,12 @@ exports._compile = function (el) {
       parentOptions._skipAttrs = null
       parentOptions._skipAttrs = null
 
 
       if (content) {
       if (content) {
+        var ol = parent._children.length
         var contentLinkFn =
         var contentLinkFn =
           compile(content, parentOptions, true)
           compile(content, parentOptions, true)
         // call content linker now, before transclusion
         // call content linker now, before transclusion
         this._contentUnlinkFn = contentLinkFn(parent, content)
         this._contentUnlinkFn = contentLinkFn(parent, content)
+        this._transCpnts = parent._children.slice(ol)
       }
       }
       // tranclude, this possibly replaces original
       // tranclude, this possibly replaces original
       el = transclude(el, options)
       el = transclude(el, options)
@@ -176,6 +178,7 @@ exports._cleanup = function () {
   this.$parent =
   this.$parent =
   this.$root =
   this.$root =
   this._children =
   this._children =
+  this._transCpnts =
   this._directives = null
   this._directives = null
   // call the last hook...
   // call the last hook...
   this._isDestroyed = true
   this._isDestroyed = true

+ 30 - 12
src/instance/events.js

@@ -79,12 +79,21 @@ exports._initDOMHooks = function () {
 
 
 function onAttached () {
 function onAttached () {
   this._isAttached = true
   this._isAttached = true
-  var children = this._children
-  for (var i = 0, l = children.length; i < l; i++) {
-    var child = children[i]
-    if (!child._isAttached && inDoc(child.$el)) {
-      child._callHook('attached')
-    }
+  this._children.forEach(callAttach)
+  if (this._transCpnts) {
+    this._transCpnts.forEach(callAttach)
+  }
+}
+
+/**
+ * Iterator to call attached hook
+ * 
+ * @param {Vue} child
+ */
+
+function callAttach (child) {
+  if (!child._isAttached && inDoc(child.$el)) {
+    child._callHook('attached')
   }
   }
 }
 }
 
 
@@ -94,12 +103,21 @@ function onAttached () {
 
 
 function onDetached () {
 function onDetached () {
   this._isAttached = false
   this._isAttached = false
-  var children = this._children
-  for (var i = 0, l = children.length; i < l; i++) {
-    var child = children[i]
-    if (child._isAttached && !inDoc(child.$el)) {
-      child._callHook('detached')
-    }
+  this._children.forEach(callDetach)
+  if (this._transCpnts) {
+    this._transCpnts.forEach(callDetach)
+  }
+}
+
+/**
+ * Iterator to call detached hook
+ * 
+ * @param {Vue} child
+ */
+
+function callDetach (child) {
+  if (child._isAttached && !inDoc(child.$el)) {
+    child._callHook('detached')
   }
   }
 }
 }
 
 

+ 2 - 0
src/instance/init.js

@@ -48,6 +48,8 @@ exports._init = function (options) {
   // children
   // children
   this._children = []
   this._children = []
   this._childCtors = {}
   this._childCtors = {}
+  // transcluded components that belong to the parent
+  this._transCpnts = null
 
 
   // merge options.
   // merge options.
   options = this.$options = mergeOptions(
   options = this.$options = mergeOptions(