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

Commit 9085099

Browse files
committed
refactor($compile): moving controller setup into its own method
1 parent a471d6f commit 9085099

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
@@ -1770,10 +1770,41 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17701770
}
17711771
}
17721772

1773+
function setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers) {
1774+
// For directives with element transclusion the element is a comment,
1775+
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1776+
// clean up (http://bugs.jquery.com/ticket/8335).
1777+
// Instead, we save the controllers for the element in a local hash and attach to .data
1778+
// later, once we have the actual element.
1779+
var controllerData = !hasElementTranscludeDirective && $element.data();
1780+
1781+
for (var directiveName in controllerDirectives) {
1782+
var directive = controllerDirectives[directiveName];
1783+
1784+
var locals = {
1785+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1786+
$element: $element,
1787+
$attrs: attrs,
1788+
$transclude: transcludeFn
1789+
};
1790+
1791+
var controller = directive.controller;
1792+
if (controller === '@') {
1793+
controller = attrs[directive.name];
1794+
}
1795+
1796+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1797+
1798+
elementControllers[directive.name] = controllerInstance;
1799+
if (controllerData) {
1800+
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
1801+
}
1802+
}
1803+
}
1804+
17731805

17741806
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn) {
1775-
var i, ii, linkFn, directive, controller, isolateScope, elementControllers, transcludeFn, $element,
1776-
attrs;
1807+
var i, ii, linkFn, isolateScope, elementControllers, transcludeFn, $element, attrs;
17771808

17781809
if (compileNode === linkNode) {
17791810
attrs = templateAttrs;
@@ -1789,37 +1820,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
17891820

17901821
transcludeFn = boundTranscludeFn && controllersBoundTransclude;
17911822
if (controllerDirectives) {
1792-
elementControllers = {};
1793-
1794-
// For directives with element transclusion the element is a comment,
1795-
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1796-
// clean up (http://bugs.jquery.com/ticket/8335).
1797-
// Instead, we save the controllers for the element in a local hash and attach to .data
1798-
// later, once we have the actual element.
1799-
var controllerData = !hasElementTranscludeDirective && $element.data();
1800-
1801-
for (var directiveName in controllerDirectives) {
1802-
var directive = controllerDirectives[directiveName];
1803-
1804-
var locals = {
1805-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1806-
$element: $element,
1807-
$attrs: attrs,
1808-
$transclude: transcludeFn
1809-
};
1810-
1811-
var controller = directive.controller;
1812-
if (controller === '@') {
1813-
controller = attrs[directive.name];
1814-
}
1815-
1816-
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1817-
1818-
elementControllers[directive.name] = controllerInstance;
1819-
if (controllerData) {
1820-
controllerData['$' + directive.name + 'Controller'] = controllerInstance.instance;
1821-
}
1822-
}
1823+
setupControllers(scope, isolateScope, $element, attrs, transcludeFn, elementControllers = {});
18231824
}
18241825

18251826
if (newIsolateScopeDirective) {
@@ -1907,8 +1908,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19071908
}
19081909

19091910
// Initialize the controllers before linking
1910-
for (i in elementControllers) {
1911-
elementControllers[i]();
1911+
if (elementControllers) {
1912+
invokeEach(elementControllers);
19121913
}
19131914

19141915
// PRELINKING
@@ -1967,6 +1968,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19671968
}
19681969
}
19691970

1971+
function invokeEach(funcs) {
1972+
for (var key in funcs) {
1973+
funcs[key]();
1974+
}
1975+
}
1976+
19701977
function markDirectivesAsIsolate(directives) {
19711978
// mark all directives as needing isolate scope.
19721979
for (var j = 0, jj = directives.length; j < jj; j++) {

0 commit comments

Comments
 (0)