11import { Promise , PromiseWrapper , EventEmitter , ObservableWrapper } from 'angular2/src/facade/async' ;
22import { Map , MapWrapper , List , ListWrapper } from 'angular2/src/facade/collection' ;
3- import { isBlank , Type } from 'angular2/src/facade/lang' ;
3+ import { isBlank , isPresent , Type } from 'angular2/src/facade/lang' ;
44
55import { RouteRegistry } from './route_registry' ;
66import { Pipeline } from './pipeline' ;
@@ -24,6 +24,8 @@ export class Router {
2424 lastNavigationAttempt: string ;
2525 previousUrl:string ;
2626
27+ _currentInstruction:Instruction ;
28+
2729 _pipeline:Pipeline ;
2830 _registry:RouteRegistry ;
2931 _outlets:Map < any , RouterOutlet > ;
@@ -42,6 +44,7 @@ export class Router {
4244 this . _registry = registry ;
4345 this . _pipeline = pipeline ;
4446 this . _subject = new EventEmitter ( ) ;
47+ this . _currentInstruction = null ;
4548 }
4649
4750
@@ -61,7 +64,11 @@ export class Router {
6164 */
6265 registerOutlet ( outlet :RouterOutlet , name = 'default' ) :Promise {
6366 MapWrapper . set ( this . _outlets , name , outlet ) ;
64- return this . renavigate ( ) ;
67+ if ( isPresent ( this . _currentInstruction ) ) {
68+ var childInstruction = this . _currentInstruction . getChildInstruction ( name ) ;
69+ return outlet . activate ( childInstruction ) ;
70+ }
71+ return PromiseWrapper . resolve ( true ) ;
6572 }
6673
6774
@@ -107,23 +114,26 @@ export class Router {
107114
108115 this . lastNavigationAttempt = url ;
109116
110- var instruction = this . recognize ( url ) ;
117+ var matchedInstruction = this . recognize ( url ) ;
111118
112- if ( isBlank ( instruction ) ) {
119+ if ( isBlank ( matchedInstruction ) ) {
113120 return PromiseWrapper . resolve ( false ) ;
114121 }
115122
116- instruction . router = this ;
123+ if ( isPresent ( this . _currentInstruction ) ) {
124+ matchedInstruction . reuseComponentsFrom ( this . _currentInstruction ) ;
125+ }
126+
127+ matchedInstruction . router = this ;
117128 this . _startNavigating ( ) ;
118129
119- var result = this . _pipeline . process ( instruction )
120- . then ( ( _ ) => {
121- this . _location . go ( instruction . matchedUrl ) ;
122- } )
130+ var result = this . _pipeline . process ( matchedInstruction )
123131 . then ( ( _ ) => {
124- ObservableWrapper . callNext ( this . _subject , instruction . matchedUrl ) ;
125- } )
126- . then ( ( _ ) => this . _finishNavigating ( ) ) ;
132+ this . _location . go ( matchedInstruction . matchedUrl ) ;
133+ ObservableWrapper . callNext ( this . _subject , matchedInstruction . matchedUrl ) ;
134+ this . _finishNavigating ( ) ;
135+ this . _currentInstruction = matchedInstruction ;
136+ } ) ;
127137
128138 PromiseWrapper . catchError ( result , ( _ ) => this . _finishNavigating ( ) ) ;
129139
@@ -148,7 +158,11 @@ export class Router {
148158
149159 activateOutlets ( instruction :Instruction ) :Promise {
150160 return this . _queryOutlets ( ( outlet , name ) => {
151- return outlet . activate ( instruction . getChildInstruction ( name ) ) ;
161+ var childInstruction = instruction . getChildInstruction ( name ) ;
162+ if ( childInstruction . reuse ) {
163+ return PromiseWrapper . resolve ( true ) ;
164+ }
165+ return outlet . activate ( childInstruction ) ;
152166 } )
153167 . then ( ( _ ) => instruction . mapChildrenAsync ( ( instruction , _ ) => {
154168 return instruction . router . activateOutlets ( instruction ) ;
0 commit comments