|
|
@@ -5,9 +5,18 @@ import { mergeVNodeHook } from 'core/vdom/helpers'
|
|
|
|
|
|
export default {
|
|
|
create: function bindDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) {
|
|
|
- mergeVNodeHook(vnode.data.hook || (vnode.data.hook = {}), 'insert', () => {
|
|
|
- applyDirectives(oldVnode, vnode, 'bind')
|
|
|
+ let hasInsert = false
|
|
|
+ forEachDirective(oldVnode, vnode, (def, dir) => {
|
|
|
+ callHook(def, dir, 'bind', vnode, oldVnode)
|
|
|
+ if (def.inserted) {
|
|
|
+ hasInsert = true
|
|
|
+ }
|
|
|
})
|
|
|
+ if (hasInsert) {
|
|
|
+ mergeVNodeHook(vnode.data.hook || (vnode.data.hook = {}), 'insert', () => {
|
|
|
+ applyDirectives(oldVnode, vnode, 'inserted')
|
|
|
+ })
|
|
|
+ }
|
|
|
},
|
|
|
update: function updateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) {
|
|
|
applyDirectives(oldVnode, vnode, 'update')
|
|
|
@@ -27,28 +36,43 @@ export default {
|
|
|
|
|
|
const emptyModifiers = Object.create(null)
|
|
|
|
|
|
-function applyDirectives (
|
|
|
+function forEachDirective (
|
|
|
oldVnode: VNodeWithData,
|
|
|
vnode: VNodeWithData,
|
|
|
- hook: string
|
|
|
+ fn: Function
|
|
|
) {
|
|
|
const dirs = vnode.data.directives
|
|
|
if (dirs) {
|
|
|
- const oldDirs = oldVnode.data.directives
|
|
|
- const isUpdate = hook === 'update' || hook === 'componentUpdated'
|
|
|
for (let i = 0; i < dirs.length; i++) {
|
|
|
const dir = dirs[i]
|
|
|
const def = resolveAsset(vnode.context.$options, 'directives', dir.name, true)
|
|
|
- const fn = def && def[hook]
|
|
|
- if (fn) {
|
|
|
- if (isUpdate && oldDirs) {
|
|
|
+ if (def) {
|
|
|
+ const oldDirs = oldVnode && oldVnode.data.directives
|
|
|
+ if (oldDirs) {
|
|
|
dir.oldValue = oldDirs[i].value
|
|
|
}
|
|
|
if (!dir.modifiers) {
|
|
|
dir.modifiers = emptyModifiers
|
|
|
}
|
|
|
- fn(vnode.elm, dir, vnode, oldVnode)
|
|
|
+ fn(def, dir)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+function applyDirectives (
|
|
|
+ oldVnode: VNodeWithData,
|
|
|
+ vnode: VNodeWithData,
|
|
|
+ hook: string
|
|
|
+) {
|
|
|
+ forEachDirective(oldVnode, vnode, (def, dir) => {
|
|
|
+ callHook(def, dir, hook, vnode, oldVnode)
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function callHook (def, dir, hook, vnode, oldVnode) {
|
|
|
+ const fn = def && def[hook]
|
|
|
+ if (fn) {
|
|
|
+ fn(vnode.elm, dir, vnode, oldVnode)
|
|
|
+ }
|
|
|
+}
|