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

Commit fa0aa83

Browse files
jbedardlgalfaso
authored andcommitted
perf($compile): avoid .data when fetching required controllers
Closes: ##11059
1 parent bd6c04a commit fa0aa83

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

src/ng/compile.js

+24-31
Original file line numberDiff line numberDiff line change
@@ -1840,49 +1840,42 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18401840

18411841

18421842
function getControllers(directiveName, require, $element, elementControllers) {
1843-
var value, retrievalMethod = 'data', optional = false;
1844-
var $searchElement = $element;
1845-
var match;
1846-
if (isString(require)) {
1847-
match = require.match(REQUIRE_PREFIX_REGEXP);
1848-
require = require.substring(match[0].length);
1843+
var value;
18491844

1850-
if (match[3]) {
1851-
if (match[1]) match[3] = null;
1852-
else match[1] = match[3];
1853-
}
1854-
if (match[1] === '^') {
1855-
retrievalMethod = 'inheritedData';
1856-
} else if (match[1] === '^^') {
1857-
retrievalMethod = 'inheritedData';
1858-
$searchElement = $element.parent();
1859-
}
1860-
if (match[2] === '?') {
1861-
optional = true;
1845+
if (isString(require)) {
1846+
var match = require.match(REQUIRE_PREFIX_REGEXP);
1847+
var name = require.substring(match[0].length);
1848+
var inheritType = match[1] || match[3];
1849+
var optional = match[2] === '?';
1850+
1851+
//If only parents then start at the parent element
1852+
if (inheritType === '^^') {
1853+
$element = $element.parent();
1854+
//Otherwise attempt getting the controller from elementControllers in case
1855+
//the element is transcluded (and has no data) and to avoid .data if possible
1856+
} else {
1857+
value = elementControllers && elementControllers[name];
1858+
value = value && value.instance;
18621859
}
18631860

1864-
value = null;
1865-
1866-
if (elementControllers && retrievalMethod === 'data') {
1867-
if (value = elementControllers[require]) {
1868-
value = value.instance;
1869-
}
1861+
if (!value) {
1862+
var dataName = '$' + name + 'Controller';
1863+
value = inheritType ? $element.inheritedData(dataName) : $element.data(dataName);
18701864
}
1871-
value = value || $searchElement[retrievalMethod]('$' + require + 'Controller');
18721865

18731866
if (!value && !optional) {
18741867
throw $compileMinErr('ctreq',
18751868
"Controller '{0}', required by directive '{1}', can't be found!",
1876-
require, directiveName);
1869+
name, directiveName);
18771870
}
1878-
return value || null;
18791871
} else if (isArray(require)) {
18801872
value = [];
1881-
forEach(require, function(require) {
1882-
value.push(getControllers(directiveName, require, $element, elementControllers));
1883-
});
1873+
for (var i = 0, ii = require.length; i < ii; i++) {
1874+
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
1875+
}
18841876
}
1885-
return value;
1877+
1878+
return value || null;
18861879
}
18871880

18881881

0 commit comments

Comments
 (0)