feat(router): make RouterLink accept single values

This commit is contained in:
vsavkin 2016-05-02 15:03:00 -07:00
parent 3aa2606ff1
commit b625f2471a
2 changed files with 73 additions and 5 deletions

View File

@ -16,7 +16,7 @@ import {
} from '@angular/core'; } from '@angular/core';
import {RouterOutletMap, Router} from '../router'; import {RouterOutletMap, Router} from '../router';
import {RouteSegment, UrlSegment, Tree} from '../segments'; import {RouteSegment, UrlSegment, Tree} from '../segments';
import {isString, isPresent} from '../facade/lang'; import {isString, isArray, isPresent} from '../facade/lang';
import {ObservableWrapper} from '../facade/async'; import {ObservableWrapper} from '../facade/async';
@Directive({selector: '[routerLink]'}) @Directive({selector: '[routerLink]'})
@ -25,8 +25,8 @@ export class RouterLink implements OnDestroy {
private _comands: any[] = []; private _comands: any[] = [];
private _subscription: any; private _subscription: any;
@HostBinding() private href: string; @HostBinding() href: string;
@HostBinding('class.router-link-active') private isActive: boolean = false; @HostBinding('class.router-link-active') isActive: boolean = false;
constructor(@Optional() private _routeSegment: RouteSegment, private _router: Router) { constructor(@Optional() private _routeSegment: RouteSegment, private _router: Router) {
this._subscription = this._subscription =
@ -36,8 +36,12 @@ export class RouterLink implements OnDestroy {
ngOnDestroy() { ObservableWrapper.dispose(this._subscription); } ngOnDestroy() { ObservableWrapper.dispose(this._subscription); }
@Input() @Input()
set routerLink(data: any[]) { set routerLink(data: any[]|any) {
this._comands = data; if (isArray(data)) {
this._comands = <any[]>data;
} else {
this._comands = [data];
}
this._updateTargetUrlAndHref(); this._updateTargetUrlAndHref();
} }

View File

@ -0,0 +1,64 @@
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
xdescribe,
describe,
expect,
iit,
inject,
beforeEachProviders,
it,
xit
} from '@angular/core/testing/testing_internal';
import {provide, Component, ComponentResolver} from '@angular/core';
import {RouterLink} from '../src/directives/router_link';
import {
Router,
RouterOutletMap,
RouteSegment,
Route,
ROUTER_DIRECTIVES,
Routes,
RouterUrlSerializer,
DefaultRouterUrlSerializer,
OnActivate,
CanDeactivate
} from '@angular/router';
import {Location} from '@angular/common';
import {SpyLocation} from '@angular/common/testing';
export function main() {
describe('RouterLink', () => {
beforeEachProviders(() => [
provide(RouterUrlSerializer, {useClass: DefaultRouterUrlSerializer}),
RouterOutletMap,
provide(Location, {useClass: SpyLocation}),
provide(Router,
{
useFactory: (resolver, urlParser, outletMap, location) => new Router(
"RootComponent", RootCmp, resolver, urlParser, outletMap, location),
deps: [ComponentResolver, RouterUrlSerializer, RouterOutletMap, Location]
})
]);
describe("routerLink=", () => {
it("should accept an array of commands", inject([Router], (router) => {
let link = new RouterLink(null, router);
link.routerLink = ['/one', 11];
expect(link.href).toEqual("/one/11");
}));
it("should accept a single command", inject([Router], (router) => {
let link = new RouterLink(null, router);
link.routerLink = '/one/11';
expect(link.href).toEqual("/one/11");
}));
});
});
}
@Component({template: ''})
class RootCmp {
}