Skip to content

Commit e883afc

Browse files
fix(globals): Use shallow copy to update the globals.params / $state.params object
Previously, the `globals.params` object was updated using a deep copy. This causes problems when object-type parameter values aren't cloneable. Now, the parameter values are copied to the `globals.params` object using a shallow copy, i.e., `Object.assign`. This is consistent with parameter handling in the `PathNode` code also. If this commit is causing problems, it is likely that app code is mutating the existing object parameters and then calling `state.go` with the mutated parameters. Recommend making a clone of the params before calling `state.go`. Closes #74
1 parent 2f1ae9a commit e883afc

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/common/common.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ let w: any = typeof window === 'undefined' ? {} : window;
1616
let angular = w.angular || {};
1717
export const fromJson = angular.fromJson || JSON.parse.bind(JSON);
1818
export const toJson = angular.toJson || JSON.stringify.bind(JSON);
19-
export const copy = angular.copy || _copy;
2019
export const forEach = angular.forEach || _forEach;
2120
export const extend = Object.assign || _extend;
2221
export const equals = angular.equals || _equals;
@@ -528,11 +527,8 @@ export function tail<T>(arr: T[]): T {
528527

529528
/**
530529
* shallow copy from src to dest
531-
*
532-
* note: This is a shallow copy, while angular.copy is a deep copy.
533-
* ui-router uses `copy` only to make copies of state parameters.
534530
*/
535-
function _copy(src: Obj, dest: Obj) {
531+
export function copy(src: Obj, dest?: Obj) {
536532
if (dest) Object.keys(dest).forEach(key => delete dest[key]);
537533
if (!dest) dest = {};
538534
return extend(dest, src);

src/globals.ts

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {StateDeclaration} from "./state/interface";
77
import {StateObject} from "./state/stateObject";
88
import {Transition} from "./transition/transition";
99
import {Queue} from "./common/queue";
10-
import {TransitionService} from "./transition/transitionService";
11-
import {copy} from "./common/common";
1210
import { Disposable } from './interface';
1311

1412
/**

test/stateServiceSpec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,27 @@ describe('stateService', function () {
426426

427427
done();
428428
});
429+
430+
it('performs a shallow copy to $stateParams (for object parameters)', async (done) => {
431+
const x = { foo: '123' }, y = { bar: 'abc' };
432+
await $state.go('D', { x, y });
433+
434+
expect($state.params.x).toBe(x);
435+
expect($state.params.y).toBe(y);
436+
437+
done();
438+
});
439+
440+
it('updates the object reference in-place', async (done) => {
441+
const params = $state.params;
442+
const x = { foo: '123' };
443+
await $state.go('D', { x });
444+
445+
expect($state.params.x).toBe(x);
446+
expect(params).toBe($state.params);
447+
448+
done();
449+
});
429450
});
430451
});
431452

@@ -952,4 +973,4 @@ describe('stateService', function () {
952973
});
953974

954975
});
955-
});
976+
});

0 commit comments

Comments
 (0)