mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-18 23:50:23 +00:00
refactor(forms): Remove touch() method
This commit is contained in:
parent
83314913e7
commit
716dd5f3f9
4 changed files with 32 additions and 77 deletions
|
|
@ -369,16 +369,9 @@ function isEmpty(value) {
|
|||
|
||||
function textInputType(scope, element, attr, ctrl) {
|
||||
element.bind('blur', function() {
|
||||
var touched = ctrl.touch(),
|
||||
value = trim(element.val());
|
||||
|
||||
if (ctrl.viewValue !== value) {
|
||||
scope.$apply(function() {
|
||||
ctrl.setViewValue(value);
|
||||
});
|
||||
} else if (touched) {
|
||||
scope.$apply();
|
||||
}
|
||||
scope.$apply(function() {
|
||||
ctrl.setViewValue(trim(element.val()));
|
||||
});
|
||||
});
|
||||
|
||||
ctrl.render = function() {
|
||||
|
|
@ -558,7 +551,6 @@ function radioInputType(scope, element, attr, ctrl) {
|
|||
element.bind('click', function() {
|
||||
if (element[0].checked) {
|
||||
scope.$apply(function() {
|
||||
ctrl.touch();
|
||||
ctrl.setViewValue(attr.value);
|
||||
});
|
||||
};
|
||||
|
|
@ -579,7 +571,6 @@ function checkboxInputType(scope, element, attr, ctrl) {
|
|||
|
||||
element.bind('click', function() {
|
||||
scope.$apply(function() {
|
||||
ctrl.touch();
|
||||
ctrl.setViewValue(element[0].checked);
|
||||
});
|
||||
});
|
||||
|
|
@ -765,34 +756,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
|
|||
this.widgetId = $attr.name;
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.module.ng.$compileProvider.directive.ng-model.NgModelController#touch
|
||||
* @methodOf angular.module.ng.$compileProvider.directive.ng-model.NgModelController
|
||||
*
|
||||
* @return {boolean} Whether it did change state.
|
||||
*
|
||||
* @description
|
||||
* This method should be called from within a DOM event handler.
|
||||
* For example {@link angular.module.ng.$compileProvider.directive.input input} or
|
||||
* {@link angular.module.ng.$compileProvider.directive.select select} directives call it.
|
||||
*
|
||||
* It changes state to `dirty` and emits `$viewTouch` event if the state was `pristine` before.
|
||||
*/
|
||||
this.touch = function() {
|
||||
if (this.dirty) return false;
|
||||
|
||||
this.dirty = true;
|
||||
this.pristine = false;
|
||||
try {
|
||||
$scope.$emit('$viewTouch');
|
||||
} catch (e) {
|
||||
$exceptionHandler(e);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angular.module.ng.$compileProvider.directive.ng-model.NgModelController#setValidity
|
||||
|
|
@ -848,6 +811,13 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', 'ngModel',
|
|||
this.setViewValue = function(value) {
|
||||
this.viewValue = value;
|
||||
|
||||
// change to dirty
|
||||
if (this.pristine) {
|
||||
this.dirty = true;
|
||||
this.pristine = false;
|
||||
$scope.$emit('$viewTouch', this);
|
||||
}
|
||||
|
||||
forEach(this.parsers, function(fn) {
|
||||
value = fn(value);
|
||||
});
|
||||
|
|
@ -1037,16 +1007,9 @@ var ngModelInstantDirective = ['$browser', function($browser) {
|
|||
require: 'ngModel',
|
||||
link: function(scope, element, attr, ctrl) {
|
||||
var handler = function() {
|
||||
var touched = ctrl.touch(),
|
||||
value = trim(element.val());
|
||||
|
||||
if (ctrl.viewValue !== value) {
|
||||
scope.$apply(function() {
|
||||
ctrl.setViewValue(value);
|
||||
});
|
||||
} else if (touched) {
|
||||
scope.$apply();
|
||||
}
|
||||
scope.$apply(function() {
|
||||
ctrl.setViewValue(trim(element.val()));
|
||||
});
|
||||
};
|
||||
|
||||
var timeout;
|
||||
|
|
|
|||
|
|
@ -167,7 +167,6 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
|
|||
|
||||
selectElement.bind('change', function() {
|
||||
scope.$apply(function() {
|
||||
ctrl.touch();
|
||||
ctrl.setViewValue(selectElement.val());
|
||||
});
|
||||
});
|
||||
|
|
@ -189,7 +188,6 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
|
|||
array.push(option.value);
|
||||
}
|
||||
});
|
||||
ctrl.touch();
|
||||
ctrl.setViewValue(array);
|
||||
});
|
||||
});
|
||||
|
|
@ -267,11 +265,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
|
|||
value = valueFn(scope, locals);
|
||||
}
|
||||
}
|
||||
ctrl.touch();
|
||||
|
||||
if (ctrl.viewValue !== value) {
|
||||
ctrl.setViewValue(value);
|
||||
}
|
||||
ctrl.setViewValue(value);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -197,7 +197,7 @@ describe('form', function() {
|
|||
it('should have ng-pristine/ng-dirty css class', function() {
|
||||
expect(doc).toBePristine();
|
||||
|
||||
widget.touch();
|
||||
widget.setViewValue('');
|
||||
scope.$apply();
|
||||
expect(doc).toBeDirty();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -34,25 +34,6 @@ describe('NgModelController', function() {
|
|||
});
|
||||
|
||||
|
||||
describe('touch', function() {
|
||||
it('should only fire $viewTouch when pristine', function() {
|
||||
var spy = jasmine.createSpy('$viewTouch');
|
||||
scope.$on('$viewTouch', spy);
|
||||
|
||||
ctrl.touch();
|
||||
expect(ctrl.pristine).toBe(false);
|
||||
expect(ctrl.dirty).toBe(true);
|
||||
expect(spy).toHaveBeenCalledOnce();
|
||||
|
||||
spy.reset();
|
||||
ctrl.touch();
|
||||
expect(ctrl.pristine).toBe(false);
|
||||
expect(ctrl.dirty).toBe(true);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('setValidity', function() {
|
||||
|
||||
it('should emit $invalid only when $valid', function() {
|
||||
|
|
@ -150,6 +131,23 @@ describe('NgModelController', function() {
|
|||
ctrl.setViewValue('val');
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('should only fire $viewTouch when pristine', function() {
|
||||
var spy = jasmine.createSpy('$viewTouch');
|
||||
scope.$on('$viewTouch', spy);
|
||||
|
||||
ctrl.setViewValue('');
|
||||
expect(ctrl.pristine).toBe(false);
|
||||
expect(ctrl.dirty).toBe(true);
|
||||
expect(spy).toHaveBeenCalledOnce();
|
||||
|
||||
spy.reset();
|
||||
ctrl.setViewValue('');
|
||||
expect(ctrl.pristine).toBe(false);
|
||||
expect(ctrl.dirty).toBe(true);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue