Skip to content

Commit 081da32

Browse files
fix(stateService): Process reload: in the StateService.target()
closes #2537
1 parent 199db79 commit 081da32

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/state/stateService.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ export class StateService {
227227

228228
/** Factory method for creating a TargetState */
229229
target(identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions = {}): TargetState {
230+
// If we're reloading, find the state object to reload from
231+
if (isObject(options.reload) && !(<any>options.reload).name)
232+
throw new Error('Invalid reload state object');
233+
options.reloadState = options.reload === true ? this.stateRegistry.root() : this.stateRegistry.matcher.find(<any> options.reload, options.relative);
234+
235+
if (options.reload && !options.reloadState)
236+
throw new Error(`No such reload state '${(isString(options.reload) ? options.reload : (<any>options.reload).name)}'`);
237+
230238
let stateDefinition = this.stateRegistry.matcher.find(identifier, options.relative);
231239
return new TargetState(identifier, stateDefinition, params, options);
232240
};
@@ -274,14 +282,6 @@ export class StateService {
274282
options = defaults(options, defaultTransOpts);
275283
options = extend(options, { current: transQueue.peekTail.bind(transQueue)});
276284

277-
// If we're reloading, find the state object to reload from
278-
if (isObject(options.reload) && !(<any>options.reload).name)
279-
throw new Error('Invalid reload state object');
280-
options.reloadState = options.reload === true ? this.$current.path[0] : this.stateRegistry.matcher.find(<any> options.reload, options.relative);
281-
282-
if (options.reload && !options.reloadState)
283-
throw new Error(`No such reload state '${(isString(options.reload) ? options.reload : (<any>options.reload).name)}'`);
284-
285285
let ref: TargetState = this.target(to, toParams, options);
286286
let latestTreeChanges: TreeChanges = treeChangesQueue.peekTail();
287287
const rootPath = () => PathFactory.bindTransNodesToPath([new Node(this.stateRegistry.root())]);

test/stateSpec.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -2096,13 +2096,20 @@ describe('otherwise and state redirects', function() {
20962096

20972097

20982098
describe('hook redirects from .otherwise()', function() {
2099+
var log;
20992100
beforeEach(module(function ($stateProvider, $urlRouterProvider) {
2101+
log = "";
21002102
$urlRouterProvider.otherwise('/home');
21012103
$stateProvider
2102-
.state('home', { url: '/home', template: 'home' })
2104+
.state('home', { url: '/home', template: 'home', controller: function() { log += "homeCtrl;"; } })
21032105
.state('loginPage', { url: '/login', template: 'login' });
21042106
}));
21052107

2108+
beforeEach(inject(function($compile, $rootScope) {
2109+
var $scope = $rootScope.$new();
2110+
$compile('<div><ui-view></ui-view></div>')($scope);
2111+
}));
2112+
21062113
// Test for #2455
21072114
it("should go to the redirect-to target state and url", inject(function($transitions, $q, $state, $location) {
21082115
$transitions.onBefore({ to: 'home' }, function() {
@@ -2112,4 +2119,27 @@ describe('hook redirects from .otherwise()', function() {
21122119
expect($state.current.name).toBe("loginPage");
21132120
expect($location.path()).toBe('/login');
21142121
}));
2122+
2123+
// Test for #2537
2124+
it("should be able to change option.reload", inject(function($transitions, $q, $state, $trace) {
2125+
var count = 0;
2126+
$q.flush();
2127+
expect($state.current.name).toBe("home");
2128+
expect(log).toBe("homeCtrl;");
2129+
2130+
$state.go('.'); $q.flush();
2131+
expect(log).toBe("homeCtrl;");
2132+
2133+
$transitions.onBefore({ to: 'home' }, function($state, $transition$) {
2134+
var options = $transition$.options();
2135+
if (!options.reload && count++ < 2) {
2136+
return $state.target($transition$.to(), $transition$.params("to"), extend({}, options, {reload: true}));
2137+
}
2138+
});
2139+
2140+
$state.go('.'); $q.flush();
2141+
2142+
expect($state.current.name).toBe("home");
2143+
expect(log).toBe("homeCtrl;homeCtrl;");
2144+
}));
21152145
});

0 commit comments

Comments
 (0)