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

Commit 540338f

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 0e6a700 commit 540338f

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);
@@ -1971,8 +1965,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19711965
return elementControllers;
19721966
}
19731967

1974-
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn,
1975-
thisLinkFn) {
1968+
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
19761969
var i, ii, linkFn, controller, isolateScope, elementControllers, transcludeFn, $element,
19771970
attrs;
19781971

@@ -2006,24 +1999,28 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20061999
compile.$$addScopeClass($element, true);
20072000
isolateScope.$$isolateBindings =
20082001
newIsolateScopeDirective.$$isolateBindings;
2009-
initializeDirectiveBindings(scope, attrs, isolateScope,
2010-
isolateScope.$$isolateBindings,
2011-
newIsolateScopeDirective, isolateScope);
2002+
var parentWatchDestroyer = initializeDirectiveBindings(scope, attrs, isolateScope,
2003+
isolateScope.$$isolateBindings,
2004+
newIsolateScopeDirective);
2005+
if (parentWatchDestroyer) {
2006+
isolateScope.$on('$destroy', parentWatchDestroyer);
2007+
}
20122008
}
20132009
if (elementControllers) {
20142010
// Initialize bindToController bindings for new/isolate scopes
20152011
var scopeDirective = newIsolateScopeDirective || newScopeDirective;
20162012
var bindings;
20172013
var controllerForBindings;
2014+
var destroyBindings;
20182015
if (scopeDirective && elementControllers[scopeDirective.name]) {
20192016
bindings = scopeDirective.$$bindings.bindToController;
20202017
controller = elementControllers[scopeDirective.name];
20212018

20222019
if (controller && controller.identifier && bindings) {
20232020
controllerForBindings = controller;
2024-
thisLinkFn.$$destroyBindings =
2021+
destroyBindings =
20252022
initializeDirectiveBindings(scope, attrs, controller.instance,
2026-
bindings, scopeDirective);
2023+
bindings, scopeDirective) || noop;
20272024
}
20282025
}
20292026
for (i in elementControllers) {
@@ -2037,8 +2034,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20372034
$element.data('$' + i + 'Controller', controllerResult);
20382035
if (controller === controllerForBindings) {
20392036
// Remove and re-install bindToController bindings
2040-
thisLinkFn.$$destroyBindings();
2041-
thisLinkFn.$$destroyBindings =
2037+
destroyBindings();
2038+
destroyBindings =
20422039
initializeDirectiveBindings(scope, attrs, controllerResult, bindings, scopeDirective);
20432040
}
20442041
}
@@ -2300,7 +2297,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23002297
childBoundTranscludeFn = boundTranscludeFn;
23012298
}
23022299
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement,
2303-
childBoundTranscludeFn, afterTemplateNodeLinkFn);
2300+
childBoundTranscludeFn);
23042301
}
23052302
linkQueue = null;
23062303
});
@@ -2317,8 +2314,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23172314
if (afterTemplateNodeLinkFn.transcludeOnThisElement) {
23182315
childBoundTranscludeFn = createBoundTranscludeFn(scope, afterTemplateNodeLinkFn.transclude, boundTranscludeFn);
23192316
}
2320-
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn,
2321-
afterTemplateNodeLinkFn);
2317+
afterTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, childBoundTranscludeFn);
23222318
}
23232319
};
23242320
}
@@ -2578,8 +2574,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
25782574

25792575
// Set up $watches for isolate scope and controller bindings. This process
25802576
// only occurs for isolate scopes and new scopes with controllerAs.
2581-
function initializeDirectiveBindings(scope, attrs, destination, bindings,
2582-
directive, newScope) {
2577+
function initializeDirectiveBindings(scope, attrs, destination, bindings, directive) {
25832578
var onNewScopeDestroyed;
25842579
forEach(bindings, function(definition, scopeName) {
25852580
var attrName = definition.attrName,
@@ -2665,16 +2660,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
26652660
break;
26662661
}
26672662
});
2668-
var destroyBindings = onNewScopeDestroyed ? function destroyBindings() {
2663+
2664+
return onNewScopeDestroyed && function destroyBindings() {
26692665
for (var i = 0, ii = onNewScopeDestroyed.length; i < ii; ++i) {
26702666
onNewScopeDestroyed[i]();
26712667
}
2672-
} : noop;
2673-
if (newScope && destroyBindings !== noop) {
2674-
newScope.$on('$destroy', destroyBindings);
2675-
return noop;
2676-
}
2677-
return destroyBindings;
2668+
};
26782669
}
26792670
}];
26802671
}

0 commit comments

Comments
 (0)