8000 feat(routeParams): Add support to catch-all parameters in routes by lrlopez · Pull Request #1559 · angular/angular.js · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(routeParams): Add support to catch-all parameters in routes #1559

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(route): Add support to catch-all parameters in routes
This allows routeProvider to accept parameters that matches substrings even when they contain slashes if they are prefixed with an asterisk instead of a colon. For example, routes like edit/color/:color/largecode/*largecode will match with something like this http://appdomain.com/edit/color/brown/largecode/code/with/slashs.
  • Loading branch information
lrlopez committed Nov 12, 2012
commit 429261296f343671c40ec712e817ef774c1975f4
4 changes: 3 additions & 1 deletion src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function $RouteProvider(){
* @param {string} path Route path (matched against `$location.path`). If `$location.path`
* contains redundant trailing slash or is missing one, the route will still match and the
* `$location.path` will be updated to add or drop the trailing slash to exacly match the
* route definition.
* route definition. A path can contain single parameters prefixed with a colon or catch-all
* partials (multiple levels including slashes) if they are prefixed with an asterisk.
*
* @param {Object} route Mapping information to be assigned to `$route.current` on route
* match.
*
Expand Down
57 changes: 57 additions & 0 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,63 @@ describe('$route', function() {
});
});

it('should route and fire change event when catch-all params are used', function() {
var log = '',
lastRoute,
nextRoute;

module(function($routeProvider) {
$routeProvider.when('/Book1/:book/Chapter/:chapter/*highlight',
{controller: noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Book2/:book/*highlight/Chapter/:chapter',
{controller: noop, templateUrl: 'Chapter.html'});
$routeProvider.when('/Blank', {});
});
inject(function($route, $location, $rootScope) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
log += 'before();';
expect(current).toBe($route.current);
lastRoute = current;
nextRoute = next;
});
$rootScope.$on('$routeChangeSuccess', function(event, current, last) {
log += 'after();';
expect(current).toBe($route.current);
expect(lastRoute).toBe(last);
expect(nextRoute).toBe(current);
});

$location.path('/Book1/Moby/Chapter/Intro/one').search('p=123');
$rootScope.$digest();
$httpBackend.flush();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one', p:'123'});

log = '';
$location.path('/Blank').search('ignore');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({ignore:true});

log = '';
$location.path('/Book1/Moby/Chapter/Intro/one/two').search('p=123');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});

log = '';
$location.path('/Book2/Moby/one/two/Chapter/Intro').search('p=123');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', highlight:'one/two', p:'123'});

log = '';
$location.path('/NONE');
$rootScope.$digest();
expect(log).toEqual('before();after();');
expect($route.current).toEqual(null);
});
});

it('should not change route when location is canceled', function() {
module(function($routeProvider) {
Expand Down
0