@@ -44,43 +44,48 @@ function $UrlRouterProvider( $urlMatcherFactory) {
4444
4545 this . when =
4646 function ( what , handler ) {
47- var rule , redirect ;
48- if ( isString ( what ) )
49- what = $urlMatcherFactory . compile ( what ) ;
50-
51- if ( $urlMatcherFactory . isMatcher ( what ) ) {
52- if ( isString ( handler ) ) {
53- redirect = $urlMatcherFactory . compile ( handler ) ;
54- handler = [ '$match' , function ( $match ) { return redirect . format ( $match ) ; } ] ;
55- }
56- else if ( ! isFunction ( handler ) && ! isArray ( handler ) )
57- throw new Error ( "invalid 'handler' in when()" ) ;
47+ var redirect , handlerIsString = isString ( handler ) ;
48+ if ( isString ( what ) ) what = $urlMatcherFactory . compile ( what ) ;
5849
59- rule = function ( $injector , $location ) {
60- return handleIfMatch ( $injector , handler , what . exec ( $location . path ( ) , $location . search ( ) ) ) ;
61- } ;
62- rule . prefix = isString ( what . prefix ) ? what . prefix : '' ;
63- }
64- else if ( what instanceof RegExp ) {
65- if ( isString ( handler ) ) {
66- redirect = handler ;
67- handler = [ '$match' , function ( $match ) { return interpolate ( redirect , $match ) ; } ] ;
50+ if ( ! handlerIsString && ! isFunction ( handler ) && ! isArray ( handler ) )
51+ throw new Error ( "invalid 'handler' in when()" ) ;
52+
53+ var strategies = {
54+ matcher : function ( what , handler ) {
55+ if ( handlerIsString ) {
56+ redirect = $urlMatcherFactory . compile ( handler ) ;
57+ handler = [ '$match' , function ( $match ) { return redirect . format ( $match ) ; } ] ;
58+ }
59+ return extend ( function ( $injector , $location ) {
60+ return handleIfMatch ( $injector , handler , what . exec ( $location . path ( ) , $location . search ( ) ) ) ;
61+ } , {
62+ prefix : isString ( what . prefix ) ? what . prefix : ''
63+ } ) ;
64+ } ,
65+ regex : function ( what , handler ) {
66+ if ( what . global || what . sticky ) throw new Error ( "when() RegExp must not be global or sticky" ) ;
67+
68+ if ( handlerIsString ) {
69+ redirect = handler ;
70+ handler = [ '$match' , function ( $match ) { return interpolate ( redirect , $match ) ; } ] ;
71+ }
72+ return extend ( function ( $injector , $location ) {
73+ return handleIfMatch ( $injector , handler , what . exec ( $location . path ( ) ) ) ;
74+ } , {
75+ prefix : regExpPrefix ( what )
76+ } ) ;
6877 }
69- else if ( ! isFunction ( handler ) && ! isArray ( handler ) )
70- throw new Error ( "invalid 'handler' in when()" ) ;
78+ } ;
7179
72- if ( what . global || what . sticky )
73- throw new Error ( "when() RegExp must not be global or sticky" ) ;
80+ var check = { matcher : $urlMatcherFactory . isMatcher ( what ) , regex : what instanceof RegExp } ;
7481
75- rule = function ( $injector , $location ) {
76- return handleIfMatch ( $injector , handler , what . exec ( $location . path ( ) ) ) ;
77- } ;
78- rule . prefix = regExpPrefix ( what ) ;
82+ for ( var n in check ) {
83+ if ( check [ n ] ) {
84+ return this . rule ( strategies [ n ] ( what , handler ) ) ;
85+ }
7986 }
80- else
81- throw new Error ( "invalid 'what' in when()" ) ;
8287
83- return this . rule ( rule ) ;
88+ throw new Error ( "invalid 'what' in when()" ) ;
8489 } ;
8590
8691 this . $get =
0 commit comments