Skip to content

Commit e2898c9

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

File tree

1 file changed

+73
-74
lines changed

1 file changed

+73
-74
lines changed

src/ng/compile.js

+73-74
Original file line numberDiff line numberDiff line change
@@ -2270,79 +2270,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
22702270
}
22712271
}
22722272

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

23762303
if (controllerDirectives) {
2377-
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope);
2304+
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective);
23782305
}
23792306

23802307
if (newIsolateScopeDirective) {
@@ -2502,6 +2429,78 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
25022429
}
25032430
}
25042431

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

0 commit comments

Comments
 (0)