Skip to content

Commit 614b368

Browse files
committed
fix vuejs#636 and v-ref for real
1 parent df5f640 commit 614b368

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

src/directives/component.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ module.exports = {
3434
// if static, build right now.
3535
if (!this._isDynamicLiteral) {
3636
this.resolveCtor(this.expression)
37-
this.childVM = this.build()
38-
this.childVM.$before(this.ref)
37+
var child = this.build()
38+
child.$before(this.ref)
39+
this.setCurrent(child)
3940
} else {
4041
// check dynamic component params
4142
this.readyEvent = this._checkParam('wait-for')
@@ -85,10 +86,6 @@ module.exports = {
8586
if (this.keepAlive) {
8687
this.cache[this.ctorId] = child
8788
}
88-
var refID = child._refID || this.refID
89-
if (refID) {
90-
vm.$[refID] = child
91-
}
9289
return child
9390
}
9491
},
@@ -100,10 +97,6 @@ module.exports = {
10097

10198
unbuild: function () {
10299
var child = this.childVM
103-
var refID = (child && child._refID) || this.refID
104-
if (refID) {
105-
this.vm.$[refID] = null
106-
}
107100
if (!child || this.keepAlive) {
108101
return
109102
}
@@ -120,8 +113,7 @@ module.exports = {
120113
* @param {Function} cb
121114
*/
122115

123-
removeCurrent: function (cb) {
124-
var child = this.childVM
116+
remove: function (child, cb) {
125117
var keepAlive = this.keepAlive
126118
if (child) {
127119
child.$remove(function () {
@@ -142,8 +134,8 @@ module.exports = {
142134
if (!value) {
143135
// just destroy and remove current
144136
this.unbuild()
145-
this.removeCurrent()
146-
this.childVM = null
137+
this.remove(this.childVM)
138+
this.unsetCurrent()
147139
} else {
148140
this.resolveCtor(value)
149141
this.unbuild()
@@ -168,23 +160,48 @@ module.exports = {
168160

169161
swapTo: function (target) {
170162
var self = this
163+
var current = this.childVM
164+
this.unsetCurrent()
165+
this.setCurrent(target)
171166
switch (self.transMode) {
172167
case 'in-out':
173168
target.$before(self.ref, function () {
174-
self.removeCurrent()
175-
self.childVM = target
169+
self.remove(current)
176170
})
177171
break
178172
case 'out-in':
179-
self.removeCurrent(function () {
173+
self.remove(current, function () {
180174
target.$before(self.ref)
181-
self.childVM = target
182175
})
183176
break
184177
default:
185-
self.removeCurrent()
178+
self.remove(current)
186179
target.$before(self.ref)
187-
self.childVM = target
180+
}
181+
},
182+
183+
/**
184+
* Set childVM and parent ref
185+
*/
186+
187+
setCurrent: function (child) {
188+
this.childVM = child
189+
var refID = child._refID || this.refID
190+
if (refID) {
191+
this.vm.$[refID] = child
192+
}
193+
},
194+
195+
/**
196+
* Unset childVM and parent ref
197+
*/
198+
199+
unsetCurrent: function () {
200+
var child = this.childVM
201+
this.childVM = null
202+
var refID = (child && child._refID) || this.refID
203+
if (refID) {
204+
this.vm.$[refID] = null
188205
}
189206
},
190207

0 commit comments

Comments
 (0)