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.
This commit is contained in:
Igor Minar 2012-04-03 15:28:09 -07:00
parent 53b2254ea7
commit 15ecc6f366
3 changed files with 26 additions and 8 deletions

View file

@ -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;
};

View file

@ -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) {

View file

@ -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');