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

Commit 78c75fc

Browse files
committed
refactor($compile): move setup/get controller methods out of the compile node closure
1 parent f5efd1e commit 78c75fc

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
@@ -2360,79 +2360,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
23602360
}
23612361
}
23622362

2363-
2364-
function getControllers(directiveName, require, $element, elementControllers) {
2365-
var value;
2366-
2367-
if (isString(require)) {
2368-
var match = require.match(REQUIRE_PREFIX_REGEXP);
2369-
var name = require.substring(match[0].length);
2370-
var inheritType = match[1] || match[3];
2371-
var optional = match[2] === '?';
2372-
2373-
//If only parents then start at the parent element
2374-
if (inheritType === '^^') {
2375-
$element = $element.parent();
2376-
//Otherwise attempt getting the controller from elementControllers in case
2377-
//the element is transcluded (and has no data) and to avoid .data if possible
2378-
} else {
2379-
value = elementControllers && elementControllers[name];
2380-
value = value && value.instance;
2381-
}
2382-
2383-
if (!value) {
2384-
var dataName = '$' + name + 'Controller';
2385-
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2386-
}
2387-
2388-
if (!value && !optional) {
2389-
throw $compileMinErr('ctreq',
2390-
"Controller '{0}', required by directive '{1}', can't be found!",
2391-
name, directiveName);
2392-
}
2393-
} else if (isArray(require)) {
2394-
value = [];
2395-
for (var i = 0, ii = require.length; i < ii; i++) {
2396-
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
2397-
}
2398-
} else if (isObject(require)) {
2399-
value = {};
2400-
forEach(require, function(controller, property) {
2401-
value[property] = getControllers(directiveName, controller, $element, elementControllers);
2402-
});
2403-
}
2404-
2405-
return value || null;
2406-
}
2407-
2408-
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope) {
2409-
var elementControllers = createMap();
2410-
for (var controllerKey in controllerDirectives) {
2411-
var directive = controllerDirectives[controllerKey];
2412-
var locals = {
2413-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
2414-
$element: $element,
2415-
$attrs: attrs,
2416-
$transclude: transcludeFn
2417-
};
2418-
2419-
var controller = directive.controller;
2420-
if (controller == '@') {
2421-
controller = attrs[directive.name];
2422-
}
2423-
2424-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2425-
2426-
// For directives with element transclusion the element is a comment.
2427-
// In this case .data will not attach any data.
2428-
// Instead, we save the controllers for the element in a local hash and attach to .data
2429-
// later, once we have the actual element.
2430-
elementControllers[directive.name] = controllerInstance;
2431-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2432-
}
2433-
return elementControllers;
2434-
}
2435-
24362363
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
24372364
var i, ii, linkFn, isolateScope, controllerScope, elementControllers, transcludeFn, $element,
24382365
attrs, removeScopeBindingWatches, removeControllerBindingWatches;
@@ -2464,7 +2391,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
24642391
}
24652392

24662393
if (controllerDirectives) {
2467-
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope);
2394+
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective);
24682395
}
24692396

24702397
if (newIsolateScopeDirective) {
@@ -2592,6 +2519,78 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
25922519
}
25932520
}
25942521

2522+
function getControllers(directiveName, require, $element, elementControllers) {
2523+
var value;
2524+
2525+
if (isString(require)) {
2526+
var match = require.match(REQUIRE_PREFIX_REGEXP);
2527+
var name = require.substring(match[0].length);
2528+
var inheritType = match[1] || match[3];
2529+
var optional = match[2] === '?';
2530+
2531+
//If only parents then start at the parent element
2532+
if (inheritType === '^^') {
2533+
$element = $element.parent();
2534+
//Otherwise attempt getting the controller from elementControllers in case
2535+
//the element is transcluded (and has no data) and to avoid .data if possible
2536+
} else {
2537+
value = elementControllers && elementControllers[name];
2538+
value = value && value.instance;
2539+
}
2540+
2541+
if (!value) {
2542+
var dataName = '$' + name + 'Controller';
2543+
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2544+
}
2545+
2546+
if (!value && !optional) {
2547+
throw $compileMinErr('ctreq',
2548+
"Controller '{0}', required by directive '{1}', can't be found!",
2549+
name, directiveName);
2550+
}
2551+
} else if (isArray(require)) {
2552+
value = [];
2553+
for (var i = 0, ii = require.length; i < ii; i++) {
2554+
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
2555+
}
2556+
} else if (isObject(require)) {
2557+
value = {};
2558+
forEach(require, function(controller, property) {
2559+
value[property] = getControllers(directiveName, controller, $element, elementControllers);
2560+
});
2561+
}
2562+
2563+
return value || null;
2564+
}
2565+
2566+
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective) {
2567+
var elementControllers = createMap();
2568+
for (var controllerKey in controllerDirectives) {
2569+
var directive = controllerDirectives[controllerKey];
2570+
var locals = {
2571+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
2572+
$element: $element,
2573+
$attrs: attrs,
2574+
$transclude: transcludeFn
2575+
};
2576+
2577+
var controller = directive.controller;
2578+
if (controller == '@') {
2579+
controller = attrs[directive.name];
2580+
}
2581+
2582+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2583+
2584+
// For directives with element transclusion the element is a comment.
2585+
// In this case .data will not attach any data.
2586+
// Instead, we save the controllers for the element in a local hash and attach to .data
2587+
// later, once we have the actual element.
2588+
elementControllers[directive.name] = controllerInstance;
2589+
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2590+
}
2591+
return elementControllers;
2592+
}
2593+
25952594
// Depending upon the context in which a directive finds itself it might need to have a new isolated
25962595
// or child scope created. For instance:
25972596
// * if the directive has been pulled into a template because another directive with a higher priority

0 commit comments

Comments
 (0)