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

Commit bfd7dc6

Browse files
committed
perf($compile): simplifying/refactoring controller loops/methods
1 parent 68307d1 commit bfd7dc6

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

src/ng/compile.js

+53-54
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16231623

16241624
if (!directive.templateUrl && directive.controller) {
16251625
directiveValue = directive.controller;
1626-
controllerDirectives = controllerDirectives || {};
1626+
controllerDirectives = controllerDirectives || createMap();
16271627
assertNoDuplicate("'" + directiveName + "' controller",
16281628
controllerDirectives[directiveName], directive, $compileNode);
16291629
controllerDirectives[directiveName] = directive;
@@ -1791,54 +1791,48 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17911791

17921792

17931793
function getControllers(directiveName, require, $element, elementControllers) {
1794-
var value, retrievalMethod = 'data', optional = false;
1795-
var $searchElement = $element;
1796-
var match;
1797-
if (isString(require)) {
1798-
match = require.match(REQUIRE_PREFIX_REGEXP);
1799-
require = require.substring(match[0].length);
1800-
1801-
if (match[3]) {
1802-
if (match[1]) match[3] = null;
1803-
else match[1] = match[3];
1804-
}
1805-
if (match[1] === '^') {
1806-
retrievalMethod = 'inheritedData';
1807-
} else if (match[1] === '^^') {
1808-
retrievalMethod = 'inheritedData';
1809-
$searchElement = $element.parent();
1810-
}
1811-
if (match[2] === '?') {
1812-
optional = true;
1813-
}
1794+
var i, value;
18141795

1815-
value = null;
1796+
if (typeof require === 'string') {
1797+
var match = require.match(REQUIRE_PREFIX_REGEXP);
1798+
var name = require.substring(match[0].length);
1799+
var type = match[1] || match[3];
18161800

1817-
if (elementControllers && retrievalMethod === 'data') {
1818-
if (value = elementControllers[require]) {
1819-
value = value.instance;
1820-
}
1801+
//If only parents then start at the parent element
1802+
//Otherwise attempt getting the controller from elementControllers to avoid .data
1803+
if (type === '^^') {
1804+
$element = $element.parent();
1805+
} else {
1806+
value = elementControllers && elementControllers[name];
1807+
value = value && value.instance;
18211808
}
1822-
value = value || $searchElement[retrievalMethod]('$' + require + 'Controller');
18231809

1824-
if (!value && !optional) {
1810+
if (!value) {
1811+
var dataName = '$' + name + 'Controller';
1812+
value = type ? $element.inheritedData(dataName) : $element.data(dataName);
1813+
}
1814+
1815+
if (!value && match[2] !== '?') {
18251816
throw $compileMinErr('ctreq',
18261817
"Controller '{0}', required by directive '{1}', can't be found!",
1827-
require, directiveName);
1818+
name, directiveName);
18281819
}
1820+
18291821
return value || null;
1830-
} else if (isArray(require)) {
1831-
value = [];
1832-
forEach(require, function(require) {
1833-
value.push(getControllers(directiveName, require, $element, elementControllers));
1834-
});
18351822
}
1836-
return value;
1823+
1824+
if (isArray(require)) {
1825+
value = new Array(i = require.length);
1826+
while (i--) {
1827+
value[i] = getControllers(directiveName, require[i], $element, elementControllers);
1828+
}
1829+
return value;
1830+
}
18371831
}
18381832

18391833

18401834
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
1841-
var i, ii, linkFn, controller, isolateScope, elementControllers, transcludeFn, $element,
1835+
var i, ii, linkFn, directive, controller, isolateScope, elementControllers, transcludeFn, $element,
18421836
attrs;
18431837

18441838
if (compileNode === linkNode) {
@@ -1861,32 +1855,37 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18611855
}
18621856

18631857
if (controllerDirectives) {
1864-
elementControllers = {};
1865-
forEach(controllerDirectives, function(directive) {
1858+
elementControllers = createMap();
1859+
1860+
// For directives with element transclusion the element is a comment,
1861+
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1862+
// clean up (http://bugs.jquery.com/ticket/8335).
1863+
// Instead, we save the controllers for the element in a local hash and attach to .data
1864+
// later, once we have the actual element.
1865+
var controllerData = !hasElementTranscludeDirective && $element.data();
1866+
1867+
for (var directiveName in controllerDirectives) {
1868+
var directive = controllerDirectives[directiveName];
1869+
18661870
var locals = {
18671871
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
18681872
$element: $element,
18691873
$attrs: attrs,
18701874
$transclude: transcludeFn
1871-
}, controllerInstance;
1875+
};
18721876

1873-
controller = directive.controller;
1874-
if (controller == '@') {
1877+
var controller = directive.controller;
1878+
if (controller === '@') {
18751879
controller = attrs[directive.name];
18761880
}
18771881

1878-
controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1882+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
18791883

1880-
// For directives with element transclusion the element is a comment,
1881-
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1882-
// clean up (http://bugs.jquery.com/ticket/8335).
1883-
// Instead, we save the controllers for the element in a local hash and attach to .data
1884-
// later, once we have the actual element.
18851884
elementControllers[directive.name] = controllerInstance;
1886-
if (!hasElementTranscludeDirective) {
1887-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
1885+
if (controllerData) {
1886+
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
18881887
}
1889-
});
1888+
}
18901889
}
18911890

18921891
if (newIsolateScopeDirective) {
@@ -1972,10 +1971,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19721971
}
19731972
});
19741973
}
1975-
if (elementControllers) {
1976-
forEach(elementControllers, function(controller) {
1977-
controller();
1978-
});
1974+
1975+
// Initialize the controllers before linking
1976+
for (i in elementControllers) {
1977+
elementControllers[i]();
19791978
}
19801979

19811980
// PRELINKING

0 commit comments

Comments
 (0)