Skip to content

Commit

Permalink
feat(router config): ignoreUnknownRoutes option
Browse files Browse the repository at this point in the history
  • Loading branch information
mgraf1 committed Dec 9, 2017
1 parent 6912e0a commit 811959b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/app-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ export class AppRouter extends Router {
return this._createNavigationInstruction(url)
.then(instruction => this._queueInstruction(instruction))
.catch(error => {
logger.error(error);
restorePreviousLocation(this);
if (!this.ignoreUnknownRoutes) {
logger.error(error);
restorePreviousLocation(this);
}
});
}

Expand Down
7 changes: 6 additions & 1 deletion src/router-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class RouterConfiguration {
title: string;
unknownRouteConfig: any;
viewPortDefaults: any;
ignoreUnknownRoutes: boolean;

/**
* Adds a step to be run during the [[Router]]'s navigation pipeline.
Expand Down Expand Up @@ -94,7 +95,7 @@ export class RouterConfiguration {
/**
* Configures defaults to use for any view ports.
*
* @param viewPortConfig a view port configuration object to use as a
* @param viewPortConfig a view port configuration object to use as a
* default, of the form { viewPortName: { moduleId } }.
* @chainable
*/
Expand Down Expand Up @@ -177,6 +178,10 @@ export class RouterConfiguration {
router.useViewPortDefaults(this.viewPortDefaults);
}

if (this.ignoreUnknownRoutes) {
router.ignoreUnknownRoutes = this.ignoreUnknownRoutes;
}

router.options = this.options;

let pipelineSteps = this.pipelineSteps;
Expand Down
8 changes: 5 additions & 3 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Router {
history: History;
viewPorts: Object;
routes: RouteConfig[];
ignoreUnknownRoutes: boolean;

/**
* The [[Router]]'s current base URL, typically based on the [[Router.currentInstruction]].
Expand Down Expand Up @@ -112,6 +113,7 @@ export class Router {
this._configuredPromise = new Promise(resolve => {
this._resolveConfiguredPromise = resolve;
});
this.ignoreUnknownRoutes = false;
}

/**
Expand Down Expand Up @@ -186,7 +188,7 @@ export class Router {
*
* @param route The name of the route to use when generating the navigation location.
* @param params The route parameters to be used when populating the route pattern.
* @param options The navigation options. See [[History.NavigationOptions]] for all available options.
* @param options The navigation options. See [[History.NavigationOptions]] for all available options.
*/
navigateToRoute(route: string, params?: any, options?: any): boolean {
let path = this.generate(route, params);
Expand Down Expand Up @@ -385,9 +387,9 @@ export class Router {
}
}

/**
/**
* Sets the default configuration for the view ports. This specifies how to
* populate a view port for which no module is specified. The default is
* populate a view port for which no module is specified. The default is
* an empty view/view-model pair.
*/
useViewPortDefaults(viewPortDefaults: any) {
Expand Down
14 changes: 14 additions & 0 deletions test/app-router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ describe('app-router', () => {
.then(done);
});

it('does not restore previous location when route not found and unknown routes are ignored', (done) => {
spyOn(history, 'navigate');

router.history.previousLocation = 'prev';
router.ignoreUnknownRoutes = true;
router.loadUrl('next')
.then(result => {
expect(result).toBeFalsy();
expect(history.navigate).not.toHaveBeenCalledWith('#/prev', { trigger: false, replace: true });
})
.catch(result => expect(true).toBeFalsy('should have succeeded'))
.then(done);
});

it('navigate to fallback route when route not found and there is no previous location', (done) => {
spyOn(history, 'navigate');

Expand Down

0 comments on commit 811959b

Please sign in to comment.