Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e31d1c2

Browse files
mheveryvojtajina
authored andcommitted
refactor($route): remove .parent(); ng:view scope creation
1 parent f16bd2f commit e31d1c2

File tree

6 files changed

+67
-45
lines changed

6 files changed

+67
-45
lines changed

docs/content/cookbook/deeplinking.ngdoc

+7-6
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ The two partials are defined in the following URLs:
3636
* <a href="./examples/settings.html" ng:ext-link>./examples/settings.html</a>
3737
* <a href="./examples/welcome.html" ng:ext-link>./examples/welcome.html</a>
3838

39-
<doc:example>
39+
<doc:example module="deepLinking">
4040
<doc:source jsfiddle="false">
4141
<script>
42+
angular.module('deepLinking', [])
43+
.config(function($routeProvider) {
44+
$routeProvider.when("/welcome", {template:'./examples/welcome.html', controller:WelcomeCntl});
45+
$routeProvider.when("/settings", {template:'./examples/settings.html', controller:SettingsCntl});
46+
});
47+
4248
AppCntl.$inject = ['$scope', '$route']
4349
function AppCntl($scope, $route) {
44-
// define routes
45-
$route.when("/welcome", {template:'./examples/welcome.html', controller:WelcomeCntl});
46-
$route.when("/settings", {template:'./examples/settings.html', controller:SettingsCntl});
47-
$route.parent($scope);
48-
4950
// initialize the model to something useful
5051
$scope.person = {
5152
name:'anonymous',

src/service/route.js

+6-24
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ function $RouteProvider(){
135135
};
136136

137137

138-
this.$get = ['$rootScope', '$location', '$routeParams', '$controller',
139-
function( $rootScope, $location, $routeParams, $controller) {
138+
this.$get = ['$rootScope', '$location', '$routeParams',
139+
function( $rootScope, $location, $routeParams) {
140140
/**
141141
* @ngdoc event
142142
* @name angular.module.ng.$route#$beforeRouteChange
@@ -185,28 +185,11 @@ function $RouteProvider(){
185185
*/
186186

187187
var matcher = switchRouteMatcher,
188-
parentScope = $rootScope,
189188
dirty = 0,
190189
forceReload = false,
191190
$route = {
192191
routes: routes,
193192

194-
/**
195-
* @ngdoc method
196-
* @name angular.module.ng.$route#parent
197-
* @methodOf angular.module.ng.$route
198-
*
199-
* @param {Scope} [scope=rootScope] Scope to be used as parent for newly created
200-
* `$route.current.scope` scopes.
201-
*
202-
* @description
203-
* Sets a scope to be used as the parent scope for scopes created on route change. If not
204-
* set, defaults to the root scope.
205-
*/
206-
parent: function(scope) {
207-
if (scope) parentScope = scope;
208-
},
209-
210193
/**
211194
* @ngdoc method
212195
* @name angular.module.ng.$route#reload
@@ -266,7 +249,10 @@ function $RouteProvider(){
266249
} else {
267250
forceReload = false;
268251
$rootScope.$broadcast('$beforeRouteChange', next, last);
269-
last && last.scope && last.scope.$destroy();
252+
if (last && last.scope) {
253+
last.scope.$destroy();
254+
last.scope = null;
255+
}
270256
$route.current = next;
271257
if (next) {
272258
if (next.redirectTo) {
@@ -279,10 +265,6 @@ function $RouteProvider(){
279265
}
280266
} else {
281267
copy(next.params, $routeParams);
282-
next.scope = parentScope.$new();
283-
if (next.controller) {
284-
$controller(next.controller, {$scope: next.scope});
285-
}
286268
}
287269
}
288270
$rootScope.$broadcast('$afterRouteChange', next, last);

src/service/scope.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ function $RootScopeProvider(){
574574
*
575575
* The event listener function format is: `function(event)`. The `event` object passed into the
576576
* listener has the following attributes
577-
*
577+
*
578578
* - `targetScope` - {Scope}: the scope on which the event was `$emit`-ed or `$broadcast`-ed.
579579
* - `currentScope` - {Scope}: the current scope which is handling the event.
580580
* - `name` - {string}: Name of the event.

src/widgets.js

+27-14
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,23 @@ var ngNonBindableDirective = valueFn({ terminal: true });
498498
*
499499
*
500500
* @example
501-
<doc:example>
501+
<doc:example module="ngView">
502502
<doc:source jsfiddle="false">
503503
<script>
504-
function MyCtrl($route) {
505-
$route.when('/overview',
506-
{ controller: OverviewCtrl,
507-
template: 'partials/guide/dev_guide.overview.html'});
508-
$route.when('/bootstrap',
509-
{ controller: BootstrapCtrl,
510-
template: 'partials/guide/dev_guide.bootstrap.auto_bootstrap.html'});
511-
};
512-
MyCtrl.$inject = ['$route'];
513-
514504
function BootstrapCtrl() {}
515505
function OverviewCtrl() {}
506+
507+
angular.module('ngView', [])
508+
.config(function($routeProvider) {
509+
$routeProvider.when('/overview',
510+
{ controller: OverviewCtrl,
511+
template: 'partials/guide/dev_guide.overview.html'});
512+
$routeProvider.when('/bootstrap',
513+
{ controller: BootstrapCtrl,
514+
template: 'partials/guide/dev_guide.bootstrap.auto_bootstrap.html'});
515+
});
516516
</script>
517-
<div ng:controller="MyCtrl">
517+
<div>
518518
<a href="overview">overview</a> |
519519
<a href="bootstrap">bootstrap</a> |
520520
<a href="undefined">undefined</a>
@@ -538,14 +538,18 @@ var ngNonBindableDirective = valueFn({ terminal: true });
538538
</doc:example>
539539
*/
540540
var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$compile',
541-
function($http, $templateCache, $route, $anchorScroll, $compile) {
541+
'$controller',
542+
function($http, $templateCache, $route, $anchorScroll, $compile,
543+
$controller) {
542544
return {
543545
terminal: true,
544546
link: function(scope, element) {
545547
var changeCounter = 0;
546548

547-
scope.$on('$afterRouteChange', function() {
549+
processRoute($route.current);
550+
scope.$on('$afterRouteChange', function(event, next) {
548551
changeCounter++;
552+
processRoute(next);
549553
});
550554

551555
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
@@ -571,6 +575,15 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
571575
clearContent();
572576
}
573577
});
578+
579+
function processRoute(route) {
580+
if (route) {
581+
route.scope = scope.$new();
582+
if (route.controller) {
583+
$controller(route.controller, {$scope: route.scope});
584+
}
585+
}
586+
}
574587
}
575588
};
576589
}];

test/service/routeSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
'use strict';
22

33
describe('$route', function() {
4+
5+
beforeEach(module(function() {
6+
return function($rootScope, $controller) {
7+
$rootScope.$on('$afterRouteChange', function(event, next) {
8+
// emulate ng:view scope creation
9+
if (next) {
10+
next.scope = $rootScope.$new();
11+
next.controller && $controller(next.controller, {$scope: next.scope});
12+
}
13+
});
14+
};
15+
}));
16+
17+
418
it('should route and fire change event', function() {
519
var log = '',
620
lastRoute,

test/widgetsSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,18 @@ describe('widget', function() {
633633
}));
634634

635635

636+
it('should create controller instance on $afterRouteChange event', inject(
637+
function($route, $rootScope) {
638+
var controllerScope;
639+
$route.current = { controller: function($scope) { controllerScope = $scope; } };
640+
$rootScope.$broadcast('$afterRouteChange', $route.current);
641+
642+
expect(controllerScope.$parent.$id).toBe($rootScope.$id);
643+
expect(controllerScope.$id).toBe($route.current.scope.$id);
644+
}
645+
));
646+
647+
636648
it('should load content via xhr when route changes', function() {
637649
module(function($routeProvider) {
638650
$routeProvider.when('/foo', {template: 'myUrl1'});

0 commit comments

Comments
 (0)