@@ -103,24 +103,31 @@ exports.activate = function (view, transition, cb) {
103
103
return
104
104
}
105
105
106
- var Component = handler . component
106
+ var Component = view . Component = handler . component
107
107
var activateHook = util . getRouteConfig ( Component , 'activate' )
108
108
var dataHook = util . getRouteConfig ( Component , 'data' )
109
109
var waitForData = util . getRouteConfig ( Component , 'waitForData' )
110
110
111
- var build = function ( data ) {
112
- view . unbuild ( true )
113
- view . Component = Component
114
- var shouldLoadData = dataHook && ! waitForData
115
- var component = view . build ( {
116
- data : data ,
117
- _meta : {
118
- $loadingRouteData : shouldLoadData
119
- }
120
- } )
121
- if ( shouldLoadData ) {
122
- loadData ( component , transition , dataHook )
111
+ // unbuild current component. this step also destroys
112
+ // and removes all nested child views.
113
+ view . unbuild ( true )
114
+ // build the new component. this will also create the
115
+ // direct child view of the current one. it will register
116
+ // itself as view.childView.
117
+ var component = view . build ( {
118
+ _meta : {
119
+ $loadingRouteData : ! ! ( dataHook && ! waitForData )
123
120
}
121
+ } )
122
+
123
+ // cleanup the component in case the transition is aborted
124
+ // before the component is ever inserted.
125
+ var cleanup = function ( ) {
126
+ component . $destroy ( )
127
+ }
128
+
129
+ // actually insert the component and trigger transition
130
+ var insert = function ( ) {
124
131
var router = transition . router
125
132
if ( router . _rendered || router . _transitionOnLoad ) {
126
133
view . transition ( component )
@@ -132,18 +139,28 @@ exports.activate = function (view, transition, cb) {
132
139
cb && cb ( )
133
140
}
134
141
135
- var activate = function ( ) {
142
+ // called after activation hook is resolved
143
+ var afterActivate = function ( ) {
144
+ // activate the child view
145
+ if ( view . childView ) {
146
+ exports . activate ( view . childView , transition )
147
+ }
136
148
if ( dataHook && waitForData ) {
137
- loadData ( null , transition , dataHook , build )
149
+ // wait until data loaded to insert
150
+ loadData ( component , transition , dataHook , insert , cleanup )
138
151
} else {
139
- build ( )
152
+ // load data and insert at the same time
153
+ if ( dataHook ) {
154
+ loadData ( component , transition , dataHook )
155
+ }
156
+ insert ( )
140
157
}
141
158
}
142
159
143
160
if ( activateHook ) {
144
- transition . callHook ( activateHook , null , activate )
161
+ transition . callHook ( activateHook , component , afterActivate , false , cleanup )
145
162
} else {
146
- activate ( )
163
+ afterActivate ( )
147
164
}
148
165
}
149
166
@@ -169,22 +186,16 @@ exports.reuse = function (view, transition) {
169
186
* @param {Transition } transition
170
187
* @param {Function } hook
171
188
* @param {Function } cb
189
+ * @param {Function } cleanup
172
190
*/
173
191
174
- function loadData ( component , transition , hook , cb ) {
175
- if ( component ) {
176
- component . $loadingRouteData = true
177
- }
192
+ function loadData ( component , transition , hook , cb , cleanup ) {
193
+ component . $loadingRouteData = true
178
194
transition . callHook ( hook , component , function ( data ) {
179
- if ( component ) {
180
- if ( data ) {
181
- for ( var key in data ) {
182
- component . $set ( key , data [ key ] )
183
- }
184
- }
185
- component . $loadingRouteData = false
186
- } else {
187
- cb ( data )
195
+ for ( var key in data ) {
196
+ component . $set ( key , data [ key ] )
188
197
}
189
- } )
198
+ component . $loadingRouteData = false
199
+ cb && cb ( data )
200
+ } , false , cleanup )
190
201
}
0 commit comments