Skip to content

Commit

Permalink
fix(stateService): Process reload: in the StateService.target()
Browse files Browse the repository at this point in the history
closes #2537
  • Loading branch information
christopherthielen committed Mar 27, 2016
1 parent 199db79 commit 081da32
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/state/stateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ export class StateService {

/** Factory method for creating a TargetState */
target(identifier: StateOrName, params: ParamsOrArray, options: TransitionOptions = {}): TargetState {
// If we're reloading, find the state object to reload from
if (isObject(options.reload) && !(<any>options.reload).name)
throw new Error('Invalid reload state object');
options.reloadState = options.reload === true ? this.stateRegistry.root() : this.stateRegistry.matcher.find(<any> options.reload, options.relative);

if (options.reload && !options.reloadState)
throw new Error(`No such reload state '${(isString(options.reload) ? options.reload : (<any>options.reload).name)}'`);

let stateDefinition = this.stateRegistry.matcher.find(identifier, options.relative);
return new TargetState(identifier, stateDefinition, params, options);
};
Expand Down Expand Up @@ -274,14 +282,6 @@ export class StateService {
options = defaults(options, defaultTransOpts);
options = extend(options, { current: transQueue.peekTail.bind(transQueue)});

// If we're reloading, find the state object to reload from
if (isObject(options.reload) && !(<any>options.reload).name)
throw new Error('Invalid reload state object');
options.reloadState = options.reload === true ? this.$current.path[0] : this.stateRegistry.matcher.find(<any> options.reload, options.relative);

if (options.reload && !options.reloadState)
throw new Error(`No such reload state '${(isString(options.reload) ? options.reload : (<any>options.reload).name)}'`);

let ref: TargetState = this.target(to, toParams, options);
let latestTreeChanges: TreeChanges = treeChangesQueue.peekTail();
const rootPath = () => PathFactory.bindTransNodesToPath([new Node(this.stateRegistry.root())]);
Expand Down
32 changes: 31 additions & 1 deletion test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2096,13 +2096,20 @@ describe('otherwise and state redirects', function() {


describe('hook redirects from .otherwise()', function() {
var log;
beforeEach(module(function ($stateProvider, $urlRouterProvider) {
log = "";
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', { url: '/home', template: 'home' })
.state('home', { url: '/home', template: 'home', controller: function() { log += "homeCtrl;"; } })
.state('loginPage', { url: '/login', template: 'login' });
}));

beforeEach(inject(function($compile, $rootScope) {
var $scope = $rootScope.$new();
$compile('<div><ui-view></ui-view></div>')($scope);
}));

// Test for #2455
it("should go to the redirect-to target state and url", inject(function($transitions, $q, $state, $location) {
$transitions.onBefore({ to: 'home' }, function() {
Expand All @@ -2112,4 +2119,27 @@ describe('hook redirects from .otherwise()', function() {
expect($state.current.name).toBe("loginPage");
expect($location.path()).toBe('/login');
}));

// Test for #2537
it("should be able to change option.reload", inject(function($transitions, $q, $state, $trace) {
var count = 0;
$q.flush();
expect($state.current.name).toBe("home");
expect(log).toBe("homeCtrl;");

$state.go('.'); $q.flush();
expect(log).toBe("homeCtrl;");

$transitions.onBefore({ to: 'home' }, function($state, $transition$) {
var options = $transition$.options();
if (!options.reload && count++ < 2) {
return $state.target($transition$.to(), $transition$.params("to"), extend({}, options, {reload: true}));
}
});

$state.go('.'); $q.flush();

expect($state.current.name).toBe("home");
expect(log).toBe("homeCtrl;homeCtrl;");
}));
});

0 comments on commit 081da32

Please sign in to comment.