Skip to content

Commit 727f145

Browse files
committed
support dynamic transitions
1 parent 5288d7f commit 727f145

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/directives/transition.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ module.exports = {
44
isLiteral: true,
55

66
bind: function () {
7+
if (!this._isDynamicLiteral) {
8+
this.update(this.expression)
9+
}
10+
},
11+
12+
update: function (id) {
13+
var vm = this.el.__vue__ || this.vm
714
this.el.__v_trans = {
8-
id: this.expression,
15+
id: id,
916
// resolve the custom transition functions now
10-
fns: this.vm.$options.transitions[this.expression]
17+
// so the transition module knows this is a
18+
// javascript transition without having to check
19+
// computed CSS.
20+
fns: vm.$options.transitions[id]
1121
}
1222
}
1323

test/unit/specs/directives/transition_spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var _ = require('../../../../src/util')
2+
var Vue = _.Vue
23
var def = require('../../../../src/directives/transition')
34

45
if (_.inBrowser) {
@@ -10,6 +11,7 @@ if (_.inBrowser) {
1011
el: document.createElement('div'),
1112
expression: 'test',
1213
bind: def.bind,
14+
update: def.update,
1315
vm: {
1416
$options: {
1517
transitions: {
@@ -23,5 +25,74 @@ if (_.inBrowser) {
2325
expect(dir.el.__v_trans.fns).toBe(fns)
2426
})
2527

28+
it('dynamic transitions', function (done) {
29+
var el = document.createElement('div')
30+
document.body.appendChild(el)
31+
var calls = {
32+
a: { enter: 0, leave: 0 },
33+
b: { enter: 0, leave: 0 }
34+
}
35+
var vm = new Vue({
36+
el: el,
37+
template: '<div v-show="show" v-transition="{{trans}}"></div>',
38+
data: {
39+
show: true,
40+
trans: 'a'
41+
},
42+
transitions: {
43+
a: {
44+
enter: function (el, done) {
45+
calls.a.enter++
46+
done()
47+
},
48+
leave: function (el, done) {
49+
calls.a.leave++
50+
done()
51+
}
52+
},
53+
b: {
54+
enter: function (el, done) {
55+
calls.b.enter++
56+
done()
57+
},
58+
leave: function (el, done) {
59+
calls.b.leave++
60+
done()
61+
}
62+
}
63+
}
64+
})
65+
66+
assertCalls(0, 0, 0, 0)
67+
vm.show = false
68+
_.nextTick(function () {
69+
assertCalls(0, 1, 0, 0)
70+
vm.trans = 'b'
71+
vm.show = true
72+
_.nextTick(function () {
73+
assertCalls(0, 1, 1, 0)
74+
vm.show = false
75+
_.nextTick(function () {
76+
assertCalls(0, 1, 1, 1)
77+
vm.trans = 'a'
78+
vm.show = true
79+
_.nextTick(function () {
80+
assertCalls(1, 1, 1, 1)
81+
done()
82+
})
83+
})
84+
})
85+
})
86+
87+
function assertCalls (a, b, c, d) {
88+
expect(el.firstChild.style.display).toBe(vm.show ? '' : 'none')
89+
expect(calls.a.enter).toBe(a)
90+
expect(calls.a.leave).toBe(b)
91+
expect(calls.b.enter).toBe(c)
92+
expect(calls.b.leave).toBe(d)
93+
}
94+
95+
})
96+
2697
})
2798
}

0 commit comments

Comments
 (0)