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

Commit f5b22e9

Browse files
jbedardNarretz
authored andcommitted
refactor($compile): move setup/get controller methods out of the compile node closure
Closes #13427
1 parent cfa415b commit f5b22e9

File tree

1 file changed

+73
-74
lines changed

1 file changed

+73
-74
lines changed

src/ng/compile.js

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,79 +2283,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
22832283
}
22842284
}
22852285

2286-
2287-
function getControllers(directiveName, require, $element, elementControllers) {
2288-
var value;
2289-
2290-
if (isString(require)) {
2291-
var match = require.match(REQUIRE_PREFIX_REGEXP);
2292-
var name = require.substring(match[0].length);
2293-
var inheritType = match[1] || match[3];
2294-
var optional = match[2] === '?';
2295-
2296-
//If only parents then start at the parent element
2297-
if (inheritType === '^^') {
2298-
$element = $element.parent();
2299-
//Otherwise attempt getting the controller from elementControllers in case
2300-
//the element is transcluded (and has no data) and to avoid .data if possible
2301-
} else {
2302-
value = elementControllers && elementControllers[name];
2303-
value = value && value.instance;
2304-
}
2305-
2306-
if (!value) {
2307-
var dataName = '$' + name + 'Controller';
2308-
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2309-
}
2310-
2311-
if (!value && !optional) {
2312-
throw $compileMinErr('ctreq',
2313-
"Controller '{0}', required by directive '{1}', can't be found!",
2314-
name, directiveName);
2315-
}
2316-
} else if (isArray(require)) {
2317-
value = [];
2318-
for (var i = 0, ii = require.length; i < ii; i++) {
2319-
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
2320-
}
2321-
} else if (isObject(require)) {
2322-
value = {};
2323-
forEach(require, function(controller, property) {
2324-
value[property] = getControllers(directiveName, controller, $element, elementControllers);
2325-
});
2326-
}
2327-
2328-
return value || null;
2329-
}
2330-
2331-
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope) {
2332-
var elementControllers = createMap();
2333-
for (var controllerKey in controllerDirectives) {
2334-
var directive = controllerDirectives[controllerKey];
2335-
var locals = {
2336-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
2337-
$element: $element,
2338-
$attrs: attrs,
2339-
$transclude: transcludeFn
2340-
};
2341-
2342-
var controller = directive.controller;
2343-
if (controller == '@') {
2344-
controller = attrs[directive.name];
2345-
}
2346-
2347-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2348-
2349-
// For directives with element transclusion the element is a comment.
2350-
// In this case .data will not attach any data.
2351-
// Instead, we save the controllers for the element in a local hash and attach to .data
2352-
// later, once we have the actual element.
2353-
elementControllers[directive.name] = controllerInstance;
2354-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2355-
}
2356-
return elementControllers;
2357-
}
2358-
23592286
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
23602287
var i, ii, linkFn, isolateScope, controllerScope, elementControllers, transcludeFn, $element,
23612288
attrs, removeScopeBindingWatches, removeControllerBindingWatches;
@@ -2387,7 +2314,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23872314
}
23882315

23892316
if (controllerDirectives) {
2390-
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope);
2317+
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective);
23912318
}
23922319

23932320
if (newIsolateScopeDirective) {
@@ -2515,6 +2442,78 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
25152442
}
25162443
}
25172444

2445+
function getControllers(directiveName, require, $element, elementControllers) {
2446+
var value;
2447+
2448+
if (isString(require)) {
2449+
var match = require.match(REQUIRE_PREFIX_REGEXP);
2450+
var name = require.substring(match[0].length);
2451+
var inheritType = match[1] || match[3];
2452+
var optional = match[2] === '?';
2453+
2454+
//If only parents then start at the parent element
2455+
if (inheritType === '^^') {
2456+
$element = $element.parent();
2457+
//Otherwise attempt getting the controller from elementControllers in case
2458+
//the element is transcluded (and has no data) and to avoid .data if possible
2459+
} else {
2460+
value = elementControllers && elementControllers[name];
2461+
value = value && value.instance;
2462+
}
2463+
2464+
if (!value) {
2465+
var dataName = '$' + name + 'Controller';
2466+
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2467+
}
2468+
2469+
if (!value && !optional) {
2470+
throw $compileMinErr('ctreq',
2471+
"Controller '{0}', required by directive '{1}', can't be found!",
2472+
name, directiveName);
2473+
}
2474+
} else if (isArray(require)) {
2475+
value = [];
2476+
for (var i = 0, ii = require.length; i < ii; i++) {
2477+
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
2478+
}
2479+
} else if (isObject(require)) {
2480+
value = {};
2481+
forEach(require, function(controller, property) {
2482+
value[property] = getControllers(directiveName, controller, $element, elementControllers);
2483+
});
2484+
}
2485+
2486+
return value || null;
2487+
}
2488+
2489+
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective) {
2490+
var elementControllers = createMap();
2491+
for (var controllerKey in controllerDirectives) {
2492+
var directive = controllerDirectives[controllerKey];
2493+
var locals = {
2494+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
2495+
$element: $element,
2496+
$attrs: attrs,
2497+
$transclude: transcludeFn
2498+
};
2499+
2500+
var controller = directive.controller;
2501+
if (controller == '@') {
2502+
controller = attrs[directive.name];
2503+
}
2504+
2505+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2506+
2507+
// For directives with element transclusion the element is a comment.
2508+
// In this case .data will not attach any data.
2509+
// Instead, we save the controllers for the element in a local hash and attach to .data
2510+
// later, once we have the actual element.
2511+
elementControllers[directive.name] = controllerInstance;
2512+
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2513+
}
2514+
return elementControllers;
2515+
}
2516+
25182517
// Depending upon the context in which a directive finds itself it might need to have a new isolated
25192518
// or child scope created. For instance:
25202519
// * if the directive has been pulled into a template because another directive with a higher priority

0 commit comments

Comments
 (0)