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

Commit 64ac177

Browse files
committed
perf(ngForm,ngModel): move initial addClass to the compile phase
1 parent c94329a commit 64ac177

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/ng/directive/form.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ function FormController(element, attrs, $scope, $animate) {
6363

6464
parentForm.$addControl(form);
6565

66-
// Setup initial state of the control
67-
element.addClass(PRISTINE_CLASS);
68-
toggleValidCss(true);
69-
7066
// convenience method for easy toggling of classes
7167
function toggleValidCss(isValid, validationErrorKey) {
7268
validationErrorKey = validationErrorKey ? '-' + snake_case(validationErrorKey, '-') : '';
@@ -410,9 +406,12 @@ var formDirectiveFactory = function(isNgForm) {
410406
name: 'form',
411407
restrict: isNgForm ? 'EAC' : 'E',
412408
controller: FormController,
413-
compile: function() {
409+
compile: function ngFormCompile(formElement) {
410+
// Setup initial state of the control
411+
formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
412+
414413
return {
415-
pre: function(scope, formElement, attr, controller) {
414+
pre: function ngFormPreLink(scope, formElement, attr, controller) {
416415
if (!attr.action) {
417416
// we can't use jq events because if a form is destroyed during submission the default
418417
// action is not prevented. see #1238

src/ng/directive/input.js

+31-32
Original file line numberDiff line numberDiff line change
@@ -1621,12 +1621,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
16211621
$error = this.$error = {}; // keep invalid keys here
16221622

16231623

1624-
// Setup initial state of the control
1625-
$element
1626-
.addClass(PRISTINE_CLASS)
1627-
.addClass(UNTOUCHED_CLASS);
1628-
toggleValidCss(true);
1629-
16301624
// convenience method for easy toggling of classes
16311625
function toggleValidCss(isValid, validationErrorKey) {
16321626
validationErrorKey = validationErrorKey ? '-' + snake_case(validationErrorKey, '-') : '';
@@ -2152,40 +2146,45 @@ var ngModelDirective = function() {
21522146
return {
21532147
require: ['ngModel', '^?form', '^?ngModelOptions'],
21542148
controller: NgModelController,
2155-
link: {
2156-
pre: function(scope, element, attr, ctrls) {
2157-
// Pass the ng-model-options to the ng-model controller
2158-
if (ctrls[2]) {
2159-
ctrls[0].$options = ctrls[2].$options;
2160-
}
2149+
compile: function ngModelCompile(element) {
2150+
// Setup initial state of the control
2151+
element.addClass(PRISTINE_CLASS).addClass(UNTOUCHED_CLASS).addClass(VALID_CLASS);
21612152

2162-
// notify others, especially parent forms
2153+
return {
2154+
pre: function ngModelPreLink(scope, element, attr, ctrls) {
2155+
// Pass the ng-model-options to the ng-model controller
2156+
if (ctrls[2]) {
2157+
ctrls[0].$options = ctrls[2].$options;
2158+
}
21632159

2164-
var modelCtrl = ctrls[0],
2165-
formCtrl = ctrls[1] || nullFormCtrl;
2160+
// notify others, especially parent forms
21662161

2167-
formCtrl.$addControl(modelCtrl);
2162+
var modelCtrl = ctrls[0],
2163+
formCtrl = ctrls[1] || nullFormCtrl;
21682164

2169-
scope.$on('$destroy', function() {
2170-
formCtrl.$removeControl(modelCtrl);
2171-
});
2172-
},
2173-
post: function(scope, element, attr, ctrls) {
2174-
var modelCtrl = ctrls[0];
2175-
if (modelCtrl.$options && modelCtrl.$options.updateOn) {
2176-
element.on(modelCtrl.$options.updateOn, function(ev) {
2165+
formCtrl.$addControl(modelCtrl);
2166+
2167+
scope.$on('$destroy', function() {
2168+
formCtrl.$removeControl(modelCtrl);
2169+
});
2170+
},
2171+
post: function ngModelPostLink(scope, element, attr, ctrls) {
2172+
var modelCtrl = ctrls[0];
2173+
if (modelCtrl.$options && modelCtrl.$options.updateOn) {
2174+
element.on(modelCtrl.$options.updateOn, function(ev) {
2175+
scope.$apply(function() {
2176+
modelCtrl.$$debounceViewValueCommit(ev && ev.type);
2177+
});
2178+
});
2179+
}
2180+
2181+
element.on('blur', function(ev) {
21772182
scope.$apply(function() {
2178-
modelCtrl.$$debounceViewValueCommit(ev && ev.type);
2183+
modelCtrl.$setTouched();
21792184
});
21802185
});
21812186
}
2182-
2183-
element.on('blur', function(ev) {
2184-
scope.$apply(function() {
2185-
modelCtrl.$setTouched();
2186-
});
2187-
});
2188-
}
2187+
};
21892188
}
21902189
};
21912190
};

0 commit comments

Comments
 (0)