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

Commit 3e56d07

Browse files
committed
refactor($compile): moving controller setup into its own method
1 parent bfd7dc6 commit 3e56d07

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/ng/compile.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,10 +1830,41 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18301830
}
18311831
}
18321832

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

18341866
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
1835-
var i, ii, linkFn, directive, controller, isolateScope, elementControllers, transcludeFn, $element,
1836-
attrs;
1867+
var i, ii, linkFn, isolateScope, elementControllers, transcludeFn, $element, attrs;
18371868

18381869
if (compileNode === linkNode) {
18391870
attrs = templateAttrs;
@@ -1855,37 +1886,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18551886
}
18561887

18571888
if (controllerDirectives) {
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-
1870-
var locals = {
1871-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1872-
$element: $element,
1873-
$attrs: attrs,
1874-
$transclude: transcludeFn
1875-
};
1876-
1877-
var controller = directive.controller;
1878-
if (controller === '@') {
1879-
controller = attrs[directive.name];
1880-
}
1881-
1882-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1883-
1884-
elementControllers[directive.name] = controllerInstance;
1885-
if (controllerData) {
1886-
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
1887-
}
1888-
}
1889+
setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers = createMap());
18891890
}
18901891

18911892
if (newIsolateScopeDirective) {
@@ -1973,8 +1974,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19731974
}
19741975

19751976
// Initialize the controllers before linking
1976-
for (i in elementControllers) {
1977-
elementControllers[i]();
1977+
if (elementControllers) {
1978+
invokeEach(elementControllers);
19781979
}
19791980

19801981
// PRELINKING
@@ -2033,6 +2034,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20332034
}
20342035
}
20352036

2037+
function invokeEach(funcs) {
2038+
for (var key in funcs) {
2039+
funcs[key]();
2040+
}
2041+
}
2042+
20362043
function markDirectivesAsIsolate(directives) {
20372044
// mark all directives as needing isolate scope.
20382045
for (var j = 0, jj = directives.length; j < jj; j++) {

0 commit comments

Comments
 (0)