From 3bc1b19a695eb91a73653c3f79769a9eefc7442d Mon Sep 17 00:00:00 2001 From: bartes Date: Fri, 18 Nov 2011 23:42:41 +0100 Subject: [PATCH 1/3] added possibility to stop route changing --- src/service/route.js | 15 ++++++++++++++- test/service/routeSpec.js | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/service/route.js b/src/service/route.js index 97c9cee00ea5..1d4e5cf19211 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -117,6 +117,7 @@ function $RouteProvider(){ parentScope = $rootScope, dirty = 0, forceReload = false, + stopUpdate = false, $route = { routes: routes, @@ -251,6 +252,14 @@ function $RouteProvider(){ return match ? dst : null; } + function disableUpdate() { + stopUpdate = true; + } + + function enableUpdate() { + stopUpdate = true; + } + function updateRoute() { var next = parseRoute(), last = $route.current, @@ -263,7 +272,11 @@ function $RouteProvider(){ last.scope && last.scope.$emit('$routeUpdate'); } else { forceReload = false; - $rootScope.$broadcast('$beforeRouteChange', next, last); + $rootScope.$broadcast('$beforeRouteChange', next, last, disableUpdate); + if(stopUpdate) { + enableUpdate(); + return; + } last && last.scope && last.scope.$destroy(); $route.current = next; if (next) { diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index fed3faef363d..365a4b2f4d41 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -48,6 +48,15 @@ describe('$route', function() { $route.when('/NONE', {template:'instant update'}); $rootScope.$digest(); expect($route.current.template).toEqual('instant update'); + + log = ''; + $rootScope.$on('$beforeRouteChange', function(event, next, current, stopCallback) { + log += 'stop();'; + stopCallback(); + }); + $location.path('/Blank').search('ignore'); + $rootScope.$digest(); + expect(log).toEqual('before();stop();'); })); From 3a4390616f09442446494ee9b7671e1eeab86c1a Mon Sep 17 00:00:00 2001 From: bartes Date: Sun, 27 Nov 2011 10:48:29 +0100 Subject: [PATCH 2/3] reverted state of location url after stopping route change --- src/service/route.js | 31 +++++++++++++++++++++---------- test/service/routeSpec.js | 7 ++++++- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/service/route.js b/src/service/route.js index 1d4e5cf19211..11debebea745 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -117,7 +117,7 @@ function $RouteProvider(){ parentScope = $rootScope, dirty = 0, forceReload = false, - stopUpdate = false, + stopUpdateUrl, $route = { routes: routes, @@ -222,7 +222,7 @@ function $RouteProvider(){ } }; - $rootScope.$watch(function() { return dirty + $location.url(); }, updateRoute); + $rootScope.$watch(function() { return {dirty: dirty, url: $location.url()}; }, updateRoute); return $route; @@ -252,15 +252,22 @@ function $RouteProvider(){ return match ? dst : null; } - function disableUpdate() { - stopUpdate = true; + function disableUpdate(backUrl) { + stopUpdateUrl = backUrl; + $location.url(backUrl); } - function enableUpdate() { - stopUpdate = true; + function enableUpdate(newUrl) { + if(equals(newUrl, stopUpdateUrl)){ + stopUpdateUrl = undefined; + } + } + + function isUpdateStopped(){ + return !!stopUpdateUrl; } - function updateRoute() { + function updateRoute(scope, newValue, oldValue) { var next = parseRoute(), last = $route.current, Controller; @@ -272,9 +279,13 @@ function $RouteProvider(){ last.scope && last.scope.$emit('$routeUpdate'); } else { forceReload = false; - $rootScope.$broadcast('$beforeRouteChange', next, last, disableUpdate); - if(stopUpdate) { - enableUpdate(); + if(!isUpdateStopped()) { + $rootScope.$broadcast('$beforeRouteChange', next, last, function(){ + disableUpdate(oldValue.url); + }); + } + if(isUpdateStopped()) { + enableUpdate(newValue.url); return; } last && last.scope && last.scope.$destroy(); diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index 365a4b2f4d41..90de5251fd2d 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -49,14 +49,19 @@ describe('$route', function() { $rootScope.$digest(); expect($route.current.template).toEqual('instant update'); + var lastPath = $location.path(); + var lastSearch = $location.search(); + log = ''; $rootScope.$on('$beforeRouteChange', function(event, next, current, stopCallback) { log += 'stop();'; stopCallback(); }); - $location.path('/Blank').search('ignore'); + $location.path('/Book/Moby2/Chapter/Intro2').search('p=1234'); $rootScope.$digest(); expect(log).toEqual('before();stop();'); + expect($location.path()).toEqual(lastPath); + expect($location.search()).toEqual(lastSearch); })); From fafe12e0881f611c658c85bde6ccee73702932f7 Mon Sep 17 00:00:00 2001 From: bartes Date: Sun, 27 Nov 2011 10:51:36 +0100 Subject: [PATCH 3/3] added docs --- src/service/route.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/service/route.js b/src/service/route.js index 11debebea745..5dee031c99e2 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -75,6 +75,7 @@ function $RouteProvider(){ * * @param {Route} next Future route information. * @param {Route} current Current route information. + * @param {Function} function which stops route change and reverts location to previous state. * * The `Route` object extends the route definition with the following properties. *