mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-08 22:37:20 +08:00
refactor(ngModel): get rid of revalidate
Since the validation was refactored we can now work out inside `$commitViewValue()` whether to ignore validation by looking at whether the input has native validators. Closes #8856
This commit is contained in:
committed by
Peter Bacon Darwin
parent
0f806d9659
commit
c3064f728c
@@ -914,7 +914,6 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
|
||||
var validity = element.prop(VALIDITY_STATE_PROPERTY);
|
||||
var placeholder = element[0].placeholder, noevent = {};
|
||||
var type = lowercase(element[0].type);
|
||||
ctrl.$$validityState = validity;
|
||||
|
||||
// In composition mode, users are still inputing intermediate text buffer,
|
||||
// hold the listener until composition is done.
|
||||
@@ -953,16 +952,15 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
|
||||
value = trim(value);
|
||||
}
|
||||
|
||||
// If a control is suffering from bad input, browsers discard its value, so it may be
|
||||
// necessary to revalidate even if the control's value is the same empty value twice in
|
||||
// a row.
|
||||
var revalidate = validity && ctrl.$$hasNativeValidators;
|
||||
if (ctrl.$viewValue !== value || (value === '' && revalidate)) {
|
||||
// If a control is suffering from bad input (due to native validators), browsers discard its
|
||||
// value, so it may be necessary to revalidate (by calling $setViewValue again) even if the
|
||||
// control's value is the same empty value twice in a row.
|
||||
if (ctrl.$viewValue !== value || (value === '' && ctrl.$$hasNativeValidators)) {
|
||||
if (scope.$root.$$phase) {
|
||||
ctrl.$setViewValue(value, event, revalidate);
|
||||
ctrl.$setViewValue(value, event);
|
||||
} else {
|
||||
scope.$apply(function() {
|
||||
ctrl.$setViewValue(value, event, revalidate);
|
||||
ctrl.$setViewValue(value, event);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1980,11 +1978,15 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
|
||||
* event defined in `ng-model-options`. this method is rarely needed as `NgModelController`
|
||||
* usually handles calling this in response to input events.
|
||||
*/
|
||||
this.$commitViewValue = function(revalidate) {
|
||||
this.$commitViewValue = function() {
|
||||
var viewValue = ctrl.$viewValue;
|
||||
|
||||
$timeout.cancel(pendingDebounce);
|
||||
if (!revalidate && ctrl.$$lastCommittedViewValue === viewValue) {
|
||||
|
||||
// If the view value has not changed then we should just exit, except in the case where there is
|
||||
// a native validator on the element. In this case the validation state may have changed even though
|
||||
// the viewValue has stayed empty.
|
||||
if (ctrl.$$lastCommittedViewValue === viewValue && (viewValue !== '' || !ctrl.$$hasNativeValidators)) {
|
||||
return;
|
||||
}
|
||||
ctrl.$$lastCommittedViewValue = viewValue;
|
||||
@@ -2080,14 +2082,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
|
||||
* @param {string} value Value from the view.
|
||||
* @param {string} trigger Event that triggered the update.
|
||||
*/
|
||||
this.$setViewValue = function(value, trigger, revalidate) {
|
||||
this.$setViewValue = function(value, trigger) {
|
||||
ctrl.$viewValue = value;
|
||||
if (!ctrl.$options || ctrl.$options.updateOnDefault) {
|
||||
ctrl.$$debounceViewValueCommit(trigger, revalidate);
|
||||
ctrl.$$debounceViewValueCommit(trigger);
|
||||
}
|
||||
};
|
||||
|
||||
this.$$debounceViewValueCommit = function(trigger, revalidate) {
|
||||
this.$$debounceViewValueCommit = function(trigger) {
|
||||
var debounceDelay = 0,
|
||||
options = ctrl.$options,
|
||||
debounce;
|
||||
@@ -2106,10 +2108,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
|
||||
$timeout.cancel(pendingDebounce);
|
||||
if (debounceDelay) {
|
||||
pendingDebounce = $timeout(function() {
|
||||
ctrl.$commitViewValue(revalidate);
|
||||
ctrl.$commitViewValue();
|
||||
}, debounceDelay);
|
||||
} else {
|
||||
ctrl.$commitViewValue(revalidate);
|
||||
ctrl.$commitViewValue();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user