diff options
| author | Pawel Kozlowski | 2012-07-07 19:02:04 +0200 | 
|---|---|---|
| committer | Igor Minar | 2012-11-26 16:44:34 +0100 | 
| commit | 733a97adf87bf8f7ec6be22b37c4676cf7b5fc2b (patch) | |
| tree | 99422e0e402fe4b4340552f8f80a4feb3ac861f0 /src/ng/directive/form.js | |
| parent | 96ed9ff59a454486c88bdf92ad9d28ab8864b85e (diff) | |
| download | angular.js-733a97adf87bf8f7ec6be22b37c4676cf7b5fc2b.tar.bz2 | |
feat(form): add ability to reset a form to pristine state
Retting a form to pristine state will cause all of the nested
form and form controls to be recursively reset as well.
Closes #856
Diffstat (limited to 'src/ng/directive/form.js')
| -rw-r--r-- | src/ng/directive/form.js | 33 | 
1 files changed, 31 insertions, 2 deletions
| diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index 4f06f093..b4d500c6 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  };  /** @@ -37,7 +38,8 @@ function FormController(element, attrs) {    var form = this,        parentForm = element.parent().controller('form') || nullFormCtrl,        invalidCount = 0, // used to easily determine if we are valid -      errors = form.$error = {}; +      errors = form.$error = {}, +      controls = [];    // init state    form.$name = attrs.name; @@ -61,6 +63,8 @@ function FormController(element, attrs) {    }    form.$addControl = function(control) { +    controls.push(control); +      if (control.$name && !form.hasOwnProperty(control.$name)) {        form[control.$name] = control;      } @@ -73,6 +77,8 @@ function FormController(element, attrs) {      forEach(errors, function(queue, validationToken) {        form.$setValidity(validationToken, true, control);      }); + +    arrayRemove(controls, control);    };    form.$setValidity = function(validationToken, isValid, control) { @@ -120,6 +126,29 @@ function FormController(element, attrs) {      parentForm.$setDirty();    }; +  /** +   * @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). 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. +   */ +  form.$setPristine = function () { +    element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS); +    form.$dirty = false; +    form.$pristine = true; +    forEach(controls, function(control) { +      control.$setPristine(); +    }); +  };  } | 
