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

Commit ff2be45

Browse files
committed
refactor($compile): moving controller setup into its own method
1 parent 659c424 commit ff2be45

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/ng/compile.js

+42-35
Original file line numberDiff line numberDiff line change
@@ -1826,10 +1826,41 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18261826
}
18271827
}
18281828

1829+
function setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers) {
1830+
// For directives with element transclusion the element is a comment,
1831+
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1832+
// clean up (http://bugs.jquery.com/ticket/8335).
1833+
// Instead, we save the controllers for the element in a local hash and attach to .data
1834+
// later, once we have the actual element.
1835+
var controllerData = !hasElementTranscludeDirective && $element.data();
1836+
1837+
for (var directiveName in controllerDirectives) {
1838+
var directive = controllerDirectives[directiveName];
1839+
1840+
var locals = {
1841+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1842+
$element: $element,
1843+
$attrs: attrs,
1844+
$transclude: transcludeFn
1845+
};
1846+
1847+
var controller = directive.controller;
1848+
if (controller === '@') {
1849+
controller = attrs[directive.name];
1850+
}
1851+
1852+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1853+
1854+
elementControllers[directive.name] = controllerInstance;
1855+
if (controllerData) {
1856+
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
1857+
}
1858+
}
1859+
}
1860+
18291861

18301862
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
1831-
var i, ii, linkFn, directive, controller, isolateScope, elementControllers, transcludeFn, $element,
1832-
attrs;
1863+
var i, ii, linkFn, isolateScope, elementControllers, transcludeFn, $element, attrs;
18331864

18341865
if (compileNode === linkNode) {
18351866
attrs = templateAttrs;
@@ -1851,37 +1882,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18511882
}
18521883

18531884
if (controllerDirectives) {
1854-
elementControllers = {};
1855-
1856-
// For directives with element transclusion the element is a comment,
1857-
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1858-
// clean up (http://bugs.jquery.com/ticket/8335).
1859-
// Instead, we save the controllers for the element in a local hash and attach to .data
1860-
// later, once we have the actual element.
1861-
var controllerData = !hasElementTranscludeDirective && $element.data();
1862-
1863-
for (var directiveName in controllerDirectives) {
1864-
var directive = controllerDirectives[directiveName];
1865-
1866-
var locals = {
1867-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1868-
$element: $element,
1869-
$attrs: attrs,
1870-
$transclude: transcludeFn
1871-
};
1872-
1873-
var controller = directive.controller;
1874-
if (controller === '@') {
1875-
controller = attrs[directive.name];
1876-
}
1877-
1878-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1879-
1880-
elementControllers[directive.name] = controllerInstance;
1881-
if (controllerData) {
1882-
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
1883-
}
1884-
}
1885+
setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers = {});
18851886
}
18861887

18871888
if (newIsolateScopeDirective) {
@@ -1969,8 +1970,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19691970
}
19701971

19711972
// Initialize the controllers before linking
1972-
for (i in elementControllers) {
1973-
elementControllers[i]();
1973+
if (elementControllers) {
1974+
invokeEach(elementControllers);
19741975
}
19751976

19761977
// PRELINKING
@@ -2029,6 +2030,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20292030
}
20302031
}
20312032

2033+
function invokeEach(funcs) {
2034+
for (var key in funcs) {
2035+
funcs[key]();
2036+
}
2037+
}
2038+
20322039
function markDirectivesAsIsolate(directives) {
20332040
// mark all directives as needing isolate scope.
20342041
for (var j = 0, jj = directives.length; j < jj; j++) {

0 commit comments

Comments
 (0)