refactor(Router): idiomatic TS

This commit is contained in:
Victor Berchet 2015-06-29 10:37:55 +02:00
parent eea989bef8
commit 1f04f70eda
7 changed files with 29 additions and 53 deletions

View File

@ -17,12 +17,11 @@ export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locatio
*/
@Injectable()
export class Location {
private _subject: EventEmitter;
private _subject: EventEmitter = new EventEmitter();
private _baseHref: string;
constructor(public _platformStrategy: LocationStrategy,
@Optional() @Inject(appBaseHrefToken) href?: string) {
this._subject = new EventEmitter();
this._baseHref = stripTrailingSlash(
stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref()));
this._platformStrategy.onPopState((_) => this._onPopState(_));

View File

@ -33,11 +33,10 @@ export class ContinuationSegment extends Segment {
class StaticSegment extends Segment {
regex: string;
name: string;
name: string = '';
constructor(public string: string) {
super();
this.name = '';
this.regex = escapeRegex(string);
}
@ -46,8 +45,9 @@ class StaticSegment extends Segment {
@IMPLEMENTS(Segment)
class DynamicSegment {
regex: string;
constructor(public name: string) { this.regex = "([^/]+)"; }
regex: string = "([^/]+)";
constructor(public name: string) {}
generate(params: StringMap<string, string>): string {
if (!StringMapWrapper.contains(params, this.name)) {
@ -60,8 +60,8 @@ class DynamicSegment {
class StarSegment {
regex: string;
constructor(public name: string) { this.regex = "(.+)"; }
regex: string = "(.+)";
constructor(public name: string) {}
generate(params: StringMap<string, string>): string {
return normalizeBlank(StringMapWrapper.get(params, this.name));
@ -134,10 +134,6 @@ export class PathRecognizer {
terminal: boolean = true;
constructor(public path: string, public handler: any) {
this.segments = [];
// TODO: use destructuring assignment
// see https://github.com/angular/ts2dart/issues/158
var parsed = parsePathString(path);
var specificity = parsed['specificity'];
var segments = parsed['segments'];

View File

@ -22,15 +22,9 @@ import {PathRecognizer, ContinuationSegment} from './path_recognizer';
* components.
*/
export class RouteRecognizer {
names: Map<string, PathRecognizer>;
redirects: Map<string, string>;
matchers: Map<RegExp, PathRecognizer>;
constructor() {
this.names = new Map();
this.matchers = new Map();
this.redirects = new Map();
}
names: Map<string, PathRecognizer> = new Map();
redirects: Map<string, string> = new Map();
matchers: Map<RegExp, PathRecognizer> = new Map();
addRedirect(path: string, target: string): void {
if (path == '/') {
@ -113,6 +107,7 @@ export class RouteMatch {
params: StringMap<string, string>;
matchedUrl: string;
unmatchedUrl: string;
constructor({specificity, handler, params, matchedUrl, unmatchedUrl}: {
specificity?: number,
handler?: StringMap<string, any>,

View File

@ -29,9 +29,7 @@ import {Injectable} from 'angular2/di';
*/
@Injectable()
export class RouteRegistry {
_rules: Map<any, RouteRecognizer>;
constructor() { this._rules = new Map(); }
_rules: Map<any, RouteRecognizer> = new Map();
/**
* Given a component and a configuration object, add the route to this registry

View File

@ -1,6 +1,6 @@
import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
import {isBlank, isPresent, Type} from 'angular2/src/facade/lang';
import {isBlank, isPresent, Type, isArray} from 'angular2/src/facade/lang';
import {RouteRegistry} from './route_registry';
import {Pipeline} from './pipeline';
@ -28,25 +28,19 @@ import {Location} from './location';
* @exportedAs angular2/router
*/
export class Router {
navigating: boolean;
navigating: boolean = false;
lastNavigationAttempt: string;
previousUrl: string;
previousUrl: string = null;
private _currentInstruction: Instruction = null;
private _currentNavigation: Promise<any> = PromiseWrapper.resolve(true);
private _outlet: RouterOutlet = null;
private _subject: EventEmitter = new EventEmitter();
private _currentInstruction: Instruction;
private _currentNavigation: Promise<any>;
private _outlet: RouterOutlet;
private _subject: EventEmitter;
// todo(jeffbcross): rename _registry to registry since it is accessed from subclasses
// todo(jeffbcross): rename _pipeline to pipeline since it is accessed from subclasses
constructor(public _registry: RouteRegistry, public _pipeline: Pipeline, public parent: Router,
public hostComponent: any) {
this.navigating = false;
this.previousUrl = null;
this._outlet = null;
this._subject = new EventEmitter();
this._currentInstruction = null;
this._currentNavigation = PromiseWrapper.resolve(true);
}
public hostComponent: any) {}
/**
@ -88,8 +82,8 @@ export class Router {
* ]);
* ```
*/
config(config: any): Promise<any> {
if (config instanceof List) {
config(config: StringMap<string, any>| List<StringMap<string, any>>): Promise<any> {
if (isArray(config)) {
(<List<any>>config)
.forEach((configObject) => { this._registry.config(this.hostComponent, configObject); });
} else {

View File

@ -38,7 +38,7 @@ import {Renderer} from 'angular2/src/render/api';
})
export class RouterLink {
private _route: string;
private _params: StringMap<string, string>;
private _params: StringMap<string, string> = StringMapWrapper.create();
// the url displayed on the anchor element.
_visibleHref: string;
@ -46,15 +46,13 @@ export class RouterLink {
_navigationHref: string;
constructor(private _elementRef: ElementRef, private _router: Router, private _location: Location,
private _renderer: Renderer) {
this._params = StringMapWrapper.create();
}
private _renderer: Renderer) {}
set route(changes: string) { this._route = changes; }
set params(changes: StringMap<string, string>) { this._params = changes; }
onClick() {
onClick(): boolean {
this._router.navigate(this._navigationHref);
return false;
}

View File

@ -22,10 +22,10 @@ import {Instruction, RouteParams} from './instruction'
selector: 'router-outlet'
})
export class RouterOutlet {
private _childRouter: routerMod.Router;
private _componentRef: ComponentRef;
private _childRouter: routerMod.Router = null;
private _componentRef: ComponentRef = null;
private _elementRef: ElementRef;
private _currentInstruction: Instruction;
private _currentInstruction: Instruction = null;
private _injector: Injector;
constructor(elementRef: ElementRef, private _loader: DynamicComponentLoader,
@ -38,10 +38,6 @@ export class RouterOutlet {
this._injector = _injector.getAppInjector();
this._elementRef = elementRef;
this._childRouter = null;
this._componentRef = null;
this._currentInstruction = null;
this._parentRouter.registerOutlet(this);
}