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

Commit 66fee7e

Browse files
jbedardpetebacondarwin
authored andcommitted
refactor($compile): move $scope.$on('$destroy') handler out of initializeDirectiveBindings
Since only one of three invocations of `initializeDirectiveBindings` actually adds a `$destroy` handler to the scope (the others just manually call unwatch as needed), we can move that code out of this method. This also has the benefit of simplifying what parameters need to be passed through to the linking functions Closes #12528
1 parent beea571 commit 66fee7e

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

src/ng/compile.js

+19-28
Original file line numberDiff line numberDiff line change
@@ -1437,11 +1437,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14371437
if (nodeLinkFn.scope) {
14381438
childScope = scope.$new();
14391439
compile.$$addScopeInfo(jqLite(node), childScope);
1440-
var destroyBindings = nodeLinkFn.$$destroyBindings;
1441-
if (destroyBindings) {
1442-
nodeLinkFn.$$destroyBindings = null;
1443-
childScope.$on('$destroyed', destroyBindings);
1444-
}
14451440
} else {
14461441
childScope = scope;
14471442
}
@@ -1460,8 +1455,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14601455
childBoundTranscludeFn = null;
14611456
}
14621457

1463-
nodeLinkFn(childLinkFn, childScope, node, $rootElement, childBoundTranscludeFn,
1464-
nodeLinkFn);
1458+
nodeLinkFn(childLinkFn, childScope, node, $rootElement, childBoundTranscludeFn);
14651459

14661460
} else if (childLinkFn) {
14671461
childLinkFn(scope, node.childNodes, undefined, parentBoundTranscludeFn);
@@ -2025,8 +2019,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20252019
return elementControllers;
20262020
}
20272021

2028-
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn,
2029-
thisLinkFn) {
2022+
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
20302023
var i, ii, linkFn, controller, isolateScope, elementControllers, transcludeFn, $element,
20312024
attrs;
20322025

@@ -2060,24 +2053,28 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20602053
compile.$$addScopeClass($element, true);
20612054
isolateScope.$$isolateBindings =
20622055
newIsolateScopeDirective.$$isolateBindings;
2063-
initializeDirectiveBindings(scope, attrs, isolateScope,
2064-
isolateScope.$$isolateBindings,
2065-
newIsolateScopeDirective, isolateScope);
2056+
var parentWatchDestroyer = initializeDirectiveBindings(scope, attrs, isolateScope,
2057+
isolateScope.$$isolateBindings,
2058+
newIsolateScopeDirective);
2059+
if (parentWatchDestroyer) {
2060+
isolateScope.$on('$destroy', parentWatchDestroyer);
2061+
}
20662062
}
20672063
if (elementControllers) {
20682064
// Initialize bindToController bindings for new/isolate scopes
20692065
var scopeDirective = newIsolateScopeDirective || newScopeDirective;
20702066
var bindings;
20712067
var controllerForBindings;
2068+
var destroyBindings;
20722069
if (scopeDirective && elementControllers[scopeDirective.name]) {
20732070
bindings = scopeDirective.$$bindings.bindToController;
20742071
controller = elementControllers[scopeDirective.name];
20752072

20762073
if (controller && controller.identifier && bindings) {
20772074
controllerForBindings = controller;
2078-
thisLinkFn.$$destroyBindings =
2075+
destroyBindings =
20792076
initializeDirectiveBindings(scope, attrs, controller.instance,
2080-
bindings, scopeDirective);
2077+
bindings, scopeDirective) || noop;
20812078
}
20822079
}
20832080
for (i in elementControllers) {
@@ -2091,8 +2088,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20912088
$element.data('$' + i + 'Controller', controllerResult);
20922089
if (controller === controllerForBindings) {
20932090
// Remove and re-install bindToController bindings
2094-
thisLinkFn.$$destroyBindings();
2095-
thisLinkFn.$$destroyBindings =
2091+
destroyBindings();
2092+
destroyBindings =
20962093
initializeDirectiveBindings(scope, attrs, controllerResult, bindings, scopeDirective);
20972094
}
20982095
}
@@ -2354,7 +2351,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23542351
childBoundTranscludeFn = boundTranscludeFn;
23552352
}
23562353
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement,
2357-
childBoundTranscludeFn, afterTemplateNodeLinkFn);
2354+
childBoundTranscludeFn);
23582355
}
23592356
linkQueue = null;
23602357
});
@@ -2371,8 +2368,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23712368
if (afterTemplateNodeLinkFn.transcludeOnThisElement) {
23722369
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn);
23732370
}
2374-
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn,
2375-
afterTemplateNodeLinkFn);
2371+
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn);
23762372
}
23772373
};
23782374
}
@@ -2632,8 +2628,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
26322628

26332629
// Set up $watches for isolate scope and controller bindings. This process
26342630
// only occurs for isolate scopes and new scopes with controllerAs.
2635-
function initializeDirectiveBindings(scope, attrs, destination, bindings,
2636-
directive, newScope) {
2631+
function initializeDirectiveBindings(scope, attrs, destination, bindings, directive) {
26372632
var onNewScopeDestroyed;
26382633
forEach(bindings, function(definition, scopeName) {
26392634
var attrName = definition.attrName,
@@ -2719,16 +2714,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
27192714
break;
27202715
}
27212716
});
2722-
var destroyBindings = onNewScopeDestroyed ? function destroyBindings() {
2717+
2718+
return onNewScopeDestroyed && function destroyBindings() {
27232719
for (var i = 0, ii = onNewScopeDestroyed.length; i < ii; ++i) {
27242720
onNewScopeDestroyed[i]();
27252721
}
2726-
} : noop;
2727-
if (newScope && destroyBindings !== noop) {
2728-
newScope.$on('$destroy', destroyBindings);
2729-
return noop;
2730-
}
2731-
return destroyBindings;
2722+
};
27322723
}
27332724
}];
27342725
}

0 commit comments

Comments
 (0)