Skip to content

Commit 6ac43c7

Browse files
committed
strategy works now with child routes
1 parent 80603e9 commit 6ac43c7

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

app/app.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ let APP_ROUTES: Routes = [
1717
component: HomeComponent
1818
},
1919

20+
2021
{
2122
path: 'flight-booking',
2223
loadChildren: './flight-booking/flight-booking.module#FlightBookingModule',
2324
// canLoad: [AuthLoadGuard]
2425
},
25-
2626
{
2727
path: 'bookings',
2828
component: BookingsComponent
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
import {RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle} from "@angular/router";
1+
import {RouteReuseStrategy, DefaultUrlSerializer, ActivatedRouteSnapshot, DetachedRouteHandle} from "@angular/router";
22

33
export class CustomReuseStrategy implements RouteReuseStrategy {
44

55
handlers: {[key: string]: DetachedRouteHandle} = {};
66

7+
calcKey(route: ActivatedRouteSnapshot) {
8+
let next = route;
9+
let url = "";
10+
while(next) {
11+
if (next.url) {
12+
url = next.url.join('/');
13+
}
14+
next = next.firstChild;
15+
}
16+
console.debug('url', url);
17+
return url;
18+
}
19+
720
shouldDetach(route: ActivatedRouteSnapshot): boolean {
8-
console.debug('CustomReuseStrategy:shouldDetach', route);
21+
//console.debug('CustomReuseStrategy:shouldDetach', route);
922
return true;
1023
}
1124

1225
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
13-
console.debug('CustomReuseStrategy:store', route, handle);
14-
this.handlers[route.routeConfig.path] = handle;
26+
//console.debug('CustomReuseStrategy:store', route, handle);
27+
this.handlers[this.calcKey(route)] = handle;
28+
1529
}
1630

1731
shouldAttach(route: ActivatedRouteSnapshot): boolean {
18-
console.debug('CustomReuseStrategy:shouldAttach', route);
19-
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
32+
//console.debug('CustomReuseStrategy:shouldAttach', route);
33+
return !!route.routeConfig && !!this.handlers[this.calcKey(route)];
2034
}
2135

2236
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
23-
console.debug('CustomReuseStrategy:retrieve', route);
37+
//console.debug('CustomReuseStrategy:retrieve', route);
2438
if (!route.routeConfig) return null;
25-
return this.handlers[route.routeConfig.path];
39+
return this.handlers[this.calcKey(route)];
2640
}
2741

2842
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
29-
console.debug('CustomReuseStrategy:shouldReuseRoute', future, curr);
30-
return future.routeConfig === curr.routeConfig;
43+
//console.debug('CustomReuseStrategy:shouldReuseRoute', future, curr);
44+
return this.calcKey(curr) === this.calcKey(future);
3145
}
3246

3347
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@
5959
"@angular/platform-browser-dynamic": "2.3.0-rc.0",
6060
"@angular/router": "3.3.0-rc.0",
6161
"@angular/upgrade": "2.3.0-rc.0",
62-
"angular-in-memory-web-api": "0.1.15",
6362
"angular2-oauth2": "1.3.10",
6463
"bootstrap": "3.3.7",
6564
"core-js": "^2.4.1",
6665
"ie-shim": "0.1.0",
6766
"reflect-metadata": "^0.1.8",
6867
"rxjs": "5.0.0-rc.4",
6968
"systemjs": "0.19.41",
70-
"zone.js": "^0.7.2"
69+
"zone.js": "^0.7.8"
7170
},
7271
"keywords": [
7372
"Angular2",

0 commit comments

Comments
 (0)