fix(router): allow generating links with numeric params

This commit is contained in:
Brian Ford 2015-07-07 13:05:05 -07:00
parent 355ced92eb
commit d828664d0c
2 changed files with 15 additions and 4 deletions

View File

@ -5,8 +5,7 @@ import {
StringWrapper,
isPresent,
isBlank,
BaseException,
normalizeBlank
BaseException
} from 'angular2/src/facade/lang';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {
@ -29,6 +28,14 @@ export class Segment {
regex: string;
}
function normalizeString(obj: any): string {
if (isBlank(obj)) {
return null;
} else {
return obj.toString();
}
}
class ContinuationSegment extends Segment {
generate(params): string { return ''; }
}
@ -56,7 +63,7 @@ class DynamicSegment {
throw new BaseException(
`Route generator for '${this.name}' was not included in parameters passed.`);
}
return normalizeBlank(StringMapWrapper.get(params, this.name));
return normalizeString(StringMapWrapper.get(params, this.name));
}
}
@ -66,7 +73,7 @@ class StarSegment {
constructor(public name: string) {}
generate(params: StringMap<string, string>): string {
return normalizeBlank(StringMapWrapper.get(params, this.name));
return normalizeString(StringMapWrapper.get(params, this.name));
}
}

View File

@ -112,6 +112,10 @@ export function main() {
expect(recognizer.generate('user', {'name': 'misko'})['url']).toEqual('app/user/misko');
});
it('should generate URLs with numeric params', () => {
recognizer.addConfig('/app/page/:number', handler, 'page');
expect(recognizer.generate('page', {'number': 42})['url']).toEqual('app/page/42');
});
it('should throw in the absence of required params URLs', () => {
recognizer.addConfig('app/user/:name', handler, 'user');