refactor(router): polishing

This commit is contained in:
Dzmitry Shylovich 2017-03-24 01:19:35 +03:00 committed by Victor Berchet
parent ea848f74af
commit fb1be83a1b
2 changed files with 99 additions and 102 deletions

View File

@ -293,7 +293,7 @@ class ApplyRedirects {
} }
if (route.loadChildren) { if (route.loadChildren) {
return mergeMap.call(runGuards(ngModule.injector, route), (shouldLoad: any) => { return mergeMap.call(runCanLoadGuard(ngModule.injector, route), (shouldLoad: boolean) => {
if (shouldLoad) { if (shouldLoad) {
return (<any>route)._loadedConfig ? return (<any>route)._loadedConfig ?
@ -396,12 +396,12 @@ class ApplyRedirects {
} }
} }
function runGuards(moduleInjector: Injector, route: Route): Observable<boolean> { function runCanLoadGuard(moduleInjector: Injector, route: Route): Observable<boolean> {
const canLoad = route.canLoad; const canLoad = route.canLoad;
if (!canLoad || canLoad.length === 0) return of (true); if (!canLoad || canLoad.length === 0) return of (true);
const obs = map.call(from(canLoad), (c: any) => { const obs = map.call(from(canLoad), (injectionToken: any) => {
const guard = moduleInjector.get(c); const guard = moduleInjector.get(injectionToken);
return wrapIntoObservable(guard.canLoad ? guard.canLoad(route) : guard(route)); return wrapIntoObservable(guard.canLoad ? guard.canLoad(route) : guard(route));
}); });

View File

@ -2075,7 +2075,6 @@ describe('Integration', () => {
}); });
describe('CanLoad', () => { describe('CanLoad', () => {
describe('should not load children when CanLoad returns false', () => {
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
providers: [ providers: [
@ -2093,7 +2092,7 @@ describe('Integration', () => {
}); });
}); });
it('works', it('should not load children when CanLoad returns false',
fakeAsync(inject( fakeAsync(inject(
[Router, Location, NgModuleFactoryLoader], [Router, Location, NgModuleFactoryLoader],
(router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => { (router: Router, location: Location, loader: SpyNgModuleFactoryLoader) => {
@ -2171,12 +2170,10 @@ describe('Integration', () => {
expectEvents(recordedEvents, [ expectEvents(recordedEvents, [
[NavigationStart, '/lazyFalse/loaded'], [NavigationCancel, '/lazyFalse/loaded'], [NavigationStart, '/lazyFalse/loaded'], [NavigationCancel, '/lazyFalse/loaded'],
[NavigationStart, '/blank'], [RoutesRecognized, '/blank'], [NavigationStart, '/blank'], [RoutesRecognized, '/blank'], [NavigationEnd, '/blank']
[NavigationEnd, '/blank']
]); ]);
}))); })));
}); });
});
describe('order', () => { describe('order', () => {