github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/fragment/factory.js (about) 1 import { compile } from '../compiler/index' 2 import { isTemplate, getOuterHTML } from '../util/index' 3 import { parseTemplate, cloneNode } from '../parsers/template' 4 import Fragment from './fragment' 5 import Cache from '../cache' 6 7 const linkerCache = new Cache(5000) 8 9 /** 10 * A factory that can be used to create instances of a 11 * fragment. Caches the compiled linker if possible. 12 * 13 * @param {Vue} vm 14 * @param {Element|String} el 15 */ 16 17 export default function FragmentFactory (vm, el) { 18 this.vm = vm 19 var template 20 var isString = typeof el === 'string' 21 if (isString || isTemplate(el) && !el.hasAttribute('v-if')) { 22 template = parseTemplate(el, true) 23 } else { 24 template = document.createDocumentFragment() 25 template.appendChild(el) 26 } 27 this.template = template 28 // linker can be cached, but only for components 29 var linker 30 var cid = vm.constructor.cid 31 if (cid > 0) { 32 var cacheId = cid + (isString ? el : getOuterHTML(el)) 33 linker = linkerCache.get(cacheId) 34 if (!linker) { 35 linker = compile(template, vm.$options, true) 36 linkerCache.put(cacheId, linker) 37 } 38 } else { 39 linker = compile(template, vm.$options, true) 40 } 41 this.linker = linker 42 } 43 44 /** 45 * Create a fragment instance with given host and scope. 46 * 47 * @param {Vue} host 48 * @param {Object} scope 49 * @param {Fragment} parentFrag 50 */ 51 52 FragmentFactory.prototype.create = function (host, scope, parentFrag) { 53 var frag = cloneNode(this.template) 54 return new Fragment(this.linker, this.vm, frag, host, scope, parentFrag) 55 }