feat(ngModelController): add $setDirty method

- extract existing functionality to public method: $setDirty
- add tests to corresponding changes
- refactor code to use extracted method

Closes #10038
Closes #10049
This commit is contained in:
IShotTheSheriff
2014-11-13 22:44:17 +01:00
committed by Martin Staffa
parent bb16759f0b
commit e8941c0fe5
2 changed files with 37 additions and 5 deletions

View File

@@ -1870,6 +1870,25 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
$animate.addClass($element, PRISTINE_CLASS);
};
/**
* @ngdoc method
* @name ngModel.NgModelController#$setDirty
*
* @description
* Sets the control to its dirty state.
*
* This method can be called to remove the `ng-pristine` class and set the control to its dirty
* state (`ng-dirty` class). A model is considered to be dirty when the control has been changed
* from when first compiled.
*/
this.$setDirty = function() {
ctrl.$dirty = true;
ctrl.$pristine = false;
$animate.removeClass($element, PRISTINE_CLASS);
$animate.addClass($element, DIRTY_CLASS);
parentForm.$setDirty();
};
/**
* @ngdoc method
* @name ngModel.NgModelController#$setUntouched
@@ -2139,11 +2158,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
// change to dirty
if (ctrl.$pristine) {
ctrl.$dirty = true;
ctrl.$pristine = false;
$animate.removeClass($element, PRISTINE_CLASS);
$animate.addClass($element, DIRTY_CLASS);
parentForm.$setDirty();
this.$setDirty();
}
this.$$parseAndValidate();
};

View File

@@ -139,6 +139,23 @@ describe('NgModelController', function() {
});
});
describe('setDirty', function() {
it('should set control to its dirty state', function() {
expect(ctrl.$pristine).toBe(true);
expect(ctrl.$dirty).toBe(false);
ctrl.$setDirty();
expect(ctrl.$pristine).toBe(false);
expect(ctrl.$dirty).toBe(true);
});
it('should set parent form to its dirty state', function() {
ctrl.$setDirty();
expect(parentFormCtrl.$setDirty).toHaveBeenCalled();
});
});
describe('setUntouched', function() {
it('should set control to its untouched state', function() {