Skip to content

Commit 81b499b

Browse files
committed
make v-else work with v-show
1 parent 9648f01 commit 81b499b

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

src/compiler/compile.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,25 @@ function checkComponent (el, options, hasAttrs) {
488488
*/
489489

490490
function checkTerminalDirectives (el, options) {
491-
if (_.attr(el, 'pre') !== null ||
492-
el.hasAttribute(config.prefix + 'else')) {
491+
// skip v-pre
492+
if (_.attr(el, 'pre') !== null) {
493493
return skip
494494
}
495+
// skip v-else block, but only if following v-if
496+
if (el.hasAttribute(config.prefix + 'else')) {
497+
var prev = el.previousElementSibling
498+
if (prev && prev.hasAttribute(config.prefix + 'if')) {
499+
return skip
500+
}
501+
}
495502
var value, dirName
496503
for (var i = 0, l = terminalDirectives.length; i < l; i++) {
497504
dirName = terminalDirectives[i]
498-
if ((value = _.attr(el, dirName)) !== null) {
505+
/* eslint-disable no-cond-assign */
506+
if (value = el.getAttribute(config.prefix + dirName)) {
499507
return makeTerminalNodeLinkFn(el, dirName, value, options)
500508
}
509+
/* eslint-enable no-cond-assign */
501510
}
502511
}
503512

@@ -604,6 +613,12 @@ function compileDirectives (attrs, options) {
604613
dirName = name
605614
.slice(config.prefix.length)
606615
.replace(argRE, '')
616+
617+
// skip v-else (when used with v-show)
618+
if (dirName === 'else') {
619+
continue
620+
}
621+
607622
dirDef = resolveAsset(options, 'directives', dirName)
608623
if (process.env.NODE_ENV !== 'production') {
609624
_.assertAsset(dirDef, 'directive', dirName)

src/directives/show.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
var _ = require('../util')
12
var transition = require('../transition')
23

3-
module.exports = function (value) {
4-
var el = this.el
5-
transition.apply(el, value ? 1 : -1, function () {
6-
el.style.display = value ? '' : 'none'
7-
}, this.vm)
4+
module.exports = {
5+
6+
bind: function () {
7+
// check else block
8+
var next = this.el.nextElementSibling
9+
if (next && _.attr(next, 'else') !== null) {
10+
this.elseEl = next
11+
}
12+
},
13+
14+
update: function (value) {
15+
var el = this.el
16+
transition.apply(el, value ? 1 : -1, function () {
17+
el.style.display = value ? '' : 'none'
18+
}, this.vm)
19+
var elseEl = this.elseEl
20+
if (elseEl) {
21+
transition.apply(elseEl, value ? -1 : 1, function () {
22+
elseEl.style.display = value ? 'none' : ''
23+
}, this.vm)
24+
}
25+
}
826
}

test/unit/specs/directives/show_spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (_.inBrowser) {
1515
it('should work', function () {
1616
var dir = {
1717
el: el,
18-
update: def,
18+
update: def.update,
1919
vm: new Vue()
2020
}
2121
dir.update(false)
@@ -24,5 +24,27 @@ if (_.inBrowser) {
2424
expect(el.style.display).toBe('')
2525
expect(transition.apply).toHaveBeenCalled()
2626
})
27+
28+
it('should work with v-else', function (done) {
29+
var vm = new Vue({
30+
el: el,
31+
template:
32+
'<p v-show="ok">YES</p>' +
33+
'<p v-else>NO</p>',
34+
data: {
35+
ok: true
36+
}
37+
})
38+
expect(el.children[0].style.display).toBe('')
39+
expect(el.children[1].style.display).toBe('none')
40+
expect(transition.apply.calls.count()).toBe(2)
41+
vm.ok = false
42+
Vue.nextTick(function () {
43+
expect(el.children[0].style.display).toBe('none')
44+
expect(el.children[1].style.display).toBe('')
45+
expect(transition.apply.calls.count()).toBe(4)
46+
done()
47+
})
48+
})
2749
})
2850
}

0 commit comments

Comments
 (0)