Skip to content

Commit 0162212

Browse files
feat(Transition): Make Transition.params() immutable to avoid confusion about mutability
refactor(params): rename type.ts to paramtype.ts
1 parent 23e2b78 commit 0162212

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

src/params/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export * from "./interface";
1111
export * from "./param";
1212
export * from "./paramTypes";
1313
export * from "./stateParams";
14-
export * from "./type";
14+
export * from "./paramType";

src/params/interface.ts

+76-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module params
44
*/ /** for typedoc */
55

6-
import {ParamType} from "./type";
6+
import {ParamType} from "./paramType";
77

88
/**
99
* Parameter values
@@ -110,6 +110,10 @@ export interface ParamDeclaration {
110110
*
111111
* This defines a default value for the parameter.
112112
* If a parameter value is `undefined`, this default value will be used instead
113+
*
114+
* ---
115+
*
116+
* Default: `undefined`
113117
*/
114118
value?: any;
115119

@@ -123,6 +127,14 @@ export interface ParamDeclaration {
123127
* The type may be either one of the built in types, or a custom type that has been registered with the [[UrlMatcherFactory]].
124128
*
125129
* See [[ParamTypes]] for the list of built in types.
130+
*
131+
* ---
132+
*
133+
* Default:
134+
* - Path parameters (`/:fooParam`): `path`
135+
* - Query parameters (`?queryParam`): `query`
136+
* - Non-url parameters (`param: { foo: null }`): `any`
137+
*
126138
*/
127139
type: (string|ParamType);
128140

@@ -212,7 +224,7 @@ export interface ParamDeclaration {
212224
* $state.go('mystate', { myparam2: 'someOtherValue' });
213225
* ```
214226
*
215-
* If squash is not set, it uses the configured default squash policy. (See [[defaultSquashPolicy]]())
227+
* Default: If squash is not set, it uses the configured default squash policy. (See [[defaultSquashPolicy]]())
216228
*/
217229
squash: (boolean|string);
218230

@@ -258,6 +270,10 @@ export interface ParamDeclaration {
258270
* ---
259271
*
260272
* Note: this value overrides the `dynamic` value on a custom parameter type ([[ParamTypeDefinition.dynamic]]).
273+
*
274+
* ---
275+
*
276+
* Default: `false`
261277
*/
262278
dynamic: boolean;
263279

@@ -292,8 +308,45 @@ export interface ParamDeclaration {
292308
* It's generally safe to use a raw parameter at the end of a path, like '/product/:slug'.
293309
* However, beware of the characters you allow in your raw parameter values.
294310
* Avoid unencoded characters that could disrupt normal URL parsing, such as `?` and `#`.
311+
*
312+
* ---
313+
*
314+
* Default: `false`
295315
*/
296316
raw: boolean;
317+
318+
/**
319+
* Enables/disables inheriting of this parameter value
320+
*
321+
* When a transition is run with [[TransitionOptions.inherit]] set to `true`, the current param values are inherited.
322+
* Parameters values which have `inherit: false` will not be inherited.
323+
*
324+
* #### Example state :
325+
* ```js
326+
* var fooState = {
327+
* name: 'foo',
328+
* url: '/:fooId?mode&refresh',
329+
* params: {
330+
* refresh: { inherit: false }
331+
* }
332+
* }
333+
*
334+
* // Set fooId to 123
335+
* $state.go('fooState', { fooId: 1234, mode: 'list', refresh: true });
336+
* ```
337+
*
338+
* In the component:
339+
* `mode: 'list' is inherited, but refresh: true is not inherited.
340+
* // The param values are thus: `{ fooId: 4567, mode: 'list' }`
341+
* ```
342+
* <ui-sref="foo({ fooId: 4567 })">4567</ui-sref>
343+
* ```
344+
*
345+
* ---
346+
*
347+
* Default: `true`
348+
*/
349+
inherit: boolean;
297350
}
298351

299352
/** @internalapi */
@@ -520,5 +573,26 @@ export interface ParamTypeDefinition {
520573
* See: [[ParamDeclaration.raw]] for details
521574
*/
522575
raw: boolean;
576+
577+
/**
578+
* Enables/disables inheriting of parameter values (of this type)
579+
*
580+
* When a transition is run with [[TransitionOptions.inherit]] set to `true`, the current param values are inherited.
581+
* Param values whose type has `inherit: false` will not be inherited.
582+
*
583+
* The internal parameter type of `hash` has `inherit: false`.
584+
* This is used to disable inheriting of the hash value (`#`) on subsequent transitions.
585+
*
586+
* #### Example:
587+
* ```js
588+
* $state.go('home', { '#': 'inboxAnchor' });
589+
* ...
590+
* // "#" is not inherited.
591+
* // The value of the "#" parameter will be `null`
592+
* // The url's hash will be cleared.
593+
* $state.go('home.nest');
594+
* ```
595+
*/
596+
inherit: boolean;
523597
}
524598

src/params/param.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { prop, propEq } from "../common/hof";
77
import { isInjectable, isDefined, isString, isArray } from "../common/predicates";
88
import { RawParams, ParamDeclaration } from "../params/interface";
99
import { services } from "../common/coreservices";
10-
import { ParamType } from "./type";
10+
import { ParamType } from "./paramType";
1111
import { ParamTypes } from "./paramTypes";
1212
import { UrlMatcherFactory } from "../url/urlMatcherFactory";
1313

File renamed without changes.

src/params/paramTypes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fromJson, toJson, identity, equals, inherit, map, extend, pick } from "
66
import { isDefined, isNullOrUndefined } from "../common/predicates";
77
import { is } from "../common/hof";
88
import { services } from "../common/coreservices";
9-
import { ParamType } from "./type";
9+
import { ParamType } from "./paramType";
1010
import { ParamTypeDefinition } from "./interface";
1111

1212
/**

src/transition/transition.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export class Transition implements IHookRegistry {
250250
* Gets transition parameter values
251251
*
252252
* Returns the parameter values for a transition as key/value pairs.
253+
* This object is immutable.
253254
*
254255
* By default, returns the new parameter values (for the "to state").
255256
* To return the previous parameter values, supply `'from'` as the `pathname` argument.
@@ -262,7 +263,7 @@ export class Transition implements IHookRegistry {
262263
params(pathname?: string): any;
263264
params<T>(pathname?: string): T;
264265
params(pathname: string = "to") {
265-
return this._treeChanges[pathname].map(prop("paramValues")).reduce(mergeR, {});
266+
return Object.freeze(this._treeChanges[pathname].map(prop("paramValues")).reduce(mergeR, {}));
266267
}
267268

268269

src/url/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @module url
99
*/ /** */
1010
import { LocationConfig } from "../common/coreservices";
11-
import { ParamType } from "../params/type";
11+
import { ParamType } from "../params/paramType";
1212
import { Param } from "../params/param";
1313
import { UIRouter } from "../router";
1414
import { TargetState } from "../state/targetState";

src/url/urlMatcherFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Param, DefType } from "../params/param";
99
import { ParamTypes } from "../params/paramTypes";
1010
import { ParamTypeDefinition } from "../params/interface";
1111
import { Disposable } from "../interface";
12-
import { ParamType } from "../params/type";
12+
import { ParamType } from "../params/paramType";
1313
import { ParamFactory, UrlMatcherConfig } from "./interface";
1414

1515
/**

0 commit comments

Comments
 (0)