From f72607c281a15a59a685d8f73e1c68edd53147ad Mon Sep 17 00:00:00 2001 From: yoerlemans Date: Fri, 17 Jan 2014 11:07:02 +0100 Subject: [PATCH] Update route.js feat(ngRoute): change This change adds a new event on ngRoute called beforeRouteChange. With this change it is possible to cancel a locationChange based on route data. The problem that I am trying to fix here is described in #5581 --- src/ngRoute/route.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ngRoute/route.js b/src/ngRoute/route.js index 34f3f9ec05f1..ea66c36ffe4e 100644 --- a/src/ngRoute/route.js +++ b/src/ngRoute/route.js @@ -421,6 +421,25 @@ function $RouteProvider(){ * instance of the Controller. */ + /** + * @ngdoc event + * @name ngRoute.$route#beforeRouteChange + * @eventOf ngRoute.$route + * @eventType broadcast on root scope + * @description + * Broadcasted at the same time as locationChangeStart, + * passes through the event so the locationChange can be cancelled. + * Passes two other objects that represent the Routes of the current location + * and the one of the possible next. + * + * `$beforeRouteChange` is fired. + * + * @param {Object} angularEvent Synthetic event object. + * @param {Route} currentRoute current route information. + * @param {Route} nextRoute route information of the future route. + * @param {Object} angularEvent Synthetic event object of locationChangeStart. + */ + var forceReload = false, $route = { routes: routes, @@ -444,6 +463,13 @@ function $RouteProvider(){ }; $rootScope.$on('$locationChangeSuccess', updateRoute); + + $rootScope.$on("$locationChangeStart", function (event, next, current) { + var nextRoute = parseRoute(), + lastRoute = $route.current; + + $rootScope.$broadcast('$beforeRouteChange', lastRoute, nextRoute, event); + }); return $route;