Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($route): implement resolveRedirectTo #14695

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented May 31, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature

What is the current behavior? (You can also link to an open issue here)
It is not possible to have a redirection gunction that is injectable and asynchronous (i.e. returns a promise).
See #5150.

What is the new behavior (if this is a feature change)?
resolveRedirectTo can be used to provide an injectable, possibly asynchronous redirection function.

Does this PR introduce a breaking change?
Yes (see below).

Please check if the PR fulfills these requirements

Other information:
resolveRedirectTo can be set to a function that will (eventually) return the new URL to redirect
to. The function supports dependency injection and should return the new URL either as a string or
as a promise that will be resolved to a string.

If the resolveRedirectTo function returns undefined or returns a promise that gets resolved to
undefined, no redirection will take place and the current route will be processed normally.

If the resolveRedirectTo function throws an error or the returned promise gets rejected, no
further processing will take place (e.g. no template fetched, no resolve functions run, no
controller instantiated) and a $routeChangeError will be broadcasted.

redirectTo takes precedence over resolveRedirectTo, so specifying both on the same route
definition, will cause the latter to be ignored.

Fixes #5150

BREAKING CHANGE:

Previously, if redirectTo was a function that threw an Error, execution was aborted without firing
a $routeChangeError event.
Now, if a redirectTo function throws an Error, a $routeChangeError event will be fired.

`resolveRedirectTo` can be set to a function that will (eventually) return the new URL to redirect
to. The function supports dependency injection and should return the new URL either as a string or
as a promise that will be resolved to a string.

If the `resolveRedirectTo` function returns `undefined` or returns a promise that gets resolved to
`undefined`, no redirection will take place and the current route will be processed normally.

If the `resolveRedirectTo` function throws an error or the returned promise gets rejected, no
further processing will take place (e.g. no template fetched, no `resolve` functions run, no
controller instantiated) and a `$routeChangeError` will be broadcasted.

`redirectTo` takes precedence over `resolveRedirectTo`, so specifying both on the same route
definition, will cause the latter to be ignored.

Fixes angular#5150

BREAKING CHANGE:

Previously, if `redirectTo` was a function that threw an Error, execution was aborted without firing
a `$routeChangeError` event.
Now, if a `redirectTo` function throws an Error, a `$routeChangeError` event will be fired.
@gkalpak
Copy link
Member Author

gkalpak commented May 31, 2016

We could avoid the BC, but the previous behavior feels like an oversight.


module(function($routeProvider) {
$routeProvider.when('/bar/:id/:subid/:subsubid', {templateUrl: 'bar.html'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that you removed this line because it is not used in the test, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right!

@petebacondarwin
Copy link
Contributor

A few nit-picks but otherwise LGTM for 1.6

- Fix typos.
- Reorder tests.
@gkalpak
Copy link
Member Author

gkalpak commented Jun 10, 2016

Thx for the review!
I'll wait for Travis and merge.

@gkalpak gkalpak closed this in e986565 Jun 10, 2016
@gkalpak gkalpak deleted the feat-ngRoute-injectable-async-redirectTo branch June 10, 2016 12:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make $routeProvider redirectTo injectable
3 participants