@@ -34,8 +34,9 @@ module.exports = {
34
34
// if static, build right now.
35
35
if ( ! this . _isDynamicLiteral ) {
36
36
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 )
39
40
} else {
40
41
// check dynamic component params
41
42
this . readyEvent = this . _checkParam ( 'wait-for' )
@@ -85,10 +86,6 @@ module.exports = {
85
86
if ( this . keepAlive ) {
86
87
this . cache [ this . ctorId ] = child
87
88
}
88
- var refID = child . _refID || this . refID
89
- if ( refID ) {
90
- vm . $ [ refID ] = child
91
- }
92
89
return child
93
90
}
94
91
} ,
@@ -100,10 +97,6 @@ module.exports = {
100
97
101
98
unbuild : function ( ) {
102
99
var child = this . childVM
103
- var refID = ( child && child . _refID ) || this . refID
104
- if ( refID ) {
105
- this . vm . $ [ refID ] = null
106
- }
107
100
if ( ! child || this . keepAlive ) {
108
101
return
109
102
}
@@ -120,8 +113,7 @@ module.exports = {
120
113
* @param {Function } cb
121
114
*/
122
115
123
- removeCurrent : function ( cb ) {
124
- var child = this . childVM
116
+ remove : function ( child , cb ) {
125
117
var keepAlive = this . keepAlive
126
118
if ( child ) {
127
119
child . $remove ( function ( ) {
@@ -142,8 +134,8 @@ module.exports = {
142
134
if ( ! value ) {
143
135
// just destroy and remove current
144
136
this . unbuild ( )
145
- this . removeCurrent ( )
146
- this . childVM = null
137
+ this . remove ( this . childVM )
138
+ this . unsetCurrent ( )
147
139
} else {
148
140
this . resolveCtor ( value )
149
141
this . unbuild ( )
@@ -168,23 +160,48 @@ module.exports = {
168
160
169
161
swapTo : function ( target ) {
170
162
var self = this
163
+ var current = this . childVM
164
+ this . unsetCurrent ( )
165
+ this . setCurrent ( target )
171
166
switch ( self . transMode ) {
172
167
case 'in-out' :
173
168
target . $before ( self . ref , function ( ) {
174
- self . removeCurrent ( )
175
- self . childVM = target
169
+ self . remove ( current )
176
170
} )
177
171
break
178
172
case 'out-in' :
179
- self . removeCurrent ( function ( ) {
173
+ self . remove ( current , function ( ) {
180
174
target . $before ( self . ref )
181
- self . childVM = target
182
175
} )
183
176
break
184
177
default :
185
- self . removeCurrent ( )
178
+ self . remove ( current )
186
179
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
188
205
}
189
206
} ,
190
207
0 commit comments