4
4
Inject , ComponentResolver , provide , ComponentFactoryResolver ,
5
5
NoComponentFactoryError } from '@angular/core' ;
6
6
7
- import { isBlank , isPresent } from '@angular/core/src/facade/lang' ;
7
+ import { isPresent } from '@angular/core/src/facade/lang' ;
8
8
9
- import { RouterOutletMap , ActivatedRoute , RouterOutlet , PRIMARY_OUTLET } from '@angular/router' ;
9
+ import { RouterOutletMap , ActivatedRoute , PRIMARY_OUTLET } from '@angular/router' ;
10
10
import { NSLocationStrategy } from "./ns-location-strategy" ;
11
11
import { DEVICE } from "../platform-providers" ;
12
12
import { Device } from "platform" ;
@@ -15,20 +15,35 @@ import {DetachedLoader} from "../common/detached-loader";
15
15
import { ViewUtil } from "../view-util" ;
16
16
import { topmost } from "ui/frame" ;
17
17
import { Page , NavigatedData } from "ui/page" ;
18
+ import { BehaviorSubject } from "rxjs" ;
18
19
19
20
interface CacheItem {
20
21
componentRef : ComponentRef < any > ;
22
+ reusedRoute : PageRoute ;
23
+ outletMap : RouterOutletMap ;
21
24
loaderRef ?: ComponentRef < any > ;
22
25
}
23
26
27
+ export class PageRoute {
28
+ activatedRoute : BehaviorSubject < ActivatedRoute > ;
29
+ constructor ( startRoute : ActivatedRoute ) {
30
+ this . activatedRoute = new BehaviorSubject ( startRoute ) ;
31
+ }
32
+ }
33
+
34
+
24
35
/**
25
36
* Reference Cache
26
37
*/
27
38
class RefCache {
28
39
private cache : Array < CacheItem > = new Array < CacheItem > ( ) ;
29
40
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 } ) ;
32
47
}
33
48
34
49
public pop ( ) : CacheItem {
@@ -125,23 +140,27 @@ export class PageRouterOutlet {
125
140
this . currentActivatedRoute = activatedRoute ;
126
141
127
142
if ( this . locationStrategy . isPageNavigatingBack ( ) ) {
128
- this . activateOnGoBack ( activatedRoute , providers ) ;
143
+ this . activateOnGoBack ( activatedRoute , providers , outletMap ) ;
129
144
} else {
130
- this . activateOnGoForward ( activatedRoute , providers ) ;
145
+ this . activateOnGoForward ( activatedRoute , providers , outletMap ) ;
131
146
}
132
147
}
133
148
134
149
private activateOnGoForward (
135
150
activatedRoute : ActivatedRoute ,
136
- providers : ResolvedReflectiveProvider [ ] ) : void {
151
+ providers : ResolvedReflectiveProvider [ ] ,
152
+ outletMap : RouterOutletMap ) : void {
137
153
const factory = this . getComponentFactory ( activatedRoute ) ;
138
154
155
+ const reusedRoute = new PageRoute ( activatedRoute ) ;
156
+ providers = [ ...providers , ...ReflectiveInjector . resolve ( [ { provide : PageRoute , useValue : reusedRoute } ] ) ] ;
157
+
139
158
if ( this . isInitalPage ) {
140
159
log ( "PageRouterOutlet.activate() inital page - just load component: " + activatedRoute . component ) ;
141
160
this . isInitalPage = false ;
142
161
const inj = ReflectiveInjector . fromResolvedProviders ( providers , this . containerRef . parentInjector ) ;
143
162
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 ) ;
145
164
146
165
} else {
147
166
log ( "PageRouterOutlet.activate() forward navigation - create detached loader in the loader container: " + activatedRoute . component ) ;
@@ -153,18 +172,28 @@ export class PageRouterOutlet {
153
172
154
173
this . currnetActivatedComp = loaderRef . instance . loadWithFactory ( factory ) ;
155
174
this . loadComponentInPage ( page , this . currnetActivatedComp ) ;
156
- this . refCache . push ( this . currnetActivatedComp , loaderRef ) ;
175
+ this . refCache . push ( this . currnetActivatedComp , reusedRoute , outletMap , loaderRef ) ;
157
176
}
158
177
}
159
178
160
179
private activateOnGoBack (
161
180
activatedRoute : ActivatedRoute ,
162
- providers : ResolvedReflectiveProvider [ ] ) : void {
181
+ providers : ResolvedReflectiveProvider [ ] ,
182
+ outletMap : RouterOutletMap ) : void {
163
183
log ( "PageRouterOutlet.activate() - Back naviation, so load from cache: " + activatedRoute . component ) ;
164
184
165
185
this . locationStrategy . finishBackPageNavigation ( ) ;
166
186
167
187
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
+
168
197
this . currnetActivatedComp = cacheItem . componentRef ;
169
198
}
170
199
0 commit comments