$route redirection should interpolate variables

This commit is contained in:
Igor Minar 2011-02-01 20:16:30 -08:00
parent bf8013ad57
commit 8724e97b7e
2 changed files with 37 additions and 32 deletions

View file

@ -801,7 +801,7 @@ angularServiceInject('$route', function(location, $updateView) {
} }
}; };
function updateRoute(){ function updateRoute(){
var childScope, routeParams, pathParams; var childScope, routeParams, pathParams, redirectPath, segmentMatch, key;
$route.current = _null; $route.current = _null;
forEach(routes, function(rParams, rPath) { forEach(routes, function(rParams, rPath) {
@ -817,7 +817,18 @@ angularServiceInject('$route', function(location, $updateView) {
if(routeParams) { if(routeParams) {
if (routeParams.redirectTo) { if (routeParams.redirectTo) {
location.updateHash(routeParams.redirectTo); redirectPath = '';
forEach(routeParams.redirectTo.split(':'), function(segment, i) {
if (i==0) {
redirectPath += segment;
} else {
segmentMatch = segment.match(/(\w+)(.*)/);
key = segmentMatch[1];
redirectPath += pathParams[key] || location.hashSearch[key];
redirectPath += segmentMatch[2] || '';
}
});
location.updateHash(redirectPath);
$updateView(); //TODO this is to work around the $location<=>$browser issues $updateView(); //TODO this is to work around the $location<=>$browser issues
return; return;
} }

View file

@ -476,6 +476,10 @@ describe("service", function(){
expect($route.current.scope.notFoundProp).toBeUndefined(); expect($route.current.scope.notFoundProp).toBeUndefined();
expect(onChangeSpy).toHaveBeenCalled(); expect(onChangeSpy).toHaveBeenCalled();
}); });
});
describe('redirection', function() {
it('should support redirection via redirectTo property by updating $location', function() { it('should support redirection via redirectTo property by updating $location', function() {
var scope = angular.scope(), var scope = angular.scope(),
@ -519,51 +523,41 @@ describe("service", function(){
expect(onChangeSpy.callCount).toBe(1); expect(onChangeSpy.callCount).toBe(1);
}); });
it('should make parentScope configurable via parent()', function() {
it('should interpolate route variables in the redirected path from hashPath', function() {
var scope = angular.scope(), var scope = angular.scope(),
parentScope = scope.$new(),
$location = scope.$service('$location'), $location = scope.$service('$location'),
$browser = scope.$service('$browser'),
$route = scope.$service('$route'); $route = scope.$service('$route');
$route.parent(parentScope); $route.when('/foo/:id/foo/:subid/:ignoredId', {redirectTo: '/bar/:id/:subid/23'});
$route.when('/foo', {template: 'foo.html'}); $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
$route.otherwise({template: '404.html'});
scope.$eval(); scope.$eval();
expect($route.current.template).toBe('404.html'); $location.updateHash('/foo/id1/foo/subid3/gah');
expect($route.current.scope.$parent).toBe(parentScope); scope.$eval(); //triggers initial route change - match the redirect route
$browser.defer.flush(); //triger route change - match the route we redirected to
$location.updateHash('/foo'); expect($location.hash).toBe('/bar/id1/subid3/23');
scope.$eval(); expect($route.current.template).toBe('bar.html');
expect($route.current.template).toBe('foo.html');
expect($route.current.scope.$parent).toBe(parentScope);
}); });
it('should reload routes when reload() is called', function() { it('should interpolate route variables in the redirected path from hashSearch', function() {
var scope = angular.scope(), var scope = angular.scope(),
$location = scope.$service('$location'), $location = scope.$service('$location'),
$route = scope.$service('$route'), $browser = scope.$service('$browser'),
onChangeSpy = jasmine.createSpy('onChange'); $route = scope.$service('$route');
$route.when('', {template: 'foo.html'});
$route.onChange(onChangeSpy);
expect($route.current).toBeNull();
expect(onChangeSpy).not.toHaveBeenCalled();
$route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
$route.when('/foo/:id', {redirectTo: '/bar/:id/:subid/99'});
scope.$eval(); scope.$eval();
expect($location.hash).toBe(''); $location.hash = '/foo/id3?subid=sid1&ignored=true';
expect($route.current.template).toBe('foo.html'); scope.$eval(); //triggers initial route change - match the redirect route
expect(onChangeSpy.callCount).toBe(1); $browser.defer.flush(); //triger route change - match the route we redirected to
$route.reload(); expect($location.hash).toBe('/bar/id3/sid1/99');
scope.$eval(); expect($route.current.template).toBe('bar.html');
expect($location.hash).toBe('');
expect($route.current.template).toBe('foo.html');
expect(onChangeSpy.callCount).toBe(2);
}); });
}); });