refactor(forms): Remove touch() method

This commit is contained in:
Vojta Jina 2012-03-09 17:19:48 -08:00
parent 83314913e7
commit 716dd5f3f9
4 changed files with 32 additions and 77 deletions

View file

@ -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;

View file

@ -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);
});
});

View file

@ -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();
});

View file

@ -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();
});
});