From 875c63fae06ce11fa58767707b216ead6e278709 Mon Sep 17 00:00:00 2001 From: tminglei Date: Mon, 23 Jun 2014 22:36:38 +0800 Subject: [PATCH 1/2] Add 'otherwise' wrapper method to route-segment --- src/route-segment.js | 11 ++++++++++- test/unit/route-segment.spec.js | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/route-segment.js b/src/route-segment.js index 2aed975..4f1a1c1 100644 --- a/src/route-segment.js +++ b/src/route-segment.js @@ -126,7 +126,16 @@ mod.provider( '$routeSegment', segmentRoutes[name] = route; return this; }; - + + /** + * The shorthand for $routeProvider.otherwise() method with specified route. + * @param {string} route Route URL, e.g. '/' + */ + $routeSegmentProvider.otherwise = function(route) { + $routeProvider.otherwise({redirectTo: route}); + return this; + }; + // Extending the provider with the methods of rootPointer // to start configuration. angular.extend($routeSegmentProvider, rootPointer); diff --git a/test/unit/route-segment.spec.js b/test/unit/route-segment.spec.js index 99f802c..eb0415b 100644 --- a/test/unit/route-segment.spec.js +++ b/test/unit/route-segment.spec.js @@ -170,6 +170,15 @@ describe('route segment', function() { }); + it('should redirect to / when invalid section', function () { + $routeSegmentProvider.otherwise('/1'); + + $location.path('/invalid'); + $rootScope.$broadcast('$locationChangeSuccess', $location.absUrl(), null); + $rootScope.$on('$locationChangeSuccess', function() { + expect($location.url()).toBe('/1'); + }); + }); it('should auto-fetch templateUrl by $http', function () { From f25de8b45497cf8cb63c9383b44cd31ab83c6d63 Mon Sep 17 00:00:00 2001 From: tminglei Date: Tue, 24 Jun 2014 07:23:38 +0800 Subject: [PATCH 2/2] update 'otherwise' doc --- src/route-segment.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/route-segment.js b/src/route-segment.js index 4f1a1c1..32f7a51 100644 --- a/src/route-segment.js +++ b/src/route-segment.js @@ -129,7 +129,7 @@ mod.provider( '$routeSegment', /** * The shorthand for $routeProvider.otherwise() method with specified route. - * @param {string} route Route URL, e.g. '/' + * @param {string|function} route Route URL, e.g. '/'; or function that return a URL */ $routeSegmentProvider.otherwise = function(route) { $routeProvider.otherwise({redirectTo: route}); @@ -418,7 +418,7 @@ mod.provider( '$routeSegment', params.watcher, {}, {segment: $routeSegment.chain[index]}); - } + }; var lastWatcherValue = getWatcherValue();