FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(router): listen to location changes · JavaScript-Resource/angular@62a0809 · GitHub

Commit 62a0809

Browse files
committed
feat(router): listen to location changes
Closes angular#8362
1 parent 76d6f5f commit 62a0809

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎modules/angular2/src/alt_router/router.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import {OnInit, provide, ReflectiveInjector, ComponentResolver} from 'angular2/c
22
import {RouterOutlet} from './directives/router_outlet';
33
import {Type, isBlank, isPresent} from 'angular2/src/facade/lang';
44
import {ListWrapper} from 'angular2/src/facade/collection';
5-
import {EventEmitter, Observable, PromiseWrapper} from 'angular2/src/facade/async';
5+
import {
6+
EventEmitter,
7+
Observable,
8+
PromiseWrapper,
9+
ObservableWrapper
10+
} from 'angular2/src/facade/async';
611
import {StringMapWrapper} from 'angular2/src/facade/collection';
712
import {BaseException} from 'angular2/src/facade/exceptions';
813
import {RouterUrlSerializer} from './router_url_serializer';
@@ -33,13 +38,14 @@ export class RouterOutletMap {
3338
export class Router {
3439
private _prevTree: Tree<RouteSegment>;
3540
private _urlTree: Tree<UrlSegment>;
36-
41+
private _locationSubscription: any;
3742
private _changes: EventEmitter<void> = new EventEmitter<void>();
3843

3944
constructor(private _rootComponent: Object, private _rootComponentType: Type,
4045
private _componentResolver: ComponentResolver,
4146
private _urlSerializer: RouterUrlSerializer,
4247
private _routerOutletMap: RouterOutletMap, private _location: Location) {
48+
this._setUpLocationChangeListener();
4349
this.navigateByUrl(this._location.path());
4450
}
4551

@@ -53,6 +59,13 @@ export class Router {
5359
return this._navigate(this.createUrlTree(changes, segment));
5460
}
5561

62+
dispose(): void { ObservableWrapper.dispose(this._locationSubscription); }
63+
64+
private _setUpLocationChangeListener(): void {
65+
this._locationSubscription = this._location.subscribe(
66+
(change) => { this._navigate(this._urlSerializer.parse(change['url'])); });
67+
}
68+
5669
private _navigate(url: Tree<UrlSegment>): Promise<void> {
5770
this._urlTree = url;
5871
return recognize(this._componentResolver, this._rootComponentType, url)

‎modules/angular2/src/alt_router/router_providers_common.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function routerFactory(app: ApplicationRef, componentResolver: ComponentResolver
2424
throw new BaseException("Bootstrap at least one component before injecting Router.");
2525
}
2626
// TODO: vsavkin this should not be null
27-
return new Router(null, app.componentTypes[0], componentResolver, urlSerializer, routerOutletMap,
28-
location);
27+
let router = new Router(null, app.componentTypes[0], componentResolver, urlSerializer,
28+
routerOutletMap, location);
29+
app.registerDisposeListener(() => router.dispose());
30+
return router;
2931
}

‎modules/angular2/test/alt_router/integration_spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ export function main() {
6464
expect(location.path()).toEqual('/team/33/simple');
6565
})));
6666

67+
it('should navigate when locations changes',
68+
fakeAsync(inject([Router, TestComponentBuilder, Location], (router, tcb, location) => {
69+
let fixture = tcb.createFakeAsync(RootCmp);
70+
71+
router.navigateByUrl('/team/22/user/victor');
72+
advance(fixture);
73+
74+
location.simulateHashChange("/team/22/user/fedor");
75+
advance(fixture);
76+
77+
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { hello fedor, aux: }');
78+
})));
79+
6780
it('should support nested routes',
6881
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => {
6982
let fixture = tcb.createFakeAsync(RootCmp);

‎modules/playground/src/alt_routing/inbox-app.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ROUTER_PROVIDERS,
88
OnActivate,
99
RouteSegment,
10-
Tree
10+
Tree,
1111
} from 'angular2/alt_router';
1212
import * as db from './data';
1313
import {Location} from 'angular2/platform/common';

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL