|
|
@@ -0,0 +1,69 @@
|
|
|
+var _ = require('../util')
|
|
|
+var templateParser = require('../parsers/template')
|
|
|
+var textParser = require('../parsers/text')
|
|
|
+var compiler = require('../compiler')
|
|
|
+var Cache = require('../cache')
|
|
|
+var cache = new Cache(1000)
|
|
|
+
|
|
|
+// v-partial reuses logic from v-if
|
|
|
+var vIf = require('../directives/if')
|
|
|
+
|
|
|
+module.exports = {
|
|
|
+
|
|
|
+ link: vIf.link,
|
|
|
+ teardown: vIf.teardown,
|
|
|
+ getContainedComponents: vIf.getContainedComponents,
|
|
|
+
|
|
|
+ bind: function () {
|
|
|
+ var el = this.el
|
|
|
+ this.start = _.createAnchor('v-partial-start')
|
|
|
+ this.end = _.createAnchor('v-partial-end')
|
|
|
+ _.replace(el, this.end)
|
|
|
+ _.before(this.start, this.end)
|
|
|
+ var id = el.getAttribute('name')
|
|
|
+ var tokens = textParser.parse(id)
|
|
|
+ if (tokens) {
|
|
|
+ // dynamic partial
|
|
|
+ this.setupDynamic(tokens)
|
|
|
+ } else {
|
|
|
+ // static partial
|
|
|
+ this.insert(id)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ setupDynamic: function (tokens) {
|
|
|
+ var self = this
|
|
|
+ var exp = textParser.tokensToExp(tokens)
|
|
|
+ this.unwatch = this.vm.$watch(exp, function (value) {
|
|
|
+ self.teardown()
|
|
|
+ self.insert(value)
|
|
|
+ }, {
|
|
|
+ immediate: true,
|
|
|
+ user: false
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ insert: function (id) {
|
|
|
+ var partial = this.vm.$options.partials[id]
|
|
|
+ _.assertAsset(partial, 'partial', id)
|
|
|
+ if (partial) {
|
|
|
+ var frag = templateParser.parse(partial, true)
|
|
|
+ var linker = this.compile(frag, partial)
|
|
|
+ // this is provided by v-if
|
|
|
+ this.link(frag, linker)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ compile: function (frag, partial) {
|
|
|
+ var hit = cache.get(partial)
|
|
|
+ if (hit) return hit
|
|
|
+ var linker = compiler.compile(frag, this.vm.$options, true)
|
|
|
+ cache.put(partial, linker)
|
|
|
+ return linker
|
|
|
+ },
|
|
|
+
|
|
|
+ unbind: function () {
|
|
|
+ if (this.unlink) this.unlink()
|
|
|
+ if (this.unwatch) this.unwatch()
|
|
|
+ }
|
|
|
+}
|