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 --- docs/content/cookbook/deeplinking.ngdoc | 13 ++++++----- src/service/route.js | 30 +++++------------------- src/service/scope.js | 2 +- src/widgets.js | 41 ++++++++++++++++++++++----------- test/service/routeSpec.js | 14 +++++++++++ test/widgetsSpec.js | 12 ++++++++++ 6 files changed, 67 insertions(+), 45 deletions(-) diff --git a/docs/content/cookbook/deeplinking.ngdoc b/docs/content/cookbook/deeplinking.ngdoc index 1d6d196b..4db8d1c7 100644 --- a/docs/content/cookbook/deeplinking.ngdoc +++ b/docs/content/cookbook/deeplinking.ngdoc @@ -36,16 +36,17 @@ The two partials are defined in the following URLs: * ./examples/settings.html * ./examples/welcome.html - + -
+
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}); + } + } + } } }; }]; diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index 6c6828bc..97532f38 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -1,6 +1,20 @@ 'use strict'; describe('$route', function() { + + beforeEach(module(function() { + return function($rootScope, $controller) { + $rootScope.$on('$afterRouteChange', function(event, next) { + // emulate ng:view scope creation + if (next) { + next.scope = $rootScope.$new(); + next.controller && $controller(next.controller, {$scope: next.scope}); + } + }); + }; + })); + + it('should route and fire change event', function() { var log = '', lastRoute, diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index e762e7c0..3b245d11 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -633,6 +633,18 @@ describe('widget', function() { })); + it('should create controller instance on $afterRouteChange event', inject( + function($route, $rootScope) { + var controllerScope; + $route.current = { controller: function($scope) { controllerScope = $scope; } }; + $rootScope.$broadcast('$afterRouteChange', $route.current); + + expect(controllerScope.$parent.$id).toBe($rootScope.$id); + expect(controllerScope.$id).toBe($route.current.scope.$id); + } + )); + + it('should load content via xhr when route changes', function() { module(function($routeProvider) { $routeProvider.when('/foo', {template: 'myUrl1'}); -- cgit v1.2.3