Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 21 additions & 51 deletions modules/angular2/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,22 @@ import {RouterLink} from './src/router/router_link';
import {RouteRegistry} from './src/router/route_registry';
import {Location} from './src/router/location';
import {bind, OpaqueToken, Binding} from './core';
import {CONST_EXPR, Type} from './src/core/facade/lang';
import {CONST_EXPR} from './src/core/facade/lang';
import {ApplicationRef} from './src/core/application_ref';
import {BaseException} from 'angular2/src/core/facade/exceptions';


/**
* Token used to bind the component with the top-level {@link RouteConfig}s for the
* application.
*
* You can use the {@link routerBindings} function in your {@link bootstrap} bindings to
* simplify setting up these bindings.
*
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
*
* ```
* import {Component, View} from 'angular2/angular2';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_BINDINGS,
* ROUTER_PRIMARY_COMPONENT,
* RouteConfig
* } from 'angular2/router';
*
Expand All @@ -58,10 +57,7 @@ import {CONST_EXPR, Type} from './src/core/facade/lang';
* // ...
* }
*
* bootstrap(AppCmp, [
* ROUTER_BINDINGS,
* bind(ROUTER_PRIMARY_COMPONENT).toValue(AppCmp)
* ]);
* bootstrap(AppCmp, [ROUTER_BINDINGS]);
* ```
*/
export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
Expand All @@ -76,7 +72,7 @@ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
*
* ```
* import {Component, View} from 'angular2/angular2';
* import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
* import {ROUTER_DIRECTIVES, ROUTER_BINDINGS, RouteConfig} from 'angular2/router';
*
* @Component({...})
* @View({directives: [ROUTER_DIRECTIVES]})
Expand All @@ -87,27 +83,21 @@ export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
* // ...
* }
*
* bootstrap(AppCmp, [routerBindings(AppCmp)]);
* bootstrap(AppCmp, ROUTER_BINDINGS);
* ```
*/
export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);

/**
* A list of {@link Binding}s. To use the router, you must add this to your application.
*
* Note that you also need to bind to {@link ROUTER_PRIMARY_COMPONENT}.
*
* You can use the {@link routerBindings} function in your {@link bootstrap} bindings to
* simplify setting up these bindings.
*
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
*
* ```
* import {Component, View} from 'angular2/angular2';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_BINDINGS,
* ROUTER_PRIMARY_COMPONENT,
* RouteConfig
* } from 'angular2/router';
*
Expand All @@ -120,50 +110,30 @@ export const ROUTER_DIRECTIVES: any[] = CONST_EXPR([RouterOutlet, RouterLink]);
* // ...
* }
*
* bootstrap(AppCmp, [
* ROUTER_BINDINGS,
* bind(ROUTER_PRIMARY_COMPONENT).toValue(AppCmp)
* ]);
* bootstrap(AppCmp, [ROUTER_BINDINGS]);
* ```
*/
export const ROUTER_BINDINGS: any[] = CONST_EXPR([
RouteRegistry,
CONST_EXPR(new Binding(LocationStrategy, {toClass: PathLocationStrategy})),
Location,
CONST_EXPR(
new Binding(Router,
{
toFactory: routerFactory,
deps: CONST_EXPR([RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT])
}))
CONST_EXPR(new Binding(Router,
{
toFactory: routerFactory,
deps: CONST_EXPR([RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT])
})),
CONST_EXPR(new Binding(
ROUTER_PRIMARY_COMPONENT,
{toFactory: routerPrimaryComponentFactory, deps: CONST_EXPR([ApplicationRef])}))
]);

function routerFactory(registry, location, primaryComponent) {
return new RootRouter(registry, location, primaryComponent);
}

/**
* A list of {@link Binding}s. To use the router, you must add these bindings to
* your application.
*
* ## Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
*
* ```
* import {Component, View} from 'angular2/angular2';
* import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
*
* @Component({...})
* @View({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {...},
* ])
* class AppCmp {
* // ...
* }
*
* bootstrap(AppCmp, [routerBindings(AppCmp)]);
* ```
*/
export function routerBindings(primaryComponent: Type): Array<any> {
return [ROUTER_BINDINGS, bind(ROUTER_PRIMARY_COMPONENT).toValue(primaryComponent)];
function routerPrimaryComponentFactory(app) {
if (app.componentTypes.length == 0) {
throw new BaseException("Bootstrap at least one component before injecting Router.");
}
return app.componentTypes[0];
}
14 changes: 12 additions & 2 deletions modules/angular2/src/core/application_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ export class PlatformRef_ extends PlatformRef {

private _initApp(zone: NgZone, bindings: Array<Type | Binding | any[]>): ApplicationRef {
var injector: Injector;
var app: ApplicationRef;
zone.run(() => {
bindings.push(bind(NgZone).toValue(zone));
bindings.push(bind(ApplicationRef).toValue(this));
bindings.push(bind(ApplicationRef).toFactory((): ApplicationRef => app, []));

var exceptionHandler;
try {
Expand All @@ -249,7 +250,7 @@ export class PlatformRef_ extends PlatformRef {
}
}
});
var app = new ApplicationRef_(this, zone, injector);
app = new ApplicationRef_(this, zone, injector);
this._applications.push(app);
return app;
}
Expand Down Expand Up @@ -313,11 +314,17 @@ export abstract class ApplicationRef {
* Dispose of this application and all of its components.
*/
abstract dispose(): void;

/**
* Get a list of component types registered to this application.
*/
get componentTypes(): Type[] { return unimplemented(); };
}

export class ApplicationRef_ extends ApplicationRef {
private _bootstrapListeners: Function[] = [];
private _rootComponents: ComponentRef[] = [];
private _rootComponentTypes: Type[] = [];

constructor(private _platform: PlatformRef_, private _zone: NgZone, private _injector: Injector) {
super();
Expand All @@ -335,6 +342,7 @@ export class ApplicationRef_ extends ApplicationRef {
componentBindings.push(bindings);
}
var exceptionHandler = this._injector.get(ExceptionHandler);
this._rootComponentTypes.push(componentType);
try {
var injector: Injector = this._injector.resolveAndCreateChild(componentBindings);
var compRefToken: Promise<ComponentRef> = injector.get(APP_COMPONENT_REF_PROMISE);
Expand Down Expand Up @@ -370,4 +378,6 @@ export class ApplicationRef_ extends ApplicationRef {
this._rootComponents.forEach((ref) => ref.dispose());
this._platform._applicationDisposed(this);
}

get componentTypes(): any[] { return this._rootComponentTypes; }
}
6 changes: 2 additions & 4 deletions modules/angular2/src/router/hash_location_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {EventListener, History, Location} from 'angular2/src/core/facade/browser
* import {Component, View} from 'angular2/angular2';
* import {
* ROUTER_DIRECTIVES,
* routerBindings,
* ROUTER_BINDINGS,
* RouteConfig,
* Location
* } from 'angular2/router';
Expand All @@ -34,9 +34,7 @@ import {EventListener, History, Location} from 'angular2/src/core/facade/browser
* }
* }
*
* bootstrap(AppCmp, [
* routerBindings(AppCmp)
* ]);
* bootstrap(AppCmp, [ROUTER_BINDINGS]);
* ```
*/
@Injectable()
Expand Down
8 changes: 4 additions & 4 deletions modules/angular2/src/router/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {Url} from './url_parser';
*
* ```
* import {bootstrap, Component, View} from 'angular2/angular2';
* import {Router, ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
* import {Router, ROUTER_DIRECTIVES, ROUTER_BINDINGS, RouteConfig} from 'angular2/router';
*
* @Component({...})
* @View({directives: [ROUTER_DIRECTIVES]})
Expand All @@ -34,7 +34,7 @@ import {Url} from './url_parser';
* }
* }
*
* bootstrap(AppCmp, routerBindings(AppCmp));
* bootstrap(AppCmp, ROUTER_BINDINGS);
* ```
*/
export class RouteParams {
Expand All @@ -54,7 +54,7 @@ export class RouteParams {
*
* ```
* import {bootstrap, Component, View} from 'angular2/angular2';
* import {Router, ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
* import {Router, ROUTER_DIRECTIVES, ROUTER_BINDINGS, RouteConfig} from 'angular2/router';
*
* @Component({...})
* @View({directives: [ROUTER_DIRECTIVES]})
Expand All @@ -68,7 +68,7 @@ export class RouteParams {
* }
* }
*
* bootstrap(AppCmp, routerBindings(AppCmp));
* bootstrap(AppCmp, ROUTER_BINDINGS);
* ```
*/
export class Instruction {
Expand Down
8 changes: 4 additions & 4 deletions modules/angular2/src/router/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/src/core/di';
*
* ```
* import {Component, View} from 'angular2/angular2';
* import {ROUTER_DIRECTIVES, routerBindings, RouteConfig} from 'angular2/router';
* import {ROUTER_DIRECTIVES, ROUTER_BINDINGS, RouteConfig} from 'angular2/router';
*
* @Component({...})
* @View({directives: [ROUTER_DIRECTIVES]})
Expand All @@ -29,7 +29,7 @@ import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/src/core/di';
* }
*
* bootstrap(AppCmp, [
* routerBindings(AppCmp),
* ROUTER_BINDINGS,
* PathLocationStrategy,
* bind(APP_BASE_HREF).toValue('/my/app')
* ]);
Expand Down Expand Up @@ -59,7 +59,7 @@ export const APP_BASE_HREF: OpaqueToken = CONST_EXPR(new OpaqueToken('appBaseHre
* import {Component, View} from 'angular2/angular2';
* import {
* ROUTER_DIRECTIVES,
* routerBindings,
* ROUTER_BINDINGS,
* RouteConfig,
* Location
* } from 'angular2/router';
Expand All @@ -75,7 +75,7 @@ export const APP_BASE_HREF: OpaqueToken = CONST_EXPR(new OpaqueToken('appBaseHre
* }
* }
*
* bootstrap(AppCmp, [routerBindings(AppCmp)]);
* bootstrap(AppCmp, [ROUTER_BINDINGS]);
* ```
*/
@Injectable()
Expand Down
6 changes: 3 additions & 3 deletions modules/angular2/src/router/path_location_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {LocationStrategy} from './location_strategy';
* browser's URL.
*
* `PathLocationStrategy` is the default binding for {@link LocationStrategy}
* provided in {@link routerBindings} and {@link ROUTER_BINDINGS}.
* provided in {@link ROUTER_BINDINGS}.
*
* If you're using `PathLocationStrategy`, you must provide a binding for
* {@link APP_BASE_HREF} to a string representing the URL prefix that should
Expand All @@ -27,7 +27,7 @@ import {LocationStrategy} from './location_strategy';
* import {
* APP_BASE_HREF
* ROUTER_DIRECTIVES,
* routerBindings,
* ROUTER_BINDINGS,
* RouteConfig,
* Location
* } from 'angular2/router';
Expand All @@ -44,7 +44,7 @@ import {LocationStrategy} from './location_strategy';
* }
*
* bootstrap(AppCmp, [
* routerBindings(AppCmp), // includes binding to PathLocationStrategy
* ROUTER_BINDINGS, // includes binding to PathLocationStrategy
* bind(APP_BASE_HREF).toValue('/my/app')
* ]);
* ```
Expand Down
1 change: 1 addition & 0 deletions modules/angular2/test/public_api_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ var NG_API = [
'ApplicationRef:js',
'ApplicationRef.injector:js',
'ApplicationRef.zone:js',
'ApplicationRef.componentTypes:js',
/*
Abstract methods
'ApplicationRef.bootstrap()',
Expand Down
Loading