From b625f2471a2bbc96e2c39df2320715511ce4297c Mon Sep 17 00:00:00 2001 From: vsavkin Date: Mon, 2 May 2016 15:03:00 -0700 Subject: [PATCH] feat(router): make RouterLink accept single values --- .../router/src/directives/router_link.ts | 14 ++-- .../@angular/router/test/router_link_spec.ts | 64 +++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 modules/@angular/router/test/router_link_spec.ts diff --git a/modules/@angular/router/src/directives/router_link.ts b/modules/@angular/router/src/directives/router_link.ts index c2278fd275..cf2ecaf00b 100644 --- a/modules/@angular/router/src/directives/router_link.ts +++ b/modules/@angular/router/src/directives/router_link.ts @@ -16,7 +16,7 @@ import { } from '@angular/core'; import {RouterOutletMap, Router} from '../router'; import {RouteSegment, UrlSegment, Tree} from '../segments'; -import {isString, isPresent} from '../facade/lang'; +import {isString, isArray, isPresent} from '../facade/lang'; import {ObservableWrapper} from '../facade/async'; @Directive({selector: '[routerLink]'}) @@ -25,8 +25,8 @@ export class RouterLink implements OnDestroy { private _comands: any[] = []; private _subscription: any; - @HostBinding() private href: string; - @HostBinding('class.router-link-active') private isActive: boolean = false; + @HostBinding() href: string; + @HostBinding('class.router-link-active') isActive: boolean = false; constructor(@Optional() private _routeSegment: RouteSegment, private _router: Router) { this._subscription = @@ -36,8 +36,12 @@ export class RouterLink implements OnDestroy { ngOnDestroy() { ObservableWrapper.dispose(this._subscription); } @Input() - set routerLink(data: any[]) { - this._comands = data; + set routerLink(data: any[]|any) { + if (isArray(data)) { + this._comands = data; + } else { + this._comands = [data]; + } this._updateTargetUrlAndHref(); } diff --git a/modules/@angular/router/test/router_link_spec.ts b/modules/@angular/router/test/router_link_spec.ts new file mode 100644 index 0000000000..5cde662618 --- /dev/null +++ b/modules/@angular/router/test/router_link_spec.ts @@ -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 { +} \ No newline at end of file