@@ -80,10 +80,19 @@ function UrlMatcher(pattern, config) {
80
80
segments = this . segments = [ ] ,
81
81
params = this . params = { } ;
82
82
83
+ /**
84
+ * [Internal] Gets the decoded representation of a value if the value is defined, otherwise, returns the
85
+ * default value, which may be the result of an injectable function.
86
+ */
87
+ function $value ( value ) {
88
+ /*jshint validthis: true */
89
+ return isDefined ( value ) ? this . type . decode ( value ) : $UrlMatcherFactory . $$getDefaultValue ( this ) ;
90
+ }
91
+
83
92
function addParameter ( id , type , config ) {
84
93
if ( ! / ^ \w + ( - + \w + ) * $ / . test ( id ) ) throw new Error ( "Invalid parameter name '" + id + "' in pattern '" + pattern + "'" ) ;
85
94
if ( params [ id ] ) throw new Error ( "Duplicate parameter name '" + id + "' in pattern '" + pattern + "'" ) ;
86
- params [ id ] = extend ( { type : type || new Type ( ) } , config ) ;
95
+ params [ id ] = extend ( { type : type || new Type ( ) , $value : $value } , config ) ;
87
96
}
88
97
89
98
function quoteRegExp ( string , pattern , isOptional ) {
@@ -216,7 +225,7 @@ UrlMatcher.prototype.exec = function (path, searchParams) {
216
225
for ( i = 0 ; i < nPath ; i ++ ) {
217
226
param = params [ i ] ;
218
227
cfg = this . params [ param ] ;
219
- values [ param ] = isDefined ( m [ i + 1 ] ) ? cfg . type . decode ( m [ i + 1 ] ) : cfg . value ;
228
+ values [ param ] = cfg . $value ( m [ i + 1 ] ) ;
220
229
}
221
230
for ( /**/ ; i < nTotal ; i ++ ) {
222
231
param = params [ i ] ;
@@ -483,6 +492,19 @@ function $UrlMatcherFactory() {
483
492
} ;
484
493
}
485
494
495
+ function isInjectable ( value ) {
496
+ return ( isFunction ( value ) || ( isArray ( value ) && isFunction ( value [ value . length - 1 ] ) ) ) ;
497
+ }
498
+
499
+ /**
500
+ * [Internal] Get the default value of a parameter, which may be an injectable function.
501
+ */
502
+ $UrlMatcherFactory . $$getDefaultValue = function ( config ) {
503
+ if ( ! isInjectable ( config . value ) ) return config . value ;
504
+ if ( ! injector ) throw new Error ( "Injectable functions cannot be called at configuration time" ) ;
505
+ return injector . invoke ( config . value ) ;
506
+ } ;
507
+
486
508
/**
487
509
* @ngdoc function
488
510
* @name ui.router.util.$urlMatcherFactory#caseInsensitive
@@ -683,8 +705,7 @@ function $UrlMatcherFactory() {
683
705
if ( UrlMatcher . prototype . $types [ type . name ] ) {
684
706
throw new Error ( "A type named '" + type . name + "' has already been defined." ) ;
685
707
}
686
- var isAnnotated = isFunction ( type . def ) || isArray ( type . def ) ;
687
- var def = new Type ( isAnnotated ? injector . invoke ( type . def ) : type . def ) ;
708
+ var def = new Type ( isInjectable ( type . def ) ? injector . invoke ( type . def ) : type . def ) ;
688
709
UrlMatcher . prototype . $types [ type . name ] = def ;
689
710
} ) ;
690
711
}
0 commit comments