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

Commit 587cdff

Browse files
committed
refactor($compile): move setup/get controller methods out of the compile node closure
1 parent 40ffe04 commit 587cdff

File tree

1 file changed

+68
-69
lines changed

1 file changed

+68
-69
lines changed

src/ng/compile.js

Lines changed: 68 additions & 69 deletions
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,73 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
22822214
}
22832215
}
22842216

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

0 commit comments

Comments
 (0)