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

Commit 15ecc6f

Browse files
committed
feat($route): allow chaining of whens and otherwise
Previously one had to write: $routeProvider.when('/foo', {...}); $routeProvider.when('/bar', {...}); $routeProvider.otherwise({...}); After this change it's just: $routeProvider. when('/foo', {...}). when('/bar', {...}). otherwise({...}); Breaks #when which used to return the route definition object but now returns self. Returning the route definition object is not very useful so its likely that nobody ever used it.
1 parent 53b2254 commit 15ecc6f

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/ng/route.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ function $RouteProvider(){
5151
* If the option is set to `false` and url in the browser changes, then
5252
* `$routeUpdate` event is broadcasted on the root scope.
5353
*
54-
* @returns {Object} route object
54+
* @returns {Object} self
5555
*
5656
* @description
5757
* Adds a new route definition to the `$route` service.
5858
*/
5959
this.when = function(path, route) {
60-
var routeDef = routes[path];
61-
if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true};
62-
if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions?
60+
routes[path] = extend({reloadOnSearch: true}, route);
6361

6462
// create redirection for trailing slashes
6563
if (path) {
@@ -70,7 +68,7 @@ function $RouteProvider(){
7068
routes[redirectPath] = {redirectTo: path};
7169
}
7270

73-
return routeDef;
71+
return this;
7472
};
7573

7674
/**
@@ -83,9 +81,11 @@ function $RouteProvider(){
8381
* is matched.
8482
*
8583
* @param {Object} params Mapping information to be assigned to `$route.current`.
84+
* @returns {Object} self
8685
*/
8786
this.otherwise = function(params) {
8887
this.when(null, params);
88+
return this;
8989
};
9090

9191

test/ng/routeParamsSpec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
describe('$routeParams', function() {
44
it('should publish the params into a service', function() {
55
module(function($routeProvider) {
6-
$routeProvider.when('/foo');
7-
$routeProvider.when('/bar/:barId');
6+
$routeProvider.when('/foo', {});
7+
$routeProvider.when('/bar/:barId', {});
88
});
99

1010
inject(function($rootScope, $route, $location, $routeParams) {

test/ng/routeSpec.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('$route', function() {
1010
module(function($routeProvider) {
1111
$routeProvider.when('/Book/:book/Chapter/:chapter',
1212
{controller: noop, template: 'Chapter.html'});
13-
$routeProvider.when('/Blank');
13+
$routeProvider.when('/Blank', {});
1414
});
1515
inject(function($route, $location, $rootScope) {
1616
$rootScope.$on('$beforeRouteChange', function(event, next, current) {
@@ -147,6 +147,24 @@ describe('$route', function() {
147147
});
148148

149149

150+
it('should chain whens and otherwise', function() {
151+
module(function($routeProvider){
152+
$routeProvider.when('/foo', {template: 'foo.html'}).
153+
otherwise({template: 'bar.html'}).
154+
when('/baz', {template: 'baz.html'});
155+
});
156+
157+
inject(function($route, $location, $rootScope) {
158+
$rootScope.$digest();
159+
expect($route.current.template).toBe('bar.html');
160+
161+
$location.url('/baz');
162+
$rootScope.$digest();
163+
expect($route.current.template).toBe('baz.html');
164+
});
165+
});
166+
167+
150168
it('should not fire $after/beforeRouteChange during bootstrap (if no route)', function() {
151169
var routeChangeSpy = jasmine.createSpy('route change');
152170

0 commit comments

Comments
 (0)