Переглянути джерело

expose context on vnode, simplify render methods

Evan You 10 роки тому
батько
коміт
f4a2afb3b3

+ 3 - 7
src/compiler/codegen.js

@@ -45,7 +45,7 @@ function genElement (el) {
     if (el.staticRoot) {
       // hoist static sub-trees out
       staticRenderFns.push(`with(this){return ${code}}`)
-      return `__static__(${staticRenderFns.length - 1})`
+      return `_staticTrees[${staticRenderFns.length - 1}]`
     } else {
       return code
     }
@@ -113,11 +113,7 @@ function genData (el) {
   }
   // transition
   if (el.transition) {
-    data += `transition:__resolveTransition__(${
-      el.transition
-    }, ${
-      el.transitionOnAppear
-    }),`
+    data += `transition:{definition:(${el.transition}),appear:${el.transitionOnAppear}},`
   }
   // v-show, used to avoid transition being applied
   // since v-show takes it over
@@ -163,7 +159,7 @@ function genDirectives (el) {
     }
     if (needRuntime) {
       hasRuntime = true
-      res += `{name:"${dir.name}",def:__resolveDirective__("${dir.name}")${
+      res += `{name:"${dir.name}"${
         dir.value ? `,value:(${dir.value})` : ''
       }${
         dir.arg ? `,arg:"${dir.arg}"` : ''

+ 1 - 18
src/core/instance/render.js

@@ -1,6 +1,6 @@
 import createElement from '../vdom/create-element'
 import { flatten } from '../vdom/helpers'
-import { bind, resolveAsset, isArray, isObject } from '../util/index'
+import { bind, isArray, isObject } from '../util/index'
 
 export const renderState = {
   activeInstance: null
@@ -39,23 +39,6 @@ export function renderMixin (Vue) {
   // shorthands used in render functions
   Vue.prototype.__h__ = createElement
 
-  Vue.prototype.__static__ = function (id) {
-    return this._staticTrees[id]
-  }
-
-  // resolve directive
-  Vue.prototype.__resolveDirective__ = function (id) {
-    return resolveAsset(this.$options, 'directives', id, true)
-  }
-
-  // resolve transition
-  Vue.prototype.__resolveTransition__ = function (id, appear) {
-    const definition = id && typeof id === 'string'
-      ? resolveAsset(this.$options, 'transitions', id) || id
-      : id
-    return { definition, appear, context: this }
-  }
-
   // toString for mustaches
   Vue.prototype.__toString__ = function (val) {
     return val == null

+ 6 - 3
src/core/vdom/create-component.js

@@ -6,7 +6,7 @@ import { warn, isObject, hasOwn, hyphenate } from '../util/index'
 const hooks = { init, prepatch, insert, destroy }
 const hooksToMerge = Object.keys(hooks)
 
-export function createComponent (Ctor, data, parent, children) {
+export function createComponent (Ctor, data, parent, children, context) {
   if (process.env.NODE_ENV !== 'production' &&
     children && typeof children !== 'function') {
     warn(
@@ -56,8 +56,11 @@ export function createComponent (Ctor, data, parent, children) {
   }
 
   // return a placeholder vnode
-  const name = Ctor.options.name ? '-' + Ctor.options.name : ''
-  const vnode = VNode(`vue-component-${Ctor.cid}-${name}`, data)
+  const name = Ctor.options.name ? ('-' + Ctor.options.name) : ''
+  const vnode = VNode(
+    `vue-component-${Ctor.cid}${name}`, data,
+    undefined, undefined, undefined, undefined, context
+  )
   vnode.componentOptions = { Ctor, propsData, listeners, parent, children }
   return vnode
 }

+ 10 - 4
src/core/vdom/create-element.js

@@ -11,9 +11,12 @@ export default function createElement (tag, data, children, namespace) {
   if (typeof tag === 'string') {
     let Ctor
     if (config.isReservedTag(tag)) {
-      return VNode(tag, data, flatten(children), undefined, undefined, namespace)
+      return VNode(
+        tag, data, flatten(children),
+        undefined, undefined, namespace, context
+      )
     } else if ((Ctor = resolveAsset(context.$options, 'components', tag))) {
-      return createComponent(Ctor, data, parent, children)
+      return createComponent(Ctor, data, parent, children, context)
     } else {
       if (process.env.NODE_ENV !== 'production') {
         if (!namespace && config.isUnknownElement(tag)) {
@@ -24,9 +27,12 @@ export default function createElement (tag, data, children, namespace) {
           )
         }
       }
-      return VNode(tag, data, flatten(children && children()), undefined, undefined, namespace)
+      return VNode(
+        tag, data, flatten(children && children()),
+        undefined, undefined, namespace, context
+      )
     }
   } else {
-    return createComponent(tag, data, parent, children)
+    return createComponent(tag, data, parent, children, context)
   }
 }

+ 4 - 1
src/core/vdom/modules/directives.js

@@ -1,3 +1,5 @@
+import { resolveAsset } from 'core/util/options'
+
 export default {
   create: function bindDirectives (oldVnode, vnode) {
     applyDirectives(oldVnode, vnode, 'bind')
@@ -15,7 +17,8 @@ function applyDirectives (oldVnode, vnode, hook, update) {
   if (dirs) {
     for (let i = 0; i < dirs.length; i++) {
       let dir = dirs[i]
-      let fn = dir.def && dir.def[hook]
+      let def = resolveAsset(vnode.context.$options, 'directives', dir.name, true)
+      let fn = def && def[hook]
       if (fn) {
         // only call update if value has changed
         if (update) {

+ 2 - 1
src/core/vdom/vnode.js

@@ -1,4 +1,4 @@
-export default function VNode (tag, data, children, text, elm, ns) {
+export default function VNode (tag, data, children, text, elm, ns, context) {
   return {
     tag,
     data,
@@ -6,6 +6,7 @@ export default function VNode (tag, data, children, text, elm, ns) {
     text,
     elm,
     ns,
+    context,
     key: data && data.key
   }
 }

+ 12 - 9
src/platforms/web/runtime/modules/transition.js

@@ -1,5 +1,5 @@
 import { addClass, removeClass } from '../class-util'
-import { inBrowser } from 'core/util/index'
+import { inBrowser, resolveAsset } from 'core/util/index'
 import { cached, remove } from 'shared/util'
 import { isIE9 } from 'web/util/index'
 
@@ -42,7 +42,7 @@ export function enter (vnode) {
   if (!data) {
     return
   }
-  if (!data.context.$root._mounted && !data.appear) {
+  if (!vnode.context.$root._mounted && !data.appear) {
     return
   }
 
@@ -53,7 +53,7 @@ export function enter (vnode) {
     enter,
     afterEnter,
     enterCancelled
-  } = detectAuto(data.definition)
+  } = resolveTransition(data.definition, vnode.context)
 
   const userWantsControl = enter && enter.length > 1
   const cb = el._enterCb = once(() => {
@@ -108,7 +108,7 @@ export function leave (vnode, rm) {
     leave,
     afterLeave,
     leaveCancelled
-  } = detectAuto(data.definition)
+  } = resolveTransition(data.definition, vnode.context)
 
   const userWantsControl = leave && leave.length > 1
   const cb = el._leaveCb = once(() => {
@@ -145,11 +145,14 @@ export function leave (vnode, rm) {
   }
 }
 
-function detectAuto (data) {
-  if (data === true) data = 'v'
-  return typeof data === 'string'
-    ? autoCssTransition(data)
-    : data
+function resolveTransition (id, context) {
+  let definition = id && typeof id === 'string'
+    ? resolveAsset(context.$options, 'transitions', id) || id
+    : id
+  if (definition === true) definition = 'v'
+  return typeof definition === 'string'
+    ? autoCssTransition(definition)
+    : definition
 }
 
 const autoCssTransition = cached(name => {