@@ -77,39 +77,46 @@ export class RouteRegistry {
7777 return null ;
7878 }
7979
80- var solutions = componentRecognizer . recognize ( url ) ;
80+ var componentSolutions = componentRecognizer . recognize ( url ) ;
81+ var fullSolutions = ListWrapper . create ( ) ;
8182
82- for ( var i = 0 ; i < solutions . length ; i ++ ) {
83- var candidate = solutions [ i ] ;
83+ for ( var i = 0 ; i < componentSolutions . length ; i ++ ) {
84+ var candidate = componentSolutions [ i ] ;
8485 if ( candidate [ 'unmatchedUrl' ] . length == 0 ) {
85- return handlerToLeafInstructions ( candidate , parentComponent ) ;
86- }
87-
88- var children = StringMapWrapper . create ( ) ,
89- allMapped = true ;
86+ ListWrapper . push ( fullSolutions , handlerToLeafInstructions ( candidate , parentComponent ) ) ;
87+ } else {
88+ var children = StringMapWrapper . create ( ) ,
89+ allMapped = true ;
90+
91+ StringMapWrapper . forEach ( candidate [ 'handler' ] [ 'components' ] , ( component , name ) => {
92+ if ( ! allMapped ) {
93+ return ;
94+ }
95+ var childInstruction = this . recognize ( candidate [ 'unmatchedUrl' ] , component ) ;
96+ if ( isPresent ( childInstruction ) ) {
97+ childInstruction . params = candidate [ 'params' ] ;
98+ children [ name ] = childInstruction ;
99+ } else {
100+ allMapped = false ;
101+ }
102+ } ) ;
90103
91- StringMapWrapper . forEach ( candidate [ 'handler' ] [ 'components' ] , ( component , name ) => {
92- if ( ! allMapped ) {
93- return ;
94- }
95- var childInstruction = this . recognize ( candidate [ 'unmatchedUrl' ] , component ) ;
96- if ( isPresent ( childInstruction ) ) {
97- childInstruction . params = candidate [ 'params' ] ;
98- children [ name ] = childInstruction ;
99- } else {
100- allMapped = false ;
104+ if ( allMapped ) {
105+ ListWrapper . push ( fullSolutions , new Instruction ( {
106+ component : parentComponent ,
107+ children : children ,
108+ matchedUrl : candidate [ 'matchedUrl' ] ,
109+ parentCost : candidate [ 'cost' ]
110+ } ) ) ;
101111 }
102- } ) ;
103-
104- if ( allMapped ) {
105- return new Instruction ( {
106- component : parentComponent ,
107- children : children ,
108- matchedUrl : candidate [ 'matchedUrl' ]
109- } ) ;
110112 }
111113 }
112114
115+ if ( fullSolutions . length > 0 ) {
116+ ListWrapper . sort ( fullSolutions , ( a , b ) => a . cost < b . cost ? - 1 : 1 ) ;
117+ return fullSolutions [ 0 ] ;
118+ }
119+
113120 return null ;
114121 }
115122
@@ -127,13 +134,15 @@ function handlerToLeafInstructions(context, parentComponent) {
127134 StringMapWrapper . forEach ( context [ 'handler' ] [ 'components' ] , ( component , outletName ) => {
128135 children [ outletName ] = new Instruction ( {
129136 component : component ,
130- params : context [ 'params' ]
137+ params : context [ 'params' ] ,
138+ parentCost : 0
131139 } ) ;
132140 } ) ;
133141 return new Instruction ( {
134142 component : parentComponent ,
135143 children : children ,
136- matchedUrl : context [ 'matchedUrl' ]
144+ matchedUrl : context [ 'matchedUrl' ] ,
145+ parentCost : context [ 'cost' ]
137146 } ) ;
138147}
139148
0 commit comments