From 4a748838642f5681e7c18fc7f4448775d746ed4c Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 11 Oct 2016 19:32:01 -0400 Subject: [PATCH] improve mergeVNodeHook logic --- src/core/vdom/helpers.js | 20 +++++++++---------- src/core/vdom/modules/directives.js | 2 +- .../web/runtime/components/transition.js | 12 +++++------ .../web/runtime/modules/transition.js | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/core/vdom/helpers.js b/src/core/vdom/helpers.js index c2bfd6af..1d533936 100644 --- a/src/core/vdom/helpers.js +++ b/src/core/vdom/helpers.js @@ -65,19 +65,19 @@ export function getFirstComponentChild (children: ?Array) { return children && children.filter(c => c && c.componentOptions)[0] } -export function mergeVNodeHook (def: Object, key: string, hook: Function) { - const oldHook = def[key] +export function mergeVNodeHook (def: Object, hookKey: string, hook: Function, key: string) { + key = key + hookKey + const injectedHash = def.__injected || (def.__injected = {}) + if (injectedHash[key]) return + injectedHash[key] = true + const oldHook = def[hookKey] if (oldHook) { - const injectedHash = def.__injected || (def.__injected = {}) - if (!injectedHash[key]) { - injectedHash[key] = true - def[key] = function () { - oldHook.apply(this, arguments) - hook.apply(this, arguments) - } + def[hookKey] = function () { + oldHook.apply(this, arguments) + hook.apply(this, arguments) } } else { - def[key] = hook + def[hookKey] = hook } } diff --git a/src/core/vdom/modules/directives.js b/src/core/vdom/modules/directives.js index f9f61fce..c3c4541c 100644 --- a/src/core/vdom/modules/directives.js +++ b/src/core/vdom/modules/directives.js @@ -15,7 +15,7 @@ export default { if (hasInsert) { mergeVNodeHook(vnode.data.hook || (vnode.data.hook = {}), 'insert', () => { applyDirectives(oldVnode, vnode, 'inserted') - }) + }, 'dir-insert') } }, update: function updateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) { diff --git a/src/platforms/web/runtime/components/transition.js b/src/platforms/web/runtime/components/transition.js index 937d0659..321ef403 100644 --- a/src/platforms/web/runtime/components/transition.js +++ b/src/platforms/web/runtime/components/transition.js @@ -119,7 +119,7 @@ export default { return placeholder(h, rawChild) } - child.key = child.key == null || child.isStatic + const key = child.key = child.key == null || child.isStatic ? `__v${child.tag + this._uid}__` : child.key const data = (child.data || (child.data = {})).transition = extractTransitionData(this) @@ -132,7 +132,7 @@ export default { child.data.show = true } - if (oldChild && oldChild.data && oldChild.key !== child.key) { + if (oldChild && oldChild.data && oldChild.key !== key) { // replace old child transition data with fresh one // important for dynamic transitions! const oldData = oldChild.data.transition = extend({}, data) @@ -144,16 +144,16 @@ export default { mergeVNodeHook(oldData, 'afterLeave', () => { this._leaving = false this.$forceUpdate() - }) + }, key) return placeholder(h, rawChild) } else if (mode === 'in-out') { var delayedLeave var performLeave = () => { delayedLeave() } - mergeVNodeHook(data, 'afterEnter', performLeave) - mergeVNodeHook(data, 'enterCancelled', performLeave) + mergeVNodeHook(data, 'afterEnter', performLeave, key) + mergeVNodeHook(data, 'enterCancelled', performLeave, key) mergeVNodeHook(oldData, 'delayLeave', leave => { delayedLeave = leave - }) + }, key) } } diff --git a/src/platforms/web/runtime/modules/transition.js b/src/platforms/web/runtime/modules/transition.js index 1ff9e30b..cb5ceb8a 100644 --- a/src/platforms/web/runtime/modules/transition.js +++ b/src/platforms/web/runtime/modules/transition.js @@ -100,7 +100,7 @@ export function enter (vnode: VNodeWithData) { pendingNode.elm._leaveCb() } enterHook && enterHook(el, cb) - }) + }, 'transition-insert') } // start enter transition