Browse Source

refactor: simplify functional renderStatic

dev
Evan You 7 years ago
parent
commit
12255ff220
  1. 14
      src/core/instance/render-helpers/render-static.js
  2. 9
      src/core/instance/render.js
  3. 4
      src/core/vdom/create-functional-component.js

14
src/core/instance/render-helpers/render-static.js

@ -10,13 +10,10 @@ export function renderStatic (
isInFor?: boolean
): VNode | Array<VNode> {
// static trees can be rendered once and cached on the contructor options
// so every instance shares the same trees
let options = this.constructor.options
if (this.$options.staticRenderFns !== options.staticRenderFns) {
options = this.$options
}
const trees = options._staticTrees || (options._staticTrees = [])
let tree = trees[index]
// so every instance shares the same cached trees
const renderFns = this.$options.staticRenderFns
const cached = renderFns.cached || (renderFns.cached = [])
let tree = cached[index]
// if has already-rendered static tree and not inside v-for,
// we can reuse the same tree by doing a shallow clone.
if (tree && !isInFor) {
@ -25,8 +22,7 @@ export function renderStatic (
: cloneVNode(tree)
}
// otherwise, render a fresh tree.
tree = trees[index] =
options.staticRenderFns[index].call(this._renderProxy, null, this)
tree = cached[index] = renderFns[index].call(this._renderProxy, null, this)
markStatic(tree, `__static__${index}`, false)
return tree
}

9
src/core/instance/render.js

@ -17,9 +17,10 @@ import { isUpdatingChildComponent } from './lifecycle'
export function initRender (vm: Component) {
vm._vnode = null // the root of the child tree
const parentVnode = vm.$vnode = vm.$options._parentVnode // the placeholder node in parent tree
const options = vm.$options
const parentVnode = vm.$vnode = options._parentVnode // the placeholder node in parent tree
const renderContext = parentVnode && parentVnode.context
vm.$slots = resolveSlots(vm.$options._renderChildren, renderContext)
vm.$slots = resolveSlots(options._renderChildren, renderContext)
vm.$scopedSlots = emptyObject
// bind the createElement fn to this instance
// so that we get proper render context inside it.
@ -39,12 +40,12 @@ export function initRender (vm: Component) {
defineReactive(vm, '$attrs', parentData && parentData.attrs || emptyObject, () => {
!isUpdatingChildComponent && warn(`$attrs is readonly.`, vm)
}, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners || emptyObject, () => {
defineReactive(vm, '$listeners', options._parentListeners || emptyObject, () => {
!isUpdatingChildComponent && warn(`$listeners is readonly.`, vm)
}, true)
} else {
defineReactive(vm, '$attrs', parentData && parentData.attrs || emptyObject, null, true)
defineReactive(vm, '$listeners', vm.$options._parentListeners || emptyObject, null, true)
defineReactive(vm, '$listeners', options._parentListeners || emptyObject, null, true)
}
}

4
src/core/vdom/create-functional-component.js

@ -38,9 +38,7 @@ function FunctionalRenderContext (
// support for compiled functional template
if (isCompiled) {
// exposing constructor and $options for renderStatic() because it needs
// to cache the rendered trees on shared options
this.constructor = Ctor
// exposing $options for renderStatic()
this.$options = options
// pre-resolve slots for renderSlot()
this.$slots = this.slots()

Loading…
Cancel
Save