@@ -103,24 +103,31 @@ exports.activate = function (view, transition, cb) {
103103 return
104104 }
105105
106- var Component = handler . component
106+ var Component = view . Component = handler . component
107107 var activateHook = util . getRouteConfig ( Component , 'activate' )
108108 var dataHook = util . getRouteConfig ( Component , 'data' )
109109 var waitForData = util . getRouteConfig ( Component , 'waitForData' )
110110
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 )
123120 }
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 ( ) {
124131 var router = transition . router
125132 if ( router . _rendered || router . _transitionOnLoad ) {
126133 view . transition ( component )
@@ -132,18 +139,28 @@ exports.activate = function (view, transition, cb) {
132139 cb && cb ( )
133140 }
134141
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+ }
136148 if ( dataHook && waitForData ) {
137- loadData ( null , transition , dataHook , build )
149+ // wait until data loaded to insert
150+ loadData ( component , transition , dataHook , insert , cleanup )
138151 } else {
139- build ( )
152+ // load data and insert at the same time
153+ if ( dataHook ) {
154+ loadData ( component , transition , dataHook )
155+ }
156+ insert ( )
140157 }
141158 }
142159
143160 if ( activateHook ) {
144- transition . callHook ( activateHook , null , activate )
161+ transition . callHook ( activateHook , component , afterActivate , false , cleanup )
145162 } else {
146- activate ( )
163+ afterActivate ( )
147164 }
148165}
149166
@@ -169,22 +186,16 @@ exports.reuse = function (view, transition) {
169186 * @param {Transition } transition
170187 * @param {Function } hook
171188 * @param {Function } cb
189+ * @param {Function } cleanup
172190 */
173191
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
178194 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 ] )
188197 }
189- } )
198+ component . $loadingRouteData = false
199+ cb && cb ( data )
200+ } , false , cleanup )
190201}
0 commit comments