@@ -29,23 +29,22 @@ import {RouterLink} from './src/router/router_link';
2929import { RouteRegistry } from './src/router/route_registry' ;
3030import { Location } from './src/router/location' ;
3131import { bind , OpaqueToken , Binding } from './core' ;
32- import { CONST_EXPR , Type } from './src/core/facade/lang' ;
32+ import { CONST_EXPR } from './src/core/facade/lang' ;
33+ import { ApplicationRef } from './src/core/application_ref' ;
34+ import { BaseException } from 'angular2/src/core/facade/exceptions' ;
35+
3336
3437/**
3538 * Token used to bind the component with the top-level {@link RouteConfig}s for the
3639 * application.
3740 *
38- * You can use the {@link routerBindings} function in your {@link bootstrap} bindings to
39- * simplify setting up these bindings.
40- *
4141 * ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
4242 *
4343 * ```
4444 * import {Component, View} from 'angular2/angular2';
4545 * import {
4646 * ROUTER_DIRECTIVES,
4747 * ROUTER_BINDINGS,
48- * ROUTER_PRIMARY_COMPONENT,
4948 * RouteConfig
5049 * } from 'angular2/router';
5150 *
@@ -58,10 +57,7 @@ import {CONST_EXPR, Type} from './src/core/facade/lang';
5857 * // ...
5958 * }
6059 *
61- * bootstrap(AppCmp, [
62- * ROUTER_BINDINGS,
63- * bind(ROUTER_PRIMARY_COMPONENT).toValue(AppCmp)
64- * ]);
60+ * bootstrap(AppCmp, [ROUTER_BINDINGS]);
6561 * ```
6662 */
6763export const ROUTER_PRIMARY_COMPONENT : OpaqueToken =
@@ -76,7 +72,7 @@ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
7672 *
7773 * ```
7874 * import {Component, View} from 'angular2/angular2';
79- * import {ROUTER_DIRECTIVES, routerBindings , RouteConfig} from 'angular2/router';
75+ * import {ROUTER_DIRECTIVES, ROUTER_BINDINGS , RouteConfig} from 'angular2/router';
8076 *
8177 * @Component ({...})
8278 * @View ({directives: [ROUTER_DIRECTIVES]})
@@ -87,27 +83,21 @@ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
8783 * // ...
8884 * }
8985 *
90- * bootstrap(AppCmp, [routerBindings(AppCmp)] );
86+ * bootstrap(AppCmp, ROUTER_BINDINGS );
9187 * ```
9288 */
9389export const ROUTER_DIRECTIVES : any [ ] = CONST_EXPR ( [ RouterOutlet , RouterLink ] ) ;
9490
9591/**
9692 * A list of {@link Binding}s. To use the router, you must add this to your application.
9793 *
98- * Note that you also need to bind to {@link ROUTER_PRIMARY_COMPONENT}.
99- *
100- * You can use the {@link routerBindings} function in your {@link bootstrap} bindings to
101- * simplify setting up these bindings.
102- *
10394 * ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
10495 *
10596 * ```
10697 * import {Component, View} from 'angular2/angular2';
10798 * import {
10899 * ROUTER_DIRECTIVES,
109100 * ROUTER_BINDINGS,
110- * ROUTER_PRIMARY_COMPONENT,
111101 * RouteConfig
112102 * } from 'angular2/router';
113103 *
@@ -120,50 +110,30 @@ export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
120110 * // ...
121111 * }
122112 *
123- * bootstrap(AppCmp, [
124- * ROUTER_BINDINGS,
125- * bind(ROUTER_PRIMARY_COMPONENT).toValue(AppCmp)
126- * ]);
113+ * bootstrap(AppCmp, [ROUTER_BINDINGS]);
127114 * ```
128115 */
129116export const ROUTER_BINDINGS : any [ ] = CONST_EXPR ( [
130117 RouteRegistry ,
131118 CONST_EXPR ( new Binding ( LocationStrategy , { toClass : PathLocationStrategy } ) ) ,
132119 Location ,
133- CONST_EXPR (
134- new Binding ( Router ,
135- {
136- toFactory : routerFactory ,
137- deps : CONST_EXPR ( [ RouteRegistry , Location , ROUTER_PRIMARY_COMPONENT ] )
138- } ) )
120+ CONST_EXPR ( new Binding ( Router ,
121+ {
122+ toFactory : routerFactory ,
123+ deps : CONST_EXPR ( [ RouteRegistry , Location , ROUTER_PRIMARY_COMPONENT ] )
124+ } ) ) ,
125+ CONST_EXPR ( new Binding (
126+ ROUTER_PRIMARY_COMPONENT ,
127+ { toFactory : routerPrimaryComponentFactory , deps : CONST_EXPR ( [ ApplicationRef ] ) } ) )
139128] ) ;
140129
141130function routerFactory ( registry , location , primaryComponent ) {
142131 return new RootRouter ( registry , location , primaryComponent ) ;
143132}
144133
145- /**
146- * A list of {@link Binding}s. To use the router, you must add these bindings to
147- * your application.
148- *
149- * ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
150- *
151- * ```
152- * import {Component, View} from 'angular2/angular2';
153- * import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
154- *
155- * @Component ({...})
156- * @View ({directives: [ROUTER_DIRECTIVES]})
157- * @RouteConfig ([
158- * {...},
159- * ])
160- * class AppCmp {
161- * // ...
162- * }
163- *
164- * bootstrap(AppCmp, [routerBindings(AppCmp)]);
165- * ```
166- */
167- export function routerBindings ( primaryComponent : Type ) : Array < any > {
168- return [ ROUTER_BINDINGS , bind ( ROUTER_PRIMARY_COMPONENT ) . toValue ( primaryComponent ) ] ;
134+ function routerPrimaryComponentFactory ( app ) {
135+ if ( app . componentTypes . length == 0 ) {
136+ throw new BaseException ( "Bootstrap at least one component before injecting Router." ) ;
137+ }
138+ return app . componentTypes [ 0 ] ;
169139}
0 commit comments