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

Commit

Permalink
feat($route): allow chaining of whens and otherwise
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
IgorMinar committed Apr 4, 2012
1 parent 53b2254 commit 15ecc6f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -70,7 +68,7 @@ function $RouteProvider(){
routes[redirectPath] = {redirectTo: path};
}

return routeDef;
return this;
};

/**
Expand All @@ -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;
};


Expand Down
4 changes: 2 additions & 2 deletions test/ng/routeParamsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 19 additions & 1 deletion test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit 15ecc6f

Please sign in to comment.