Skip to content

Commit

Permalink
feat(Transition): Make Transition.params() immutable to avoid confusi…
Browse files Browse the repository at this point in the history
…on about mutability

refactor(params): rename type.ts to paramtype.ts
  • Loading branch information
christopherthielen committed Jan 7, 2017
1 parent 23e2b78 commit 0162212
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/params/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export * from "./interface";
export * from "./param";
export * from "./paramTypes";
export * from "./stateParams";
export * from "./type";
export * from "./paramType";
78 changes: 76 additions & 2 deletions src/params/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module params
*/ /** for typedoc */

import {ParamType} from "./type";
import {ParamType} from "./paramType";

/**
* Parameter values
Expand Down Expand Up @@ -110,6 +110,10 @@ export interface ParamDeclaration {
*
* This defines a default value for the parameter.
* If a parameter value is `undefined`, this default value will be used instead
*
* ---
*
* Default: `undefined`
*/
value?: any;

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

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

Expand Down Expand Up @@ -258,6 +270,10 @@ export interface ParamDeclaration {
* ---
*
* Note: this value overrides the `dynamic` value on a custom parameter type ([[ParamTypeDefinition.dynamic]]).
*
* ---
*
* Default: `false`
*/
dynamic: boolean;

Expand Down Expand Up @@ -292,8 +308,45 @@ export interface ParamDeclaration {
* It's generally safe to use a raw parameter at the end of a path, like '/product/:slug'.
* However, beware of the characters you allow in your raw parameter values.
* Avoid unencoded characters that could disrupt normal URL parsing, such as `?` and `#`.
*
* ---
*
* Default: `false`
*/
raw: boolean;

/**
* Enables/disables inheriting of this parameter value
*
* When a transition is run with [[TransitionOptions.inherit]] set to `true`, the current param values are inherited.
* Parameters values which have `inherit: false` will not be inherited.
*
* #### Example state :
* ```js
* var fooState = {
* name: 'foo',
* url: '/:fooId?mode&refresh',
* params: {
* refresh: { inherit: false }
* }
* }
*
* // Set fooId to 123
* $state.go('fooState', { fooId: 1234, mode: 'list', refresh: true });
* ```
*
* In the component:
* `mode: 'list' is inherited, but refresh: true is not inherited.
* // The param values are thus: `{ fooId: 4567, mode: 'list' }`
* ```
* <ui-sref="foo({ fooId: 4567 })">4567</ui-sref>
* ```
*
* ---
*
* Default: `true`
*/
inherit: boolean;
}

/** @internalapi */
Expand Down Expand Up @@ -520,5 +573,26 @@ export interface ParamTypeDefinition {
* See: [[ParamDeclaration.raw]] for details
*/
raw: boolean;

/**
* Enables/disables inheriting of parameter values (of this type)
*
* When a transition is run with [[TransitionOptions.inherit]] set to `true`, the current param values are inherited.
* Param values whose type has `inherit: false` will not be inherited.
*
* The internal parameter type of `hash` has `inherit: false`.
* This is used to disable inheriting of the hash value (`#`) on subsequent transitions.
*
* #### Example:
* ```js
* $state.go('home', { '#': 'inboxAnchor' });
* ...
* // "#" is not inherited.
* // The value of the "#" parameter will be `null`
* // The url's hash will be cleared.
* $state.go('home.nest');
* ```
*/
inherit: boolean;
}

2 changes: 1 addition & 1 deletion src/params/param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { prop, propEq } from "../common/hof";
import { isInjectable, isDefined, isString, isArray } from "../common/predicates";
import { RawParams, ParamDeclaration } from "../params/interface";
import { services } from "../common/coreservices";
import { ParamType } from "./type";
import { ParamType } from "./paramType";
import { ParamTypes } from "./paramTypes";
import { UrlMatcherFactory } from "../url/urlMatcherFactory";

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/params/paramTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fromJson, toJson, identity, equals, inherit, map, extend, pick } from "
import { isDefined, isNullOrUndefined } from "../common/predicates";
import { is } from "../common/hof";
import { services } from "../common/coreservices";
import { ParamType } from "./type";
import { ParamType } from "./paramType";
import { ParamTypeDefinition } from "./interface";

/**
Expand Down
3 changes: 2 additions & 1 deletion src/transition/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export class Transition implements IHookRegistry {
* Gets transition parameter values
*
* Returns the parameter values for a transition as key/value pairs.
* This object is immutable.
*
* By default, returns the new parameter values (for the "to state").
* To return the previous parameter values, supply `'from'` as the `pathname` argument.
Expand All @@ -262,7 +263,7 @@ export class Transition implements IHookRegistry {
params(pathname?: string): any;
params<T>(pathname?: string): T;
params(pathname: string = "to") {
return this._treeChanges[pathname].map(prop("paramValues")).reduce(mergeR, {});
return Object.freeze(this._treeChanges[pathname].map(prop("paramValues")).reduce(mergeR, {}));
}


Expand Down
2 changes: 1 addition & 1 deletion src/url/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @module url
*/ /** */
import { LocationConfig } from "../common/coreservices";
import { ParamType } from "../params/type";
import { ParamType } from "../params/paramType";
import { Param } from "../params/param";
import { UIRouter } from "../router";
import { TargetState } from "../state/targetState";
Expand Down
2 changes: 1 addition & 1 deletion src/url/urlMatcherFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Param, DefType } from "../params/param";
import { ParamTypes } from "../params/paramTypes";
import { ParamTypeDefinition } from "../params/interface";
import { Disposable } from "../interface";
import { ParamType } from "../params/type";
import { ParamType } from "../params/paramType";
import { ParamFactory, UrlMatcherConfig } from "./interface";

/**
Expand Down

0 comments on commit 0162212

Please sign in to comment.