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

Commit 6ed81a3

Browse files
committed
refactor($compile): move setup/get controller methods out of the compile node closure
1 parent d069020 commit 6ed81a3

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

src/ng/compile.js

+69-69
Original file line numberDiff line numberDiff line change
@@ -2070,74 +2070,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20702070
}
20712071
}
20722072

2073-
2074-
function getControllers(directiveName, require, $element, elementControllers) {
2075-
var value;
2076-
2077-
if (isString(require)) {
2078-
var match = require.match(REQUIRE_PREFIX_REGEXP);
2079-
var name = require.substring(match[0].length);
2080-
var inheritType = match[1] || match[3];
2081-
var optional = match[2] === '?';
2082-
2083-
//If only parents then start at the parent element
2084-
if (inheritType === '^^') {
2085-
$element = $element.parent();
2086-
//Otherwise attempt getting the controller from elementControllers in case
2087-
//the element is transcluded (and has no data) and to avoid .data if possible
2088-
} else {
2089-
value = elementControllers && elementControllers[name];
2090-
value = value && value.instance;
2091-
}
2092-
2093-
if (!value) {
2094-
var dataName = '$' + name + 'Controller';
2095-
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2096-
}
2097-
2098-
if (!value && !optional) {
2099-
throw $compileMinErr('ctreq',
2100-
"Controller '{0}', required by directive '{1}', can't be found!",
2101-
name, directiveName);
2102-
}
2103-
} else if (isArray(require)) {
2104-
value = [];
2105-
for (var i = 0, ii = require.length; i < ii; i++) {
2106-
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
2107-
}
2108-
}
2109-
2110-
return value || null;
2111-
}
2112-
2113-
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope) {
2114-
var elementControllers = createMap();
2115-
for (var controllerKey in controllerDirectives) {
2116-
var directive = controllerDirectives[controllerKey];
2117-
var locals = {
2118-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
2119-
$element: $element,
2120-
$attrs: attrs,
2121-
$transclude: transcludeFn
2122-
};
2123-
2124-
var controller = directive.controller;
2125-
if (controller == '@') {
2126-
controller = attrs[directive.name];
2127-
}
2128-
2129-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2130-
2131-
// For directives with element transclusion the element is a comment.
2132-
// In this case jQuery .data will not attach any data.
2133-
// Instead, we save the controllers for the element in a local hash and attach to .data
2134-
// later, once we have the actual element.
2135-
elementControllers[directive.name] = controllerInstance;
2136-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2137-
}
2138-
return elementControllers;
2139-
}
2140-
21412073
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
21422074
var i, ii, linkFn, isolateScope, controllerScope, elementControllers, transcludeFn, $element,
21432075
attrs, removeScopeBindingWatches, removeControllerBindingWatches;
@@ -2169,7 +2101,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
21692101
}
21702102

21712103
if (controllerDirectives) {
2172-
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope);
2104+
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective);
21732105
}
21742106

21752107
if (newIsolateScopeDirective) {
@@ -2282,6 +2214,74 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
22822214
}
22832215
}
22842216

2217+
2218+
function getControllers(directiveName, require, $element, elementControllers) {
2219+
var value;
2220+
2221+
if (isString(require)) {
2222+
var match = require.match(REQUIRE_PREFIX_REGEXP);
2223+
var name = require.substring(match[0].length);
2224+
var inheritType = match[1] || match[3];
2225+
var optional = match[2] === '?';
2226+
2227+
//If only parents then start at the parent element
2228+
if (inheritType === '^^') {
2229+
$element = $element.parent();
2230+
//Otherwise attempt getting the controller from elementControllers in case
2231+
//the element is transcluded (and has no data) and to avoid .data if possible
2232+
} else {
2233+
value = elementControllers && elementControllers[name];
2234+
value = value && value.instance;
2235+
}
2236+
2237+
if (!value) {
2238+
var dataName = '$' + name + 'Controller';
2239+
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
2240+
}
2241+
2242+
if (!value && !optional) {
2243+
throw $compileMinErr('ctreq',
2244+
"Controller '{0}', required by directive '{1}', can't be found!",
2245+
name, directiveName);
2246+
}
2247+
} else if (isArray(require)) {
2248+
value = [];
2249+
for (var i = 0, ii = require.length; i < ii; i++) {
2250+
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
2251+
}
2252+
}
2253+
2254+
return value || null;
2255+
}
2256+
2257+
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope, newIsolateScopeDirective) {
2258+
var elementControllers = createMap();
2259+
for (var controllerKey in controllerDirectives) {
2260+
var directive = controllerDirectives[controllerKey];
2261+
var locals = {
2262+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
2263+
$element: $element,
2264+
$attrs: attrs,
2265+
$transclude: transcludeFn
2266+
};
2267+
2268+
var controller = directive.controller;
2269+
if (controller == '@') {
2270+
controller = attrs[directive.name];
2271+
}
2272+
2273+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
2274+
2275+
// For directives with element transclusion the element is a comment.
2276+
// In this case jQuery .data will not attach any data.
2277+
// Instead, we save the controllers for the element in a local hash and attach to .data
2278+
// later, once we have the actual element.
2279+
elementControllers[directive.name] = controllerInstance;
2280+
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
2281+
}
2282+
return elementControllers;
2283+
}
2284+
22852285
// Depending upon the context in which a directive finds itself it might need to have a new isolated
22862286
// or child scope created. For instance:
22872287
// * if the directive has been pulled into a template because another directive with a higher priority

0 commit comments

Comments
 (0)