44 Inject , ComponentResolver , provide , ComponentFactoryResolver ,
55 NoComponentFactoryError } from '@angular/core' ;
66
7- import { isBlank , isPresent } from '@angular/core/src/facade/lang' ;
7+ import { isPresent } from '@angular/core/src/facade/lang' ;
88
9- import { RouterOutletMap , ActivatedRoute , RouterOutlet , PRIMARY_OUTLET } from '@angular/router' ;
9+ import { RouterOutletMap , ActivatedRoute , PRIMARY_OUTLET } from '@angular/router' ;
1010import { NSLocationStrategy } from "./ns-location-strategy" ;
1111import { DEVICE } from "../platform-providers" ;
1212import { Device } from "platform" ;
@@ -15,20 +15,35 @@ import {DetachedLoader} from "../common/detached-loader";
1515import { ViewUtil } from "../view-util" ;
1616import { topmost } from "ui/frame" ;
1717import { Page , NavigatedData } from "ui/page" ;
18+ import { BehaviorSubject } from "rxjs" ;
1819
1920interface CacheItem {
2021 componentRef : ComponentRef < any > ;
22+ reusedRoute : PageRoute ;
23+ outletMap : RouterOutletMap ;
2124 loaderRef ?: ComponentRef < any > ;
2225}
2326
27+ export class PageRoute {
28+ activatedRoute : BehaviorSubject < ActivatedRoute > ;
29+ constructor ( startRoute : ActivatedRoute ) {
30+ this . activatedRoute = new BehaviorSubject ( startRoute ) ;
31+ }
32+ }
33+
34+
2435/**
2536 * Reference Cache
2637 */
2738class RefCache {
2839 private cache : Array < CacheItem > = new Array < CacheItem > ( ) ;
2940
30- public push ( comp : ComponentRef < any > , loaderRef ?: ComponentRef < any > ) {
31- this . cache . push ( { componentRef : comp , loaderRef : loaderRef } ) ;
41+ public push (
42+ componentRef : ComponentRef < any > ,
43+ reusedRoute : PageRoute ,
44+ outletMap : RouterOutletMap ,
45+ loaderRef : ComponentRef < any > ) {
46+ this . cache . push ( { componentRef, reusedRoute, outletMap, loaderRef } ) ;
3247 }
3348
3449 public pop ( ) : CacheItem {
@@ -125,23 +140,27 @@ export class PageRouterOutlet {
125140 this . currentActivatedRoute = activatedRoute ;
126141
127142 if ( this . locationStrategy . isPageNavigatingBack ( ) ) {
128- this . activateOnGoBack ( activatedRoute , providers ) ;
143+ this . activateOnGoBack ( activatedRoute , providers , outletMap ) ;
129144 } else {
130- this . activateOnGoForward ( activatedRoute , providers ) ;
145+ this . activateOnGoForward ( activatedRoute , providers , outletMap ) ;
131146 }
132147 }
133148
134149 private activateOnGoForward (
135150 activatedRoute : ActivatedRoute ,
136- providers : ResolvedReflectiveProvider [ ] ) : void {
151+ providers : ResolvedReflectiveProvider [ ] ,
152+ outletMap : RouterOutletMap ) : void {
137153 const factory = this . getComponentFactory ( activatedRoute ) ;
138154
155+ const reusedRoute = new PageRoute ( activatedRoute ) ;
156+ providers = [ ...providers , ...ReflectiveInjector . resolve ( [ { provide : PageRoute , useValue : reusedRoute } ] ) ] ;
157+
139158 if ( this . isInitalPage ) {
140159 log ( "PageRouterOutlet.activate() inital page - just load component: " + activatedRoute . component ) ;
141160 this . isInitalPage = false ;
142161 const inj = ReflectiveInjector . fromResolvedProviders ( providers , this . containerRef . parentInjector ) ;
143162 this . currnetActivatedComp = this . containerRef . createComponent ( factory , this . containerRef . length , inj , [ ] ) ;
144- this . refCache . push ( this . currnetActivatedComp , null ) ;
163+ this . refCache . push ( this . currnetActivatedComp , reusedRoute , outletMap , null ) ;
145164
146165 } else {
147166 log ( "PageRouterOutlet.activate() forward navigation - create detached loader in the loader container: " + activatedRoute . component ) ;
@@ -153,18 +172,28 @@ export class PageRouterOutlet {
153172
154173 this . currnetActivatedComp = loaderRef . instance . loadWithFactory ( factory ) ;
155174 this . loadComponentInPage ( page , this . currnetActivatedComp ) ;
156- this . refCache . push ( this . currnetActivatedComp , loaderRef ) ;
175+ this . refCache . push ( this . currnetActivatedComp , reusedRoute , outletMap , loaderRef ) ;
157176 }
158177 }
159178
160179 private activateOnGoBack (
161180 activatedRoute : ActivatedRoute ,
162- providers : ResolvedReflectiveProvider [ ] ) : void {
181+ providers : ResolvedReflectiveProvider [ ] ,
182+ outletMap : RouterOutletMap ) : void {
163183 log ( "PageRouterOutlet.activate() - Back naviation, so load from cache: " + activatedRoute . component ) ;
164184
165185 this . locationStrategy . finishBackPageNavigation ( ) ;
166186
167187 let cacheItem = this . refCache . peek ( ) ;
188+ cacheItem . reusedRoute . activatedRoute . next ( activatedRoute ) ;
189+
190+ this . outletMap = cacheItem . outletMap ;
191+
192+ // HACK: Fill the outlet map provided by the router, with the outlets that we have cached.
193+ // This is needed beacuse the component is taken form the cache and not created - so it will not register
194+ // its child router-outlets to the newly created outlet map.
195+ Object . assign ( outletMap , cacheItem . outletMap ) ;
196+
168197 this . currnetActivatedComp = cacheItem . componentRef ;
169198 }
170199
0 commit comments