Skip to content

Commit

Permalink
fix(Transition): Reset URL to current state after aborted transition
Browse files Browse the repository at this point in the history
If the user changd the url, we started a transition to a state, and then cancelled, the url remained set to the cancelled transition's tostate's url.
closes #2611
  • Loading branch information
christopherthielen committed Mar 27, 2016
1 parent 4b6d56f commit 3a1308b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/state/hooks/transitionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {TargetState} from "../targetState";
import {ViewHooks} from "./viewHooks";
import {EnterExitHooks} from "./enterExitHooks";
import {ResolveHooks} from "./resolveHooks";
import {UrlRouter} from "../../url/urlRouter";

/**
* This class:
Expand Down Expand Up @@ -41,7 +42,7 @@ export class TransitionManager {
constructor(
private transition: Transition,
private $transitions,
private $urlRouter,
private $urlRouter: UrlRouter,
private $view, // service
private $state: StateService,
private $stateParams, // service/obj
Expand Down Expand Up @@ -108,6 +109,10 @@ export class TransitionManager {
if (error.type === RejectType.SUPERSEDED && error.redirected && error.detail instanceof TargetState) {
return this._redirectMgr(transition.redirect(error.detail)).runTransition();
}

if (error.type === RejectType.ABORTED) {
this.$urlRouter.update();
}
}

this.$transitions.defaultErrorHandler()(error);
Expand Down
2 changes: 1 addition & 1 deletion src/url/urlRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class UrlRouter {
return this.listener = this.listener || $location.onChange(evt => update(this.urlRouterProvider.rules, this.urlRouterProvider.otherwiseFn, evt));
}

update(read) {
update(read?) {
if (read) {
this.location = $location.url();
return;
Expand Down
18 changes: 14 additions & 4 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2095,7 +2095,7 @@ describe('otherwise and state redirects', function() {
});


describe('hook redirects', function() {
describe('transition hook', function() {
var log, resolvelog;
beforeEach(module(function ($stateProvider, $urlRouterProvider) {
log = resolvelog = "";
Expand All @@ -2118,7 +2118,7 @@ describe('hook redirects', function() {
}));

// Test for #2455
it("from .otherwise() should go to the redirect-to target state and url", inject(function($transitions, $q, $state, $location) {
it("redirects from .otherwise() should go to the redirect-to target state and url", inject(function($transitions, $q, $state, $location) {
$transitions.onBefore({ to: 'home' }, function() {
return $state.target('loginPage', {}, { location: true });
});
Expand All @@ -2128,7 +2128,7 @@ describe('hook redirects', function() {
}));

// Test for #2537
it("should be able to change option.reload", inject(function($transitions, $q, $state, $trace) {
it("redirects should be able to change option.reload", inject(function($transitions, $q, $state, $trace) {
var count = 0;
$q.flush();
expect($state.current.name).toBe("home");
Expand All @@ -2151,7 +2151,7 @@ describe('hook redirects', function() {
}));

// Test for #2539
it("should re-resolve when reloading during a redirect", inject(function($transitions, $q, $state, $trace) {
it("redirects should re-resolve when reloading during a redirect", inject(function($transitions, $q, $state, $trace) {
var count = 0;
$q.flush();

Expand All @@ -2173,4 +2173,14 @@ describe('hook redirects', function() {
expect($state.current.name).toBe("home");
expect(resolvelog).toBe("fooResolve;fooResolve;");
}));

// Test for #2611
it("aborts should reset the URL to the prevous state's", inject(function($transitions, $q, $state, $location) {
$q.flush();
$transitions.onStart({ to: 'home.foo' }, function() { return false; });
$location.path('/home/foo'); $q.flush();
expect($state.current.name).toBe("home");
expect($location.path()).toBe('/home');
}));

});

0 comments on commit 3a1308b

Please sign in to comment.