refactor(router): RouteData as a type

BREAKING CHANGE

The ROUTE_DATA token has been removed and replaced with a type RouteData,
allowing a type injection like we do with RouteParams.

Before:

    constructor(routeParams: RouteParams, @Inject(ROUTE_DATA) routeData) {
      let id = routeParams.get('id');
      let name = ROUTE_DATA.name;
    }

After:

    constructor(routeParams: RouteParams, routeData: RouteData) {
      let id = routeParams.get('id');
      let name = routeData.get('name');
    }

Fixes #4392

Closes #4428
This commit is contained in:
cexbrayat
2015-09-30 14:48:58 +02:00
committed by Brian Ford
parent fbe748f273
commit b87da8f47c
11 changed files with 79 additions and 67 deletions
@@ -26,7 +26,7 @@ import {
} from 'angular2/src/core/facade/async';
import {RootRouter} from 'angular2/src/router/router';
import {Router, RouterOutlet, RouterLink, RouteParams, ROUTE_DATA} from 'angular2/router';
import {Router, RouterOutlet, RouterLink, RouteParams} from 'angular2/router';
import {
RouteConfig,
Route,
@@ -16,11 +16,10 @@ import {
} from 'angular2/testing_internal';
import {provide, Component, View, Injector, Inject} from 'angular2/core';
import {CONST, NumberWrapper, isPresent, Json} from 'angular2/src/core/facade/lang';
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
import {RootRouter} from 'angular2/src/router/router';
import {Router, RouterOutlet, RouterLink, RouteParams, ROUTE_DATA} from 'angular2/router';
import {Router, RouterOutlet, RouterLink, RouteParams, RouteData} from 'angular2/router';
import {
RouteConfig,
Route,
@@ -200,13 +199,12 @@ export function main() {
it('should inject route data into component', inject([AsyncTestCompleter], (async) => {
compile()
.then((_) => rtr.config([
new Route({path: '/route-data', component: RouteDataCmp, data: {'isAdmin': true}})
new Route({path: '/route-data', component: RouteDataCmp, data: {isAdmin: true}})
]))
.then((_) => rtr.navigateByUrl('/route-data'))
.then((_) => {
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement)
.toHaveText(Json.stringify({'isAdmin': true}));
expect(rootTC.debugElement.nativeElement).toHaveText('true');
async.done();
});
}));
@@ -221,13 +219,12 @@ export function main() {
.then((_) => rtr.navigateByUrl('/route-data'))
.then((_) => {
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement)
.toHaveText(Json.stringify({'isAdmin': true}));
expect(rootTC.debugElement.nativeElement).toHaveText('true');
async.done();
});
}));
it('should inject null if the route has no data property',
it('should inject empty object if the route has no data property',
inject([AsyncTestCompleter], (async) => {
compile()
.then((_) => rtr.config(
@@ -235,34 +232,7 @@ export function main() {
.then((_) => rtr.navigateByUrl('/route-data-default'))
.then((_) => {
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement).toHaveText('null');
async.done();
});
}));
it('should allow an array as the route data', inject([AsyncTestCompleter], (async) => {
compile()
.then((_) => rtr.config([
new Route({path: '/route-data-array', component: RouteDataCmp, data: [1, 2, 3]})
]))
.then((_) => rtr.navigateByUrl('/route-data-array'))
.then((_) => {
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement).toHaveText(Json.stringify([1, 2, 3]));
async.done();
});
}));
it('should allow a string as the route data', inject([AsyncTestCompleter], (async) => {
compile()
.then((_) => rtr.config([
new Route(
{path: '/route-data-string', component: RouteDataCmp, data: 'hello world'})
]))
.then((_) => rtr.navigateByUrl('/route-data-string'))
.then((_) => {
rootTC.detectChanges();
expect(rootTC.debugElement.nativeElement).toHaveText(Json.stringify('hello world'));
expect(rootTC.debugElement.nativeElement).toHaveText('');
async.done();
});
}));
@@ -298,10 +268,8 @@ function AsyncRouteDataCmp() {
@Component({selector: 'data-cmp'})
@View({template: "{{myData}}"})
class RouteDataCmp {
myData: string;
constructor(@Inject(ROUTE_DATA) data: any) {
this.myData = isPresent(data) ? Json.stringify(data) : 'null';
}
myData: boolean;
constructor(data: RouteData) { this.myData = data.get('isAdmin'); }
}
@Component({selector: 'user-cmp'})
@@ -32,9 +32,13 @@ import {
} from 'angular2/router';
import {DOM} from 'angular2/src/core/dom/dom_adapter';
import {ComponentInstruction_} from "angular2/src/router/instruction";
import {ComponentInstruction_} from 'angular2/src/router/instruction';
import {PathRecognizer} from 'angular2/src/router/path_recognizer';
import {SyncRouteHandler} from 'angular2/src/router/sync_route_handler';
var dummyInstruction = new Instruction(new ComponentInstruction_('detail', [], null), null, {});
let dummyPathRecognizer = new PathRecognizer('', new SyncRouteHandler(null));
let dummyInstruction =
new Instruction(new ComponentInstruction_('detail', [], dummyPathRecognizer), null, {});
export function main() {
describe('router-link directive', function() {