Skip to content

Commit 1f04f70

Browse files
committed
refactor(Router): idiomatic TS
1 parent eea989b commit 1f04f70

File tree

7 files changed

+29
-53
lines changed

7 files changed

+29
-53
lines changed

modules/angular2/src/router/location.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locatio
1717
*/
1818
@Injectable()
1919
export class Location {
20-
private _subject: EventEmitter;
20+
private _subject: EventEmitter = new EventEmitter();
2121
private _baseHref: string;
2222

2323
constructor(public _platformStrategy: LocationStrategy,
2424
@Optional() @Inject(appBaseHrefToken) href?: string) {
25-
this._subject = new EventEmitter();
2625
this._baseHref = stripTrailingSlash(
2726
stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref()));
2827
this._platformStrategy.onPopState((_) => this._onPopState(_));

modules/angular2/src/router/path_recognizer.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ export class ContinuationSegment extends Segment {
3333

3434
class StaticSegment extends Segment {
3535
regex: string;
36-
name: string;
36+
name: string = '';
3737

3838
constructor(public string: string) {
3939
super();
40-
this.name = '';
4140
this.regex = escapeRegex(string);
4241
}
4342

@@ -46,8 +45,9 @@ class StaticSegment extends Segment {
4645

4746
@IMPLEMENTS(Segment)
4847
class DynamicSegment {
49-
regex: string;
50-
constructor(public name: string) { this.regex = "([^/]+)"; }
48+
regex: string = "([^/]+)";
49+
50+
constructor(public name: string) {}
5151

5252
generate(params: StringMap<string, string>): string {
5353
if (!StringMapWrapper.contains(params, this.name)) {
@@ -60,8 +60,8 @@ class DynamicSegment {
6060

6161

6262
class StarSegment {
63-
regex: string;
64-
constructor(public name: string) { this.regex = "(.+)"; }
63+
regex: string = "(.+)";
64+
constructor(public name: string) {}
6565

6666
generate(params: StringMap<string, string>): string {
6767
return normalizeBlank(StringMapWrapper.get(params, this.name));
@@ -134,10 +134,6 @@ export class PathRecognizer {
134134
terminal: boolean = true;
135135

136136
constructor(public path: string, public handler: any) {
137-
this.segments = [];
138-
139-
// TODO: use destructuring assignment
140-
// see https://github.com/angular/ts2dart/issues/158
141137
var parsed = parsePathString(path);
142138
var specificity = parsed['specificity'];
143139
var segments = parsed['segments'];

modules/angular2/src/router/route_recognizer.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@ import {PathRecognizer, ContinuationSegment} from './path_recognizer';
2222
* components.
2323
*/
2424
export class RouteRecognizer {
25-
names: Map<string, PathRecognizer>;
26-
redirects: Map<string, string>;
27-
matchers: Map<RegExp, PathRecognizer>;
28-
29-
constructor() {
30-
this.names = new Map();
31-
this.matchers = new Map();
32-
this.redirects = new Map();
33-
}
25+
names: Map<string, PathRecognizer> = new Map();
26+
redirects: Map<string, string> = new Map();
27+
matchers: Map<RegExp, PathRecognizer> = new Map();
3428

3529
addRedirect(path: string, target: string): void {
3630
if (path == '/') {
@@ -113,6 +107,7 @@ export class RouteMatch {
113107
params: StringMap<string, string>;
114108
matchedUrl: string;
115109
unmatchedUrl: string;
110+
116111
constructor({specificity, handler, params, matchedUrl, unmatchedUrl}: {
117112
specificity?: number,
118113
handler?: StringMap<string, any>,

modules/angular2/src/router/route_registry.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import {Injectable} from 'angular2/di';
2929
*/
3030
@Injectable()
3131
export class RouteRegistry {
32-
_rules: Map<any, RouteRecognizer>;
33-
34-
constructor() { this._rules = new Map(); }
32+
_rules: Map<any, RouteRecognizer> = new Map();
3533

3634
/**
3735
* Given a component and a configuration object, add the route to this registry

modules/angular2/src/router/router.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
22
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
3-
import {isBlank, isPresent, Type} from 'angular2/src/facade/lang';
3+
import {isBlank, isPresent, Type, isArray} from 'angular2/src/facade/lang';
44

55
import {RouteRegistry} from './route_registry';
66
import {Pipeline} from './pipeline';
@@ -28,25 +28,19 @@ import {Location} from './location';
2828
* @exportedAs angular2/router
2929
*/
3030
export class Router {
31-
navigating: boolean;
31+
navigating: boolean = false;
3232
lastNavigationAttempt: string;
33-
previousUrl: string;
33+
previousUrl: string = null;
34+
35+
private _currentInstruction: Instruction = null;
36+
private _currentNavigation: Promise<any> = PromiseWrapper.resolve(true);
37+
private _outlet: RouterOutlet = null;
38+
private _subject: EventEmitter = new EventEmitter();
3439

35-
private _currentInstruction: Instruction;
36-
private _currentNavigation: Promise<any>;
37-
private _outlet: RouterOutlet;
38-
private _subject: EventEmitter;
3940
// todo(jeffbcross): rename _registry to registry since it is accessed from subclasses
4041
// todo(jeffbcross): rename _pipeline to pipeline since it is accessed from subclasses
4142
constructor(public _registry: RouteRegistry, public _pipeline: Pipeline, public parent: Router,
42-
public hostComponent: any) {
43-
this.navigating = false;
44-
this.previousUrl = null;
45-
this._outlet = null;
46-
this._subject = new EventEmitter();
47-
this._currentInstruction = null;
48-
this._currentNavigation = PromiseWrapper.resolve(true);
49-
}
43+
public hostComponent: any) {}
5044

5145

5246
/**
@@ -88,8 +82,8 @@ export class Router {
8882
* ]);
8983
* ```
9084
*/
91-
config(config: any): Promise<any> {
92-
if (config instanceof List) {
85+
config(config: StringMap<string, any>| List<StringMap<string, any>>): Promise<any> {
86+
if (isArray(config)) {
9387
(<List<any>>config)
9488
.forEach((configObject) => { this._registry.config(this.hostComponent, configObject); });
9589
} else {

modules/angular2/src/router/router_link.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@ import {Renderer} from 'angular2/src/render/api';
3838
})
3939
export class RouterLink {
4040
private _route: string;
41-
private _params: StringMap<string, string>;
41+
private _params: StringMap<string, string> = StringMapWrapper.create();
4242

4343
// the url displayed on the anchor element.
4444
_visibleHref: string;
4545
// the url passed to the router navigation.
4646
_navigationHref: string;
4747

4848
constructor(private _elementRef: ElementRef, private _router: Router, private _location: Location,
49-
private _renderer: Renderer) {
50-
this._params = StringMapWrapper.create();
51-
}
49+
private _renderer: Renderer) {}
5250

5351
set route(changes: string) { this._route = changes; }
5452

5553
set params(changes: StringMap<string, string>) { this._params = changes; }
5654

57-
onClick() {
55+
onClick(): boolean {
5856
this._router.navigate(this._navigationHref);
5957
return false;
6058
}

modules/angular2/src/router/router_outlet.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import {Instruction, RouteParams} from './instruction'
2222
selector: 'router-outlet'
2323
})
2424
export class RouterOutlet {
25-
private _childRouter: routerMod.Router;
26-
private _componentRef: ComponentRef;
25+
private _childRouter: routerMod.Router = null;
26+
private _componentRef: ComponentRef = null;
2727
private _elementRef: ElementRef;
28-
private _currentInstruction: Instruction;
28+
private _currentInstruction: Instruction = null;
2929
private _injector: Injector;
3030

3131
constructor(elementRef: ElementRef, private _loader: DynamicComponentLoader,
@@ -38,10 +38,6 @@ export class RouterOutlet {
3838

3939
this._injector = _injector.getAppInjector();
4040
this._elementRef = elementRef;
41-
42-
this._childRouter = null;
43-
this._componentRef = null;
44-
this._currentInstruction = null;
4541
this._parentRouter.registerOutlet(this);
4642
}
4743

0 commit comments

Comments
 (0)