From 28a1bf96cae2c051fe37ebcd9367226f45f7130d Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sat, 7 Jul 2012 19:02:04 +0200 Subject: [PATCH 1/2] feat(form): add ability to reset a form to its pristine state --- src/ng/directive/form.js | 28 +++++++++++++++++++++++++++- src/ng/directive/input.js | 16 ++++++++++++++++ test/ng/directive/formSpec.js | 26 ++++++++++++++++++++++++++ test/ng/directive/inputSpec.js | 12 ++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index cc229759b9cb..b3d9a3c96a6a 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -5,7 +5,8 @@ var nullFormCtrl = { $addControl: noop, $removeControl: noop, $setValidity: noop, - $setDirty: noop + $setDirty: noop, + $setPristine: noop }; /** @@ -119,6 +120,31 @@ function FormController(element, attrs) { form.$pristine = false; }; + /** + * @ngdoc function + * @name ng.directive:form.FormController#$setPristine + * @methodOf ng.directive:form.FormController + * + * @description + * Sets the form to its pristine state. + * + * This method can be called to remove the 'ng-dirty' class and set the form to its pristine + * state (ng-pristine class). Calling this method will also affect all the controls contained in + * this form. + * + * Setting a form back to a pristine state is often useful when we want to 'reuse' a form after + * saving or resetting it. + */ + form.$setPristine = function () { + element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS); + form.$dirty = false; + form.$pristine = true; + angular.forEach(form, function(value, key) { + if (value.$name && value.$setPristine) { + value.$setPristine(); + } + }); + }; } diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 40b29b236900..65dc944c398a 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -947,6 +947,22 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ parentForm.$setValidity(validationErrorKey, isValid, this); }; + /** + * @ngdoc function + * @name ng.directive:ngModel.NgModelController#$setPristine + * @methodOf ng.directive:ngModel.NgModelController + * + * @description + * Sets the control to its pristine state. + * + * This method can be called to remove the 'ng-dirty' class and set the control to its pristine + * state (ng-pristine class). + */ + this.$setPristine = function () { + this.$dirty = false; + this.$pristine = true; + $element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS); + } /** * @ngdoc function diff --git a/test/ng/directive/formSpec.js b/test/ng/directive/formSpec.js index 8b87f266c2cf..433ac7630c40 100644 --- a/test/ng/directive/formSpec.js +++ b/test/ng/directive/formSpec.js @@ -327,4 +327,30 @@ describe('form', function() { expect(doc).toBeDirty(); }); }); + + describe('set pristine state', function() { + + beforeEach(function() { + doc = $compile( + '
' + + '' + + '
')(scope); + + scope.$digest(); + }); + + it('should set form and controls to the pristine state', function() { + + var form = scope.form, input = doc.find('input').eq(0) + + control.$setViewValue(''); + scope.$apply(); + expect(doc).toBeDirty(); + + form.$setPristine(); + expect(doc).toBePristine(); + expect(input).toBePristine(); + expect(control.$pristine).toBeTruthy(); + }); + }); }); diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 3b511011d1a6..4e3353cefccc 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -117,6 +117,18 @@ describe('NgModelController', function() { }); }); + describe('setPristine', function() { + + it('should set control to its pristine state', function() { + ctrl.$setViewValue('edit'); + expect(ctrl.$dirty).toBe(true); + expect(ctrl.$pristine).toBe(false); + + ctrl.$setPristine(); + expect(ctrl.$dirty).toBe(false); + expect(ctrl.$pristine).toBe(true); + }); + }); describe('view -> model', function() { From df35bb74e2ccf43f684a029918ebfc45e10a1998 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Tue, 10 Jul 2012 19:43:44 +0200 Subject: [PATCH 2/2] feat(form): add ability to reset a form to its pristine state - corrections --- src/ng/directive/form.js | 4 ++-- test/ng/directive/formSpec.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index b3d9a3c96a6a..51dff495fab4 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -129,8 +129,8 @@ function FormController(element, attrs) { * Sets the form to its pristine state. * * This method can be called to remove the 'ng-dirty' class and set the form to its pristine - * state (ng-pristine class). Calling this method will also affect all the controls contained in - * this form. + * state (ng-pristine class). This method will also propagate to all the controls contained + * in this form. * * Setting a form back to a pristine state is often useful when we want to 'reuse' a form after * saving or resetting it. diff --git a/test/ng/directive/formSpec.js b/test/ng/directive/formSpec.js index 433ac7630c40..41425be4c7e4 100644 --- a/test/ng/directive/formSpec.js +++ b/test/ng/directive/formSpec.js @@ -341,7 +341,7 @@ describe('form', function() { it('should set form and controls to the pristine state', function() { - var form = scope.form, input = doc.find('input').eq(0) + var form = scope.form, input = doc.find('input').eq(0); control.$setViewValue(''); scope.$apply();