Skip to content

fix($state): populate default toParams in .transitionTo. closes #1396 #1424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
}
if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'");
if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState);
var defaultParams = {};
forEach(toState.path, function(state) { extend(defaultParams, state && state.self && state.self.params); });
toParams = extend(defaultParams, toParams);
to = toState;

var toPath = to.path;
Expand Down
33 changes: 28 additions & 5 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ describe('state', function () {
var A = { data: {} },
B = {},
C = {},
D = { params: { x: {}, y: {} } },
DD = { parent: D, params: { x: {}, y: {}, z: {} } },
D = { params: { x: null, y: null } },
DD = { parent: D, params: { x: null, y: null, z: null } },
E = { params: { i: {} } },
H = { data: {propA: 'propA', propB: 'propB'} },
HH = { parent: H },
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} },
RS = { url: '^/search?term', reloadOnSearch: false },
OPT = { url: '/opt/:param', params: { param: 100 } },
AppInjectable = {};

beforeEach(module(function ($stateProvider, $provide) {
Expand All @@ -46,6 +47,7 @@ describe('state', function () {
.state('H', H)
.state('HH', HH)
.state('HHH', HHH)
.state('OPT', OPT)
.state('RS', RS)

.state('home', { url: "/" })
Expand Down Expand Up @@ -101,8 +103,8 @@ describe('state', function () {
// State param inheritance tests. param1 is inherited by sub1 & sub2;
// param2 should not be transferred (unless explicitly set).
.state('root', { url: '^/root?param1' })
.state('root.sub1', {url: '/1?param2' })
.state('root.sub2', {url: '/2?param2' });
.state('root.sub1', {url: '/1?param2' });
$stateProvider.state('root.sub2', {url: '/2?param2' });

$provide.value('AppInjectable', AppInjectable);
}));
Expand Down Expand Up @@ -642,7 +644,7 @@ describe('state', function () {

it('contains the parameter values for the current state', inject(function ($state, $q) {
initStateTo(D, { x: 'x value', z: 'invalid value' });
expect($state.params).toEqual({ x: 'x value', y: undefined });
expect($state.params).toEqual({ x: 'x value', y: null });
}));
});

Expand Down Expand Up @@ -745,6 +747,7 @@ describe('state', function () {
'H',
'HH',
'HHH',
'OPT',
'RS',
'about',
'about.person',
Expand Down Expand Up @@ -786,6 +789,26 @@ describe('state', function () {
}));
});

describe('optional parameters', function() {
it("should be populated during transition, if unspecified", inject(function($state, $q) {
var stateParams;
$state.get("OPT").onEnter = function($stateParams) { stateParams = $stateParams; };
$state.go("OPT"); $q.flush();
expect($state.current.name).toBe("OPT");
expect($state.params).toEqual({ param: 100 });
expect(stateParams).toEqual({ param: 100 });
}));

it("should be populated during primary transition, if unspecified", inject(function($state, $q) {
var count = 0;
$state.get("OPT").onEnter = function($stateParams) { count++; };
$state.go("OPT"); $q.flush();
expect($state.current.name).toBe("OPT");
expect($state.params).toEqual({ param: 100 });
expect(count).toEqual(1);
}));
});

describe('url handling', function () {
it('should transition to the same state with different parameters', inject(function ($state, $rootScope, $location) {
$location.path("/about/bob");
Expand Down