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

perf(ngForm,ngModel): move initial addClass to the compile phase #8268

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ function FormController(element, attrs, $scope, $animate, $interpolate) {

parentForm.$addControl(form);

// Setup initial state of the control
element.addClass(PRISTINE_CLASS);

/**
* @ngdoc method
* @name form.FormController#$rollbackViewValue
Expand Down Expand Up @@ -451,9 +448,12 @@ var formDirectiveFactory = function(isNgForm) {
name: 'form',
restrict: isNgForm ? 'EAC' : 'E',
controller: FormController,
compile: function() {
compile: function ngFormCompile(formElement) {
// Setup initial state of the control
formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);

return {
pre: function(scope, formElement, attr, controller) {
pre: function ngFormPreLink(scope, formElement, attr, controller) {
if (!attr.action) {
// we can't use jq events because if a form is destroyed during submission the default
// action is not prevented. see #1238
Expand Down
69 changes: 35 additions & 34 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1756,11 +1756,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
var parentForm = $element.inheritedData('$formController') || nullFormCtrl,
currentValidationRunId = 0;

// Setup initial state of the control
$element
.addClass(PRISTINE_CLASS)
.addClass(UNTOUCHED_CLASS);

/**
* @ngdoc method
* @name ngModel.NgModelController#$setValidity
Expand Down Expand Up @@ -2380,42 +2375,47 @@ var ngModelDirective = function() {
restrict: 'A',
require: ['ngModel', '^?form', '^?ngModelOptions'],
controller: NgModelController,
link: {
pre: function(scope, element, attr, ctrls) {
var modelCtrl = ctrls[0],
formCtrl = ctrls[1] || nullFormCtrl;
compile: function ngModelCompile(element) {
// Setup initial state of the control
element.addClass(PRISTINE_CLASS).addClass(UNTOUCHED_CLASS).addClass(VALID_CLASS);

modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options);
return {
pre: function ngModelPreLink(scope, element, attr, ctrls) {
var modelCtrl = ctrls[0],
formCtrl = ctrls[1] || nullFormCtrl;

// notify others, especially parent forms
formCtrl.$addControl(modelCtrl);
modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options);

attr.$observe('name', function(newValue) {
if (modelCtrl.$name !== newValue) {
formCtrl.$$renameControl(modelCtrl, newValue);
}
});
// notify others, especially parent forms
formCtrl.$addControl(modelCtrl);

scope.$on('$destroy', function() {
formCtrl.$removeControl(modelCtrl);
});
},
post: function(scope, element, attr, ctrls) {
var modelCtrl = ctrls[0];
if (modelCtrl.$options && modelCtrl.$options.updateOn) {
element.on(modelCtrl.$options.updateOn, function(ev) {
modelCtrl.$$debounceViewValueCommit(ev && ev.type);
attr.$observe('name', function(newValue) {
if (modelCtrl.$name !== newValue) {
formCtrl.$$renameControl(modelCtrl, newValue);
}
});
}

element.on('blur', function(ev) {
if (modelCtrl.$touched) return;
scope.$on('$destroy', function() {
formCtrl.$removeControl(modelCtrl);
});
},
post: function ngModelPostLink(scope, element, attr, ctrls) {
var modelCtrl = ctrls[0];
if (modelCtrl.$options && modelCtrl.$options.updateOn) {
element.on(modelCtrl.$options.updateOn, function(ev) {
modelCtrl.$$debounceViewValueCommit(ev && ev.type);
});
}

element.on('blur', function(ev) {
if (modelCtrl.$touched) return;

scope.$apply(function() {
modelCtrl.$setTouched();
scope.$apply(function() {
modelCtrl.$setTouched();
});
});
});
}
}
};
}
};
};
Expand Down Expand Up @@ -2970,8 +2970,9 @@ function addSetValidityMethod(context) {
parentForm = context.parentForm,
$animate = context.$animate;

classCache[INVALID_CLASS] = !(classCache[VALID_CLASS] = $element.hasClass(VALID_CLASS));

ctrl.$setValidity = setValidity;
toggleValidationCss('', true);

function setValidity(validationErrorKey, state, options) {
if (state === undefined) {
Expand Down