Skip to content

Commit

Permalink
fix 1699: support preventDefault() for $routeChangeStart
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-hamel committed Apr 13, 2013
1 parent b7b08ee commit f8ac46e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/ng/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ function $RouteProvider(){
* defined in `resolve` route property. Once all of the dependencies are resolved
* `$routeChangeSuccess` is fired.
*
* Call event.preventDefault() to prevent changing route but leave the browser Url
* to it's new location (as opposed to calling event.preventDefault() on $location's
* $locationChangeStart event which cancels changing the browser's Url).
*
*
* @param {Route} next Future route information.
* @param {Route} current Current route information.
*/
Expand Down Expand Up @@ -405,7 +410,9 @@ function $RouteProvider(){
$rootScope.$broadcast('$routeUpdate', last);
} else if (next || last) {
forceReload = false;
$rootScope.$broadcast('$routeChangeStart', next, last);
if ( $rootScope.$broadcast('$routeChangeStart', next, last).defaultPrevented ){
return;
}
$route.current = next;
if (next) {
if (next.redirectTo) {
Expand Down
22 changes: 22 additions & 0 deletions test/ng/routeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ describe('$route', function() {
});


it('should not change route when $routeChangeStart is canceled', function() {
module(function($routeProvider) {
$routeProvider.when('/somePath', {template: 'some path'});
});
inject(function($route, $location, $rootScope, $log) {
$rootScope.$on('$routeChangeStart', function(event) {
$log.info('$routeChangeStart');
event.preventDefault();
});

$rootScope.$on('$routeChangeSuccess', function(event) {
throw new Error('Should not get here');
});

$location.path('/somePath');
$rootScope.$digest();

expect($log.info.logs.shift()).toEqual(['$routeChangeStart']);
});
});


describe('should match a route that contains special chars in the path', function() {
beforeEach(module(function($routeProvider) {
$routeProvider.when('/$test.23/foo*(bar)/:baz', {templateUrl: 'test.html'});
Expand Down

1 comment on commit f8ac46e

@mishari00
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.