diff --git a/src/ng/route.js b/src/ng/route.js index 2b9d187adabe..864cb0ee7f19 100644 --- a/src/ng/route.js +++ b/src/ng/route.js @@ -51,15 +51,13 @@ function $RouteProvider(){ * If the option is set to `false` and url in the browser changes, then * `$routeUpdate` event is broadcasted on the root scope. * - * @returns {Object} route object + * @returns {Object} self * * @description * Adds a new route definition to the `$route` service. */ this.when = function(path, route) { - var routeDef = routes[path]; - if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true}; - if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions? + routes[path] = extend({reloadOnSearch: true}, route); // create redirection for trailing slashes if (path) { @@ -70,7 +68,7 @@ function $RouteProvider(){ routes[redirectPath] = {redirectTo: path}; } - return routeDef; + return this; }; /** @@ -83,9 +81,11 @@ function $RouteProvider(){ * is matched. * * @param {Object} params Mapping information to be assigned to `$route.current`. + * @returns {Object} self */ this.otherwise = function(params) { this.when(null, params); + return this; }; diff --git a/test/ng/routeParamsSpec.js b/test/ng/routeParamsSpec.js index d1b2ecb117a9..e3aac1a26213 100644 --- a/test/ng/routeParamsSpec.js +++ b/test/ng/routeParamsSpec.js @@ -3,8 +3,8 @@ describe('$routeParams', function() { it('should publish the params into a service', function() { module(function($routeProvider) { - $routeProvider.when('/foo'); - $routeProvider.when('/bar/:barId'); + $routeProvider.when('/foo', {}); + $routeProvider.when('/bar/:barId', {}); }); inject(function($rootScope, $route, $location, $routeParams) { diff --git a/test/ng/routeSpec.js b/test/ng/routeSpec.js index 88e54b9aaa35..b66cbb8edeec 100644 --- a/test/ng/routeSpec.js +++ b/test/ng/routeSpec.js @@ -10,7 +10,7 @@ describe('$route', function() { module(function($routeProvider) { $routeProvider.when('/Book/:book/Chapter/:chapter', {controller: noop, template: 'Chapter.html'}); - $routeProvider.when('/Blank'); + $routeProvider.when('/Blank', {}); }); inject(function($route, $location, $rootScope) { $rootScope.$on('$beforeRouteChange', function(event, next, current) { @@ -147,6 +147,24 @@ describe('$route', function() { }); + it('should chain whens and otherwise', function() { + module(function($routeProvider){ + $routeProvider.when('/foo', {template: 'foo.html'}). + otherwise({template: 'bar.html'}). + when('/baz', {template: 'baz.html'}); + }); + + inject(function($route, $location, $rootScope) { + $rootScope.$digest(); + expect($route.current.template).toBe('bar.html'); + + $location.url('/baz'); + $rootScope.$digest(); + expect($route.current.template).toBe('baz.html'); + }); + }); + + it('should not fire $after/beforeRouteChange during bootstrap (if no route)', function() { var routeChangeSpy = jasmine.createSpy('route change');