bb214cfc920a474bb243f0eef7d5b29a702e16a3ae126f55fb81e3e202c5675ad6f7dfeb32f4531f91980e9a7eaec6508bf1f6349ad49bb40856958477cdf9 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* @flow */
  2. /**
  3. * Runtime helper for rendering static trees.
  4. */
  5. export function renderStatic (
  6. index: number,
  7. isInFor: boolean
  8. ): VNode | Array<VNode> {
  9. const cached = this._staticTrees || (this._staticTrees = [])
  10. let tree = cached[index]
  11. // if has already-rendered static tree and not inside v-for,
  12. // we can reuse the same tree.
  13. if (tree && !isInFor) {
  14. return tree
  15. }
  16. // otherwise, render a fresh tree.
  17. tree = cached[index] = this.$options.staticRenderFns[index].call(
  18. this._renderProxy,
  19. null,
  20. this // for render fns generated for functional component templates
  21. )
  22. markStatic(tree, `__static__${index}`, false)
  23. return tree
  24. }
  25. /**
  26. * Runtime helper for v-once.
  27. * Effectively it means marking the node as static with a unique key.
  28. */
  29. export function markOnce (
  30. tree: VNode | Array<VNode>,
  31. index: number,
  32. key: string
  33. ) {
  34. markStatic(tree, `__once__${index}${key ? `_${key}` : ``}`, true)
  35. return tree
  36. }
  37. function markStatic (
  38. tree: VNode | Array<VNode>,
  39. key: string,
  40. isOnce: boolean
  41. ) {
  42. if (Array.isArray(tree)) {
  43. for (let i = 0; i < tree.length; i++) {
  44. if (tree[i] && typeof tree[i] !== 'string') {
  45. markStaticNode(tree[i], `${key}_${i}`, isOnce)
  46. }
  47. }
  48. } else {
  49. markStaticNode(tree, key, isOnce)
  50. }
  51. }
  52. function markStaticNode (node, key, isOnce) {
  53. node.isStatic = true
  54. node.key = key
  55. node.isOnce = isOnce
  56. }