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

Commit 89a4f89

Browse files
committed
refactor($compile): moving controller setup into its own method
1 parent fe97f87 commit 89a4f89

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
@@ -1808,10 +1808,41 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18081808
}
18091809
}
18101810

1811+
function setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers) {
1812+
// For directives with element transclusion the element is a comment,
1813+
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1814+
// clean up (http://bugs.jquery.com/ticket/8335).
1815+
// Instead, we save the controllers for the element in a local hash and attach to .data
1816+
// later, once we have the actual element.
1817+
var controllerData = !hasElementTranscludeDirective && $element.data();
1818+
1819+
for (var directiveName in controllerDirectives) {
1820+
var directive = controllerDirectives[directiveName];
1821+
1822+
var locals = {
1823+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1824+
$element: $element,
1825+
$attrs: attrs,
1826+
$transclude: transcludeFn
1827+
};
1828+
1829+
var controller = directive.controller;
1830+
if (controller === '@') {
1831+
controller = attrs[directive.name];
1832+
}
1833+
1834+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1835+
1836+
elementControllers[directive.name] = controllerInstance;
1837+
if (controllerData) {
1838+
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
1839+
}
1840+
}
1841+
}
1842+
18111843

18121844
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
1813-
var i, ii, linkFn, directive, controller, isolateScope, elementControllers, transcludeFn, $element,
1814-
attrs;
1845+
var i, ii, linkFn, isolateScope, elementControllers, transcludeFn, $element, attrs;
18151846

18161847
if (compileNode === linkNode) {
18171848
attrs = templateAttrs;
@@ -1833,37 +1864,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18331864
}
18341865

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

18691870
if (newIsolateScopeDirective) {
@@ -1951,8 +1952,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19511952
}
19521953

19531954
// Initialize the controllers before linking
1954-
for (i in elementControllers) {
1955-
elementControllers[i]();
1955+
if (elementControllers) {
1956+
invokeEach(elementControllers);
19561957
}
19571958

19581959
// PRELINKING
@@ -2011,6 +2012,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
20112012
}
20122013
}
20132014

2015+
function invokeEach(funcs) {
2016+
for (var key in funcs) {
2017+
funcs[key]();
2018+
}
2019+
}
2020+
20142021
function markDirectivesAsIsolate(directives) {
20152022
// mark all directives as needing isolate scope.
20162023
for (var j = 0, jj = directives.length; j < jj; j++) {

0 commit comments

Comments
 (0)