Skip to content

Commit 6b7f9d9

Browse files
committed
bring partials back
1 parent 0c78d63 commit 6b7f9d9

File tree

9 files changed

+84
-13
lines changed

9 files changed

+84
-13
lines changed

src/directives/if.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
this.invalid = true
2929
_.warn(
3030
'v-if="' + this.expression + '" cannot be ' +
31-
'used on an already mounted instance.'
31+
'used on an instance root element.'
3232
)
3333
}
3434
},
@@ -39,19 +39,19 @@ module.exports = {
3939
// avoid duplicate compiles, since update() can be
4040
// called with different truthy values
4141
if (!this.unlink) {
42-
this.compile()
42+
this.link(
43+
templateParser.clone(this.template),
44+
this.linker
45+
)
4346
}
4447
} else {
4548
this.teardown()
4649
}
4750
},
4851

49-
compile: function () {
52+
link: function (frag, linker) {
5053
var vm = this.vm
51-
var frag = templateParser.clone(this.template)
52-
// the linker is not guaranteed to be present because
53-
// this function might get called by v-partial
54-
this.unlink = this.linker(vm, frag)
54+
this.unlink = linker(vm, frag)
5555
transition.blockAppend(frag, this.end, vm)
5656
// call attached for all the child components created
5757
// during the compilation
@@ -124,4 +124,4 @@ function callDetach (child) {
124124
if (child._isAttached) {
125125
child._callHook('detached')
126126
}
127-
}
127+
}
File renamed without changes.

src/element-directives/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.content = require('./content')
2+
exports.partial = require('./partial')

src/element-directives/partial.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var _ = require('../util')
2+
var templateParser = require('../parsers/template')
3+
var textParser = require('../parsers/text')
4+
var compiler = require('../compiler')
5+
var Cache = require('../cache')
6+
var cache = new Cache(1000)
7+
8+
// v-partial reuses logic from v-if
9+
var vIf = require('../directives/if')
10+
11+
module.exports = {
12+
13+
link: vIf.link,
14+
teardown: vIf.teardown,
15+
getContainedComponents: vIf.getContainedComponents,
16+
17+
bind: function () {
18+
var el = this.el
19+
this.start = _.createAnchor('v-partial-start')
20+
this.end = _.createAnchor('v-partial-end')
21+
_.replace(el, this.end)
22+
_.before(this.start, this.end)
23+
var id = el.getAttribute('name')
24+
var tokens = textParser.parse(id)
25+
if (tokens) {
26+
// dynamic partial
27+
this.setupDynamic(tokens)
28+
} else {
29+
// static partial
30+
this.insert(id)
31+
}
32+
},
33+
34+
setupDynamic: function (tokens) {
35+
var self = this
36+
var exp = textParser.tokensToExp(tokens)
37+
this.unwatch = this.vm.$watch(exp, function (value) {
38+
self.teardown()
39+
self.insert(value)
40+
}, {
41+
immediate: true,
42+
user: false
43+
})
44+
},
45+
46+
insert: function (id) {
47+
var partial = this.vm.$options.partials[id]
48+
_.assertAsset(partial, 'partial', id)
49+
if (partial) {
50+
var frag = templateParser.parse(partial, true)
51+
var linker = this.compile(frag, partial)
52+
// this is provided by v-if
53+
this.link(frag, linker)
54+
}
55+
},
56+
57+
compile: function (frag, partial) {
58+
var hit = cache.get(partial)
59+
if (hit) return hit
60+
var linker = compiler.compile(frag, this.vm.$options, true)
61+
cache.put(partial, linker)
62+
return linker
63+
},
64+
65+
unbind: function () {
66+
if (this.unlink) this.unlink()
67+
if (this.unwatch) this.unwatch()
68+
}
69+
}

src/util/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ strats.directives =
149149
strats.filters =
150150
strats.transitions =
151151
strats.components =
152+
strats.partials =
152153
strats.elementDirectives = function (parentVal, childVal) {
153154
var res = Object.create(parentVal)
154155
return childVal

src/vue.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ extend(Vue, require('./api/global'))
3636

3737
Vue.options = {
3838
directives: require('./directives'),
39+
elementDirectives: require('./element-directives'),
3940
filters: require('./filters'),
4041
transitions: {},
4142
components: {},
42-
elementDirectives: {
43-
content: require('./compiler/content')
44-
}
43+
partials: {}
4544
}
4645

4746
/**
@@ -86,4 +85,4 @@ extend(p, require('./api/events'))
8685
extend(p, require('./api/child'))
8786
extend(p, require('./api/lifecycle'))
8887

89-
module.exports = _.Vue = Vue
88+
module.exports = _.Vue = Vue

test/unit/specs/directives/if_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ if (_.inBrowser) {
173173
var vm = new Vue({
174174
el: el
175175
})
176-
expect(hasWarned(_, 'already mounted instance')).toBe(true)
176+
expect(hasWarned(_, 'cannot be used on an instance root element')).toBe(true)
177177
})
178178

179179
it('call attach/detach for transcluded components', function (done) {

test/unit/specs/element-directives/partial_spec.js

Whitespace-only changes.

0 commit comments

Comments
 (0)