@@ -24,42 +24,49 @@ export interface RawParams {
24
24
export type ParamsOrArray = ( RawParams | RawParams [ ] ) ;
25
25
26
26
/**
27
- * Inside a [[StateDeclaration.params]]:
27
+ * Configuration for a single Parameter
28
28
*
29
- * A ParamDeclaration object defines how a single State Parameter should work.
30
- *
31
- * @example
32
- * ```
29
+ * In a [[StateDeclaration.params]], each `ParamDeclaration`
30
+ * defines how a single State Parameter should work.
33
31
*
32
+ * #### Example:
33
+ * ```js
34
34
* var mystate = {
35
35
* template: '<div ui-view/>',
36
36
* controller: function() {}
37
- * url: '/mystate/:param1 ',
37
+ * url: '/mystate/:start?{count:int} ',
38
38
* params: {
39
- * param1: "index", // <-- Default value for 'param1'
40
- * // (shorthand ParamDeclaration)
39
+ * start: { // <-- ParamDeclaration for `start`
40
+ * type: 'date',
41
+ * value: new Date(), // <-- Default value
42
+ * squash: true,
43
+ * },
41
44
*
42
45
* nonUrlParam: { // <-- ParamDeclaration for 'nonUrlParam'
43
46
* type: "int",
44
47
* array: true,
45
48
* value: []
46
- * }
49
+ * },
50
+ *
51
+ * count: 0, // <-- Default value for 'param1'
52
+ * // (shorthand ParamDeclaration.value)
47
53
* }
48
54
* }
49
55
* ```
50
56
*/
51
57
export interface ParamDeclaration {
52
58
/**
53
- * A property of [[ParamDeclaration]]:
59
+ * The default value for this parameter.
54
60
*
55
- * Specifies the default value for this parameter. This implicitly sets this parameter as optional.
61
+ * Specifies the default value for this parameter.
62
+ * This implicitly sets this parameter as optional.
56
63
*
57
64
* When UI-Router routes to a state and no value is specified for this parameter in the URL or transition,
58
- * the default value will be used instead. If value is a function, it will be injected and invoked, and the
59
- * return value used.
65
+ * the default value will be used instead.
66
+ * If value is a function, it will be injected and invoked, and the return value used.
60
67
*
61
- * Note: `value: undefined` is treated as though no default value was specified, while `value: null` is treated
62
- * as "the default value is null".
68
+ * Note: `value: undefined` is treated as though ** no default value was specified** , while `value: null` is treated
69
+ * as ** "the default value is null"** .
63
70
*
64
71
* ```
65
72
* // define default values for param1 and param2
@@ -74,43 +81,48 @@ export interface ParamDeclaration {
74
81
* ```
75
82
*
76
83
* ### Shorthand Declaration
84
+ *
77
85
* If you only want to set the default value of the parameter, you may use a shorthand syntax.
78
86
* In the params map, instead mapping the param name to a full parameter configuration object, simply set map it
79
87
* to the default parameter value, e.g.:
80
88
* ```
81
- * // define a parameter's default value
89
+ * // Normal (non-shorthand) default value syntax
82
90
* params: {
83
91
* param1: {
84
92
* value: "defaultValue"
85
93
* },
86
94
* param2: {
87
- * value: "param2Default;
95
+ * value: "param2Default"
88
96
* }
89
97
* }
90
98
*
91
- * // shorthand default values
99
+ * // Shorthand default value syntax
92
100
* params: {
93
101
* param1: "defaultValue",
94
102
* param2: "param2Default"
95
103
* }
96
104
* ```
97
105
*
98
- * This defines a default value for the parameter. If the parameter value is `undefined`, this value will be used instead
106
+ * This defines a default value for the parameter.
107
+ * If a parameter value is `undefined`, this default value will be used instead
99
108
*/
100
109
value ?: any ;
101
110
102
111
/**
103
- * A property of [[ParamDeclaration]]:
112
+ * The parameter's type
104
113
*
105
114
* Specifies the [[ParamType]] of the parameter.
115
+ * Parameter types can be used to customize the encoding/decoding of parameter values.
116
+ *
117
+ * Set this property to the name of parameter's type.
118
+ * The type may be either one of the built in types, or a custom type that has been registered with the [[UrlMatcherFactory]].
106
119
*
107
- * Set this property to the name of parameter's type. The type may be either one of the
108
- * built in types, or a custom type that has been registered with the [[$urlMatcherFactory]]
120
+ * See [[ParamTypes]] for the list of built in types.
109
121
*/
110
122
type : ( string | ParamType ) ;
111
123
112
124
/**
113
- * A property of [[ParamDeclaration]]:
125
+ * The parameter's `array` mode
114
126
*
115
127
* Explicitly specifies the array mode of a URL parameter
116
128
*
@@ -124,9 +136,8 @@ export interface ParamDeclaration {
124
136
* If you specified a [[type]] for the parameter, the value will be treated as an array
125
137
* of the specified [[ParamType]].
126
138
*
127
- * @example
128
- * ```
129
- *
139
+ * #### Example:
140
+ * ```js
130
141
* {
131
142
* name: 'foo',
132
143
* url: '/foo/{arrayParam:int}`,
@@ -144,24 +155,24 @@ export interface ParamDeclaration {
144
155
* @default `true` if the parameter name ends in `[]`, such as `url: '/foo/{implicitArrayParam:int[]}'`
145
156
*/
146
157
array : boolean ;
158
+
147
159
/**
148
- * A property of [[ParamDeclaration]]:
160
+ * Squash mode: omit default parameter values in URL
149
161
*
150
162
* Configures how a default parameter value is represented in the URL when the current parameter value
151
163
* is the same as the default value.
152
164
*
153
165
* There are three squash settings:
154
166
*
155
167
* - `false`: The parameter's default value is not squashed. It is encoded and included in the URL
156
- * - `true`: The parameter's default value is omitted from the URL. If the parameter is preceeded
157
- * and followed by slashes in the state's url declaration, then one of those slashes are omitted.
168
+ * - `true`: The parameter's default value is omitted from the URL.
169
+ * If the parameter is preceeded and followed by slashes in the state's url declaration, then one of those slashes are omitted.
158
170
* This can allow for cleaner looking URLs.
159
171
* - `"<arbitrary string>"`: The parameter's default value is replaced with an arbitrary
160
172
* placeholder of your choice.
161
173
*
162
- * @example
163
- * ```
164
- *
174
+ * #### Example:
175
+ * ```js
165
176
* {
166
177
* name: 'mystate',
167
178
* url: '/mystate/:myparam',
@@ -178,9 +189,8 @@ export interface ParamDeclaration {
178
189
* $state.go('mystate', { myparam: 'someOtherValue' });
179
190
* ```
180
191
*
181
- * @example
182
- * ```
183
- *
192
+ * #### Example:
193
+ * ```js
184
194
* {
185
195
* name: 'mystate2',
186
196
* url: '/mystate2/:myparam2',
@@ -200,6 +210,7 @@ export interface ParamDeclaration {
200
210
* If squash is not set, it uses the configured default squash policy. (See [[defaultSquashPolicy]]())
201
211
*/
202
212
squash : ( boolean | string ) ;
213
+
203
214
/**
204
215
* @internalapi
205
216
*
@@ -218,31 +229,69 @@ export interface ParamDeclaration {
218
229
* ```
219
230
*/
220
231
replace : Replace [ ] ;
232
+
221
233
/**
222
234
* @hidden
223
235
* @internalapi
224
236
*
225
237
* This is not part of the declaration; it is a calculated value depending on if a default value was specified or not.
226
238
*/
227
239
isOptional : boolean ;
240
+
228
241
/**
229
242
* Dynamic flag
230
243
*
231
244
* When `dynamic` is `true`, changes to the parameter value will not cause the state to be entered/exited.
245
+ * The resolves will not be re-fetched, nor will views be reloaded.
232
246
*
233
- * The resolves will not be re-fetched, nor will views be recreated.
247
+ * Normally, if a parameter value changes, the state which declared that the parameter will be reloaded (entered/exited).
248
+ * When a parameter is `dynamic`, a transition still occurs, but it does not cause the state to exit/enter.
249
+ *
250
+ * This can be useful to build UI where the component updates itself when the param values change.
251
+ * A common scenario where this is useful is searching/paging/sorting.
234
252
*/
235
253
dynamic : boolean ;
254
+
255
+ /**
256
+ * Disables url-encoding of parameter values
257
+ *
258
+ * When `true`, parameter values are not url-encoded.
259
+ * This is commonly used to allow "slug" urls, with a parameter value including non-semantic slashes.
260
+ *
261
+ * #### Example:
262
+ * ```js
263
+ * url: '/product/:slug',
264
+ * params: {
265
+ * slug: { type: 'string', raw: true }
266
+ * }
267
+ * ```
268
+ *
269
+ * This allows a URL parameter of `{ slug: 'camping/tents/awesome_tent' }`
270
+ * to serialize to `/product/camping/tents/awesome_tent`
271
+ * instead of `/product/camping%2Ftents%2Fawesome_tent`.
272
+ *
273
+ * ### Decoding warning
274
+ *
275
+ * The decoding behavior of raw parameters is not defined.
276
+ * For example, given a url template such as `/:raw1/:raw2`
277
+ * the url `/foo/bar/baz/qux/`, there is no way to determine which slashes belong to which params.
278
+ *
279
+ * It's generally safe to use a raw parameter at the end of a path, like '/product/:slug'.
280
+ * However, beware of the characters you allow in your raw parameter values.
281
+ * Avoid unencoded characters that could disrupt normal URL parsing, such as `?` and `#`.
282
+ */
283
+ raw : boolean ;
236
284
}
237
285
238
286
export interface Replace {
239
287
from : string ;
240
288
to : string ;
241
289
}
242
290
243
-
244
291
/**
245
- * Definition for a custom [[ParamType]]
292
+ * Describes a custom [[ParamType]]
293
+ *
294
+ * See: [[UrlMatcherFactory.type]]
246
295
*
247
296
* A developer can create a custom parameter type definition to customize the encoding and decoding of parameter values.
248
297
* The definition should implement all the methods of this interface.
@@ -254,11 +303,10 @@ export interface Replace {
254
303
* - date
255
304
* - array of <integer/date/string>
256
305
* - custom object
257
- * - some custom string representation
306
+ * - some internal string representation
258
307
*
259
308
* Typed parameter definitions control how parameter values are encoded (to the URL) and decoded (from the URL).
260
- * UI-Router always provides the decoded parameter values to the user from methods such as [[Transition.params]].
261
- *
309
+ * UI-Router always provides the decoded parameter values to the user (from methods such as [[Transition.params]])).
262
310
*
263
311
* For example, if a state has a url of `/foo/{fooId:int}` (the `fooId` parameter is of the `int` ParamType)
264
312
* and if the browser is at `/foo/123`, then the 123 is parsed as an integer:
@@ -358,13 +406,17 @@ export interface ParamTypeDefinition {
358
406
* If your custom type encodes the parameter to a specific type, check for that type here.
359
407
* For example, if your custom type decodes the URL parameter value as an array of ints, return true if the
360
408
* input is an array of ints:
361
- * `(val) => Array.isArray(val) && array.reduce((acc, x) => acc && parseInt(val, 10) === val, true)`.
409
+ *
410
+ * ```
411
+ * is: (val) => Array.isArray(val) && array.reduce((acc, x) => acc && parseInt(val, 10) === val, true)
412
+ * ```
413
+ *
362
414
* If your type decodes the URL parameter value to a custom string, check that the string matches
363
415
* the pattern (don't use an arrow fn if you need `this`): `function (val) { return !!this.pattern.exec(val) }`
364
416
*
365
417
* Note: This method is _not used to check if the URL matches_.
366
- * It's used to check if a _decoded value is this type_.
367
- * Use [[pattern]] to check the URL.
418
+ * It's used to check if a _decoded value *is* this type_.
419
+ * Use [[pattern]] to check the encoded value in the URL.
368
420
*
369
421
* @param val The value to check.
370
422
* @param key If the type check is happening in the context of a specific [[UrlMatcher]] object,
@@ -377,11 +429,14 @@ export interface ParamTypeDefinition {
377
429
/**
378
430
* Encodes a custom/native type value to a string that can be embedded in a URL.
379
431
*
380
- * Note that the return value does *not* need to be URL-safe (i.e. passed through `encodeURIComponent()`), it
381
- * only needs to be a representation of `val` that has been encoded as a string.
432
+ * Note that the return value does *not* need to be URL-safe (i.e. passed through `encodeURIComponent()`).
433
+ * It only needs to be a representation of `val` that has been encoded as a string.
382
434
*
383
- * For example, if your type decodes to an array of ints, then encode the array of ints as a string here:
384
- * `(intarray) => intarray.join("-")`
435
+ * For example, if your custom type decodes to an array of ints, then encode the array of ints to a string here:
436
+ *
437
+ * ```js
438
+ * encode: (intarray) => intarray.join("-")
439
+ * ```
385
440
*
386
441
* Note: in general, [[encode]] and [[decode]] should be symmetrical. That is, `encode(decode(str)) === str`
387
442
*
@@ -395,7 +450,9 @@ export interface ParamTypeDefinition {
395
450
* Decodes a parameter value string (from URL string or transition param) to a custom/native value.
396
451
*
397
452
* For example, if your type decodes to an array of ints, then decode the string as an array of ints here:
398
- * `(str) => str.split("-").map(str => parseInt(str, 10))`
453
+ * ```js
454
+ * decode: (str) => str.split("-").map(str => parseInt(str, 10))
455
+ * ```
399
456
*
400
457
* Note: in general, [[encode]] and [[decode]] should be symmetrical. That is, `encode(decode(str)) === str`
401
458
*
@@ -409,7 +466,9 @@ export interface ParamTypeDefinition {
409
466
* Determines whether two decoded values are equivalent.
410
467
*
411
468
* For example, if your type decodes to an array of ints, then check if the arrays are equal:
412
- * `(a, b) => a.length === b.length && a.reduce((acc, x, idx) => acc && x === b[idx], true)`
469
+ * ```js
470
+ * equals: (a, b) => a.length === b.length && a.reduce((acc, x, idx) => acc && x === b[idx], true)
471
+ * ```
413
472
*
414
473
* @param a A value to compare against.
415
474
* @param b A value to compare against.
@@ -420,7 +479,7 @@ export interface ParamTypeDefinition {
420
479
/**
421
480
* A regular expression that matches the encoded parameter type
422
481
*
423
- * This regular expression is used to match the parameter type in the URL.
482
+ * This regular expression is used to match an encoded parameter value ** in the URL** .
424
483
*
425
484
* For example, if your type encodes as a dash-separated numbers, match that here:
426
485
* `new RegExp("[0-9]+(?:-[0-9]+)*")`.
0 commit comments