Skip to content

Commit 039a2f0

Browse files
committed
b2a940f feat(router): listen to location changes
1 parent dbf01e0 commit 039a2f0

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

BUILD_INFO

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Sat Apr 30 18:27:22 UTC 2016
2-
76d6f5fa0d3cd6b376c2327e5073442e2b8c5a8d
1+
Sat Apr 30 19:12:31 UTC 2016
2+
b2a940f0613d4b84706c58e1b51f1251b118bd32

lib/src/alt_router/router.dart

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "directives/router_outlet.dart" show RouterOutlet;
77
import "package:angular2/src/facade/lang.dart" show Type, isBlank, isPresent;
88
import "package:angular2/src/facade/collection.dart" show ListWrapper;
99
import "package:angular2/src/facade/async.dart"
10-
show EventEmitter, Stream, PromiseWrapper;
10+
show EventEmitter, Stream, PromiseWrapper, ObservableWrapper;
1111
import "package:angular2/src/facade/collection.dart" show StringMapWrapper;
1212
import "package:angular2/src/facade/exceptions.dart" show BaseException;
1313
import "router_url_serializer.dart" show RouterUrlSerializer;
@@ -45,9 +45,11 @@ class Router {
4545
Location _location;
4646
Tree<RouteSegment> _prevTree;
4747
Tree<UrlSegment> _urlTree;
48+
dynamic _locationSubscription;
4849
EventEmitter _changes = new EventEmitter();
4950
Router(this._rootComponent, this._rootComponentType, this._componentResolver,
5051
this._urlSerializer, this._routerOutletMap, this._location) {
52+
this._setUpLocationChangeListener();
5153
this.navigateByUrl(this._location.path());
5254
}
5355
Tree<UrlSegment> get urlTree {
@@ -62,6 +64,16 @@ class Router {
6264
return this._navigate(this.createUrlTree(changes, segment));
6365
}
6466

67+
void dispose() {
68+
ObservableWrapper.dispose(this._locationSubscription);
69+
}
70+
71+
void _setUpLocationChangeListener() {
72+
this._locationSubscription = this._location.subscribe((change) {
73+
this._navigate(this._urlSerializer.parse(change["url"]));
74+
});
75+
}
76+
6577
Future _navigate(Tree<UrlSegment> url) {
6678
this._urlTree = url;
6779
return recognize(this._componentResolver, this._rootComponentType, url)

lib/src/alt_router/router_providers_common.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Router routerFactory(
3838
"Bootstrap at least one component before injecting Router.");
3939
}
4040
// TODO: vsavkin this should not be null
41-
return new Router(null, app.componentTypes[0], componentResolver,
41+
var router = new Router(null, app.componentTypes[0], componentResolver,
4242
urlSerializer, routerOutletMap, location);
43+
app.registerDisposeListener(() => router.dispose());
44+
return router;
4345
}

test/alt_router/integration_spec.dart

+12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ main() {
6666
advance(fixture);
6767
expect(location.path()).toEqual("/team/33/simple");
6868
})));
69+
it(
70+
"should navigate when locations changes",
71+
fakeAsync(inject([Router, TestComponentBuilder, Location],
72+
(router, tcb, location) {
73+
var fixture = tcb.createFakeAsync(RootCmp);
74+
router.navigateByUrl("/team/22/user/victor");
75+
advance(fixture);
76+
location.simulateHashChange("/team/22/user/fedor");
77+
advance(fixture);
78+
expect(fixture.debugElement.nativeElement)
79+
.toHaveText("team 22 { hello fedor, aux: }");
80+
})));
6981
it(
7082
"should support nested routes",
7183
fakeAsync(inject([Router, TestComponentBuilder], (router, tcb) {

0 commit comments

Comments
 (0)