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

Commit b1ee538

Browse files
jbedardIgorMinar
authored andcommitted
perf(ngForm,ngModel): move initial addClass to the compile phase
Closes #8268
1 parent ab80cd9 commit b1ee538

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

src/ng/directive/form.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ function FormController(element, attrs, $scope, $animate, $interpolate) {
7676

7777
parentForm.$addControl(form);
7878

79-
// Setup initial state of the control
80-
element.addClass(PRISTINE_CLASS);
81-
8279
/**
8380
* @ngdoc method
8481
* @name form.FormController#$rollbackViewValue
@@ -451,9 +448,12 @@ var formDirectiveFactory = function(isNgForm) {
451448
name: 'form',
452449
restrict: isNgForm ? 'EAC' : 'E',
453450
controller: FormController,
454-
compile: function() {
451+
compile: function ngFormCompile(formElement) {
452+
// Setup initial state of the control
453+
formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
454+
455455
return {
456-
pre: function(scope, formElement, attr, controller) {
456+
pre: function ngFormPreLink(scope, formElement, attr, controller) {
457457
if (!attr.action) {
458458
// we can't use jq events because if a form is destroyed during submission the default
459459
// action is not prevented. see #1238

src/ng/directive/input.js

+35-34
Original file line numberDiff line numberDiff line change
@@ -1756,11 +1756,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
17561756
var parentForm = $element.inheritedData('$formController') || nullFormCtrl,
17571757
currentValidationRunId = 0;
17581758

1759-
// Setup initial state of the control
1760-
$element
1761-
.addClass(PRISTINE_CLASS)
1762-
.addClass(UNTOUCHED_CLASS);
1763-
17641759
/**
17651760
* @ngdoc method
17661761
* @name ngModel.NgModelController#$setValidity
@@ -2380,42 +2375,47 @@ var ngModelDirective = function() {
23802375
restrict: 'A',
23812376
require: ['ngModel', '^?form', '^?ngModelOptions'],
23822377
controller: NgModelController,
2383-
link: {
2384-
pre: function(scope, element, attr, ctrls) {
2385-
var modelCtrl = ctrls[0],
2386-
formCtrl = ctrls[1] || nullFormCtrl;
2378+
compile: function ngModelCompile(element) {
2379+
// Setup initial state of the control
2380+
element.addClass(PRISTINE_CLASS).addClass(UNTOUCHED_CLASS).addClass(VALID_CLASS);
23872381

2388-
modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options);
2382+
return {
2383+
pre: function ngModelPreLink(scope, element, attr, ctrls) {
2384+
var modelCtrl = ctrls[0],
2385+
formCtrl = ctrls[1] || nullFormCtrl;
23892386

2390-
// notify others, especially parent forms
2391-
formCtrl.$addControl(modelCtrl);
2387+
modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options);
23922388

2393-
attr.$observe('name', function(newValue) {
2394-
if (modelCtrl.$name !== newValue) {
2395-
formCtrl.$$renameControl(modelCtrl, newValue);
2396-
}
2397-
});
2389+
// notify others, especially parent forms
2390+
formCtrl.$addControl(modelCtrl);
23982391

2399-
scope.$on('$destroy', function() {
2400-
formCtrl.$removeControl(modelCtrl);
2401-
});
2402-
},
2403-
post: function(scope, element, attr, ctrls) {
2404-
var modelCtrl = ctrls[0];
2405-
if (modelCtrl.$options && modelCtrl.$options.updateOn) {
2406-
element.on(modelCtrl.$options.updateOn, function(ev) {
2407-
modelCtrl.$$debounceViewValueCommit(ev && ev.type);
2392+
attr.$observe('name', function(newValue) {
2393+
if (modelCtrl.$name !== newValue) {
2394+
formCtrl.$$renameControl(modelCtrl, newValue);
2395+
}
24082396
});
2409-
}
24102397

2411-
element.on('blur', function(ev) {
2412-
if (modelCtrl.$touched) return;
2398+
scope.$on('$destroy', function() {
2399+
formCtrl.$removeControl(modelCtrl);
2400+
});
2401+
},
2402+
post: function ngModelPostLink(scope, element, attr, ctrls) {
2403+
var modelCtrl = ctrls[0];
2404+
if (modelCtrl.$options && modelCtrl.$options.updateOn) {
2405+
element.on(modelCtrl.$options.updateOn, function(ev) {
2406+
modelCtrl.$$debounceViewValueCommit(ev && ev.type);
2407+
});
2408+
}
2409+
2410+
element.on('blur', function(ev) {
2411+
if (modelCtrl.$touched) return;
24132412

2414-
scope.$apply(function() {
2415-
modelCtrl.$setTouched();
2413+
scope.$apply(function() {
2414+
modelCtrl.$setTouched();
2415+
});
24162416
});
2417-
});
2418-
}
2417+
}
2418+
};
24192419
}
24202420
};
24212421
};
@@ -2970,8 +2970,9 @@ function addSetValidityMethod(context) {
29702970
parentForm = context.parentForm,
29712971
$animate = context.$animate;
29722972

2973+
classCache[INVALID_CLASS] = !(classCache[VALID_CLASS] = $element.hasClass(VALID_CLASS));
2974+
29732975
ctrl.$setValidity = setValidity;
2974-
toggleValidationCss('', true);
29752976

29762977
function setValidity(validationErrorKey, state, options) {
29772978
if (state === undefined) {

0 commit comments

Comments
 (0)