diff options
| -rw-r--r-- | src/services.js | 15 | ||||
| -rw-r--r-- | test/servicesSpec.js | 54 |
2 files changed, 37 insertions, 32 deletions
diff --git a/src/services.js b/src/services.js index b215dcdf..9dfdd35e 100644 --- a/src/services.js +++ b/src/services.js @@ -801,7 +801,7 @@ angularServiceInject('$route', function(location, $updateView) { } }; function updateRoute(){ - var childScope, routeParams, pathParams; + var childScope, routeParams, pathParams, redirectPath, segmentMatch, key; $route.current = _null; forEach(routes, function(rParams, rPath) { @@ -817,7 +817,18 @@ angularServiceInject('$route', function(location, $updateView) { if(routeParams) { 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 return; } diff --git a/test/servicesSpec.js b/test/servicesSpec.js index b3f6ec10..637f767c 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -476,6 +476,10 @@ describe("service", function(){ expect($route.current.scope.notFoundProp).toBeUndefined(); expect(onChangeSpy).toHaveBeenCalled(); }); + }); + + + describe('redirection', function() { it('should support redirection via redirectTo property by updating $location', function() { var scope = angular.scope(), @@ -519,51 +523,41 @@ describe("service", function(){ 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(), - parentScope = scope.$new(), $location = scope.$service('$location'), + $browser = scope.$service('$browser'), $route = scope.$service('$route'); - $route.parent(parentScope); - $route.when('/foo', {template: 'foo.html'}); - $route.otherwise({template: '404.html'}); - + $route.when('/foo/:id/foo/:subid/:ignoredId', {redirectTo: '/bar/:id/:subid/23'}); + $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); scope.$eval(); - expect($route.current.template).toBe('404.html'); - expect($route.current.scope.$parent).toBe(parentScope); - - $location.updateHash('/foo'); - scope.$eval(); + $location.updateHash('/foo/id1/foo/subid3/gah'); + scope.$eval(); //triggers initial route change - match the redirect route + $browser.defer.flush(); //triger route change - match the route we redirected to - expect($route.current.template).toBe('foo.html'); - expect($route.current.scope.$parent).toBe(parentScope); + expect($location.hash).toBe('/bar/id1/subid3/23'); + expect($route.current.template).toBe('bar.html'); }); - 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(), $location = scope.$service('$location'), - $route = scope.$service('$route'), - onChangeSpy = jasmine.createSpy('onChange'); - - $route.when('', {template: 'foo.html'}); - $route.onChange(onChangeSpy); - expect($route.current).toBeNull(); - expect(onChangeSpy).not.toHaveBeenCalled(); + $browser = scope.$service('$browser'), + $route = scope.$service('$route'); + $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); + $route.when('/foo/:id', {redirectTo: '/bar/:id/:subid/99'}); scope.$eval(); - expect($location.hash).toBe(''); - expect($route.current.template).toBe('foo.html'); - expect(onChangeSpy.callCount).toBe(1); - - $route.reload(); - scope.$eval(); + $location.hash = '/foo/id3?subid=sid1&ignored=true'; + scope.$eval(); //triggers initial route change - match the redirect route + $browser.defer.flush(); //triger route change - match the route we redirected to - expect($location.hash).toBe(''); - expect($route.current.template).toBe('foo.html'); - expect(onChangeSpy.callCount).toBe(2); + expect($location.hash).toBe('/bar/id3/sid1/99'); + expect($route.current.template).toBe('bar.html'); }); }); |
