| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 76d6f5f commit 62a0809
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,12 @@ import {OnInit, provide, ReflectiveInjector, ComponentResolver} from 'angular2/c | |||
| 2 | 2 | import {RouterOutlet} from './directives/router_outlet'; | |
| 3 | 3 | import {Type, isBlank, isPresent} from 'angular2/src/facade/lang'; | |
| 4 | 4 | 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'; | ||
| 6 | 11 | import {StringMapWrapper} from 'angular2/src/facade/collection'; | |
| 7 | 12 | import {BaseException} from 'angular2/src/facade/exceptions'; | |
| 8 | 13 | import {RouterUrlSerializer} from './router_url_serializer'; | |
@@ -33,13 +38,14 @@ export class RouterOutletMap { | |||
| 33 | 38 | export class Router { | |
| 34 | 39 | private _prevTree: Tree<RouteSegment>; | |
| 35 | 40 | private _urlTree: Tree<UrlSegment>; | |
| 36 | - | ||
| 41 | + private _locationSubscription: any; | ||
| 37 | 42 | private _changes: EventEmitter<void> = new EventEmitter<void>(); | |
| 38 | 43 | ||
| 39 | 44 | constructor(private _rootComponent: Object, private _rootComponentType: Type, | |
| 40 | 45 | private _componentResolver: ComponentResolver, | |
| 41 | 46 | private _urlSerializer: RouterUrlSerializer, | |
| 42 | 47 | private _routerOutletMap: RouterOutletMap, private _location: Location) { | |
| 48 | + this._setUpLocationChangeListener(); | ||
| 43 | 49 | this.navigateByUrl(this._location.path()); | |
| 44 | 50 | } | |
| 45 | 51 | ||
@@ -53,6 +59,13 @@ export class Router { | |||
| 53 | 59 | return this._navigate(this.createUrlTree(changes, segment)); | |
| 54 | 60 | } | |
| 55 | 61 | ||
| 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 | + | ||
| 56 | 69 | private _navigate(url: Tree<UrlSegment>): Promise<void> { | |
| 57 | 70 | this._urlTree = url; | |
| 58 | 71 | return recognize(this._componentResolver, this._rootComponentType, url) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,8 @@ function routerFactory(app: ApplicationRef, componentResolver: ComponentResolver | |||
| 24 | 24 | throw new BaseException("Bootstrap at least one component before injecting Router."); | |
| 25 | 25 | } | |
| 26 | 26 | // 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; | ||
| 29 | 31 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,6 +64,19 @@ export function main() { | |||
| 64 | 64 | expect(location.path()).toEqual('/team/33/simple'); | |
| 65 | 65 | }))); | |
| 66 | 66 | ||
| 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 | + | ||
| 67 | 80 | it('should support nested routes', | |
| 68 | 81 | fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) => { | |
| 69 | 82 | let fixture = tcb.createFakeAsync(RootCmp); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ import { | |||
| 7 | 7 | ROUTER_PROVIDERS, | |
| 8 | 8 | OnActivate, | |
| 9 | 9 | RouteSegment, | |
| 10 | - Tree | ||
| 10 | + Tree, | ||
| 11 | 11 | } from 'angular2/alt_router'; | |
| 12 | 12 | import * as db from './data'; | |
| 13 | 13 | import {Location} from 'angular2/platform/common'; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments