forked from vuejs/vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
56 lines (51 loc) · 1.47 KB
/
factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var _ = require('../util')
var compiler = require('../compiler')
var templateParser = require('../parsers/template')
var Fragment = require('./fragment')
var Cache = require('../cache')
var linkerCache = new Cache(5000)
/**
* A factory that can be used to create instances of a
* fragment. Caches the compiled linker if possible.
*
* @param {Vue} vm
* @param {Element|String} el
*/
function FragmentFactory (vm, el) {
this.vm = vm
var template
var isString = typeof el === 'string'
if (isString || _.isTemplate(el)) {
template = templateParser.parse(el, true)
} else {
template = document.createDocumentFragment()
template.appendChild(el)
}
this.template = template
// linker can be cached, but only for components
var linker
var cid = vm.constructor.cid
if (cid > 0) {
var cacheId = cid + (isString ? el : el.outerHTML)
linker = linkerCache.get(cacheId)
if (!linker) {
linker = compiler.compile(template, vm.$options, true)
linkerCache.put(cacheId, linker)
}
} else {
linker = compiler.compile(template, vm.$options, true)
}
this.linker = linker
}
/**
* Create a fragment instance with given host and scope.
*
* @param {Vue} host
* @param {Object} scope
* @param {Fragment} parentFrag
*/
FragmentFactory.prototype.create = function (host, scope, parentFrag) {
var frag = templateParser.clone(this.template)
return new Fragment(this.linker, this.vm, frag, host, scope, parentFrag)
}
module.exports = FragmentFactory