From e31d1c287d972d633bdaf9c385d3012192f64918 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 15 Feb 2012 21:49:07 -0800 Subject: refactor($route): remove .parent(); ng:view scope creation --- src/service/route.js | 30 ++++++------------------------ src/service/scope.js | 2 +- src/widgets.js | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/service/route.js b/src/service/route.js index 7cdcfd05..b7f8bd02 100644 --- a/src/service/route.js +++ b/src/service/route.js @@ -135,8 +135,8 @@ function $RouteProvider(){ }; - this.$get = ['$rootScope', '$location', '$routeParams', '$controller', - function( $rootScope, $location, $routeParams, $controller) { + this.$get = ['$rootScope', '$location', '$routeParams', + function( $rootScope, $location, $routeParams) { /** * @ngdoc event * @name angular.module.ng.$route#$beforeRouteChange @@ -185,28 +185,11 @@ function $RouteProvider(){ */ var matcher = switchRouteMatcher, - parentScope = $rootScope, dirty = 0, forceReload = false, $route = { routes: routes, - /** - * @ngdoc method - * @name angular.module.ng.$route#parent - * @methodOf angular.module.ng.$route - * - * @param {Scope} [scope=rootScope] Scope to be used as parent for newly created - * `$route.current.scope` scopes. - * - * @description - * Sets a scope to be used as the parent scope for scopes created on route change. If not - * set, defaults to the root scope. - */ - parent: function(scope) { - if (scope) parentScope = scope; - }, - /** * @ngdoc method * @name angular.module.ng.$route#reload @@ -266,7 +249,10 @@ function $RouteProvider(){ } else { forceReload = false; $rootScope.$broadcast('$beforeRouteChange', next, last); - last && last.scope && last.scope.$destroy(); + if (last && last.scope) { + last.scope.$destroy(); + last.scope = null; + } $route.current = next; if (next) { if (next.redirectTo) { @@ -279,10 +265,6 @@ function $RouteProvider(){ } } else { copy(next.params, $routeParams); - next.scope = parentScope.$new(); - if (next.controller) { - $controller(next.controller, {$scope: next.scope}); - } } } $rootScope.$broadcast('$afterRouteChange', next, last); diff --git a/src/service/scope.js b/src/service/scope.js index c8ee9298..a2de67e9 100644 --- a/src/service/scope.js +++ b/src/service/scope.js @@ -574,7 +574,7 @@ function $RootScopeProvider(){ * * The event listener function format is: `function(event)`. The `event` object passed into the * listener has the following attributes - * + * * - `targetScope` - {Scope}: the scope on which the event was `$emit`-ed or `$broadcast`-ed. * - `currentScope` - {Scope}: the current scope which is handling the event. * - `name` - {string}: Name of the event. diff --git a/src/widgets.js b/src/widgets.js index 613ae1b4..bc41d761 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -498,23 +498,23 @@ var ngNonBindableDirective = valueFn({ terminal: true }); * * * @example - + -
+
overview | bootstrap | undefined @@ -538,14 +538,18 @@ var ngNonBindableDirective = valueFn({ terminal: true }); */ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$compile', - function($http, $templateCache, $route, $anchorScroll, $compile) { + '$controller', + function($http, $templateCache, $route, $anchorScroll, $compile, + $controller) { return { terminal: true, link: function(scope, element) { var changeCounter = 0; - scope.$on('$afterRouteChange', function() { + processRoute($route.current); + scope.$on('$afterRouteChange', function(event, next) { changeCounter++; + processRoute(next); }); scope.$watch(function() {return changeCounter;}, function(newChangeCounter) { @@ -571,6 +575,15 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c clearContent(); } }); + + function processRoute(route) { + if (route) { + route.scope = scope.$new(); + if (route.controller) { + $controller(route.controller, {$scope: route.scope}); + } + } + } } }; }]; -- cgit v1.2.3