Skip to content
This repository was archived by the owner on Aug 15, 2020. It is now read-only.

Commit 7b53c63

Browse files
committed
Revert "Extended schema-validate directive to handle cleaning the model when a form field element triggers the $destroy. Uses a new service, based on Select, to traverse the model and update it to the value chosen as part of the configured destroyStrategy. This destroyStrategy can be configured at the field, or as part of the forms global options. If both are defined, the field-level strategy will override."
This reverts commit a31cdbe.
1 parent a31cdbe commit 7b53c63

File tree

3 files changed

+2
-157
lines changed

3 files changed

+2
-157
lines changed

docs/index.md

-9
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,6 @@ General options most field types can handle:
634634
labelHtmlClass: "street" // CSS Class(es) to be added to the label of the field (or similar)
635635
copyValueTo: ["address.street"], // Copy values to these schema keys.
636636
condition: "person.age < 18" // Show or hide field depending on an angular expression
637-
destroyStrategy: '' // Update the model when the field is destoyed, e.g. when condition is not longer satisfied.
638637
}
639638
```
640639
@@ -824,14 +823,6 @@ function FormCtrl($scope) {
824823
Note that arrays inside arrays won't work with conditions.
825824
826825
827-
### destroyStrategy
828-
By default, when a field is removed from the DOM and the $destroy event is broadcast, the schema-validate directive
829-
will update the model to set the field value to undefined. This can be overridden by setting the destroyStrategy
830-
on a field to one of null, empty string (""), undefined, or "retain". Any other value will be ignored and the default
831-
behavior will apply. The empty string option only applies to fields that have a type of string; using the empty string
832-
with other field types will just be set to the default destroyStrategy. If you'd like to set the destroyStrategy for
833-
an entire form, add it to the formDefaults in the [globalOptions](#global-options)
834-
835826
836827
837828
Specific options and types

src/directives/schema-validate.js

+2-65
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect',
2-
function(sfValidator, sfSelect, sfUnselect) {
3-
1+
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', function(sfValidator, sfSelect) {
42
return {
53
restrict: 'A',
64
scope: false,
@@ -10,6 +8,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
108
require: 'ngModel',
119
link: function(scope, element, attrs, ngModel) {
1210

11+
1312
// We need the ngModelController on several places,
1413
// most notably for errors.
1514
// So we emit it up to the decorator directive so it can put it on scope.
@@ -102,68 +101,6 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
102101

103102
});
104103

105-
106-
var DEFAULT_DESTROY_STRATEGY;
107-
if (scope.options && scope.options.formDefaults) {
108-
var formDefaultDestroyStrategy = scope.options.formDefaults.destroyStrategy;
109-
var isValidFormDefaultDestroyStrategy = (formDefaultDestroyStrategy === undefined ||
110-
formDefaultDestroyStrategy === '' ||
111-
formDefaultDestroyStrategy === null ||
112-
formDefaultDestroyStrategy === 'retain');
113-
if (isValidFormDefaultDestroyStrategy) {
114-
DEFAULT_DESTROY_STRATEGY = formDefaultDestroyStrategy;
115-
}
116-
else {
117-
console.warn('Unrecognized formDefaults.destroyStrategy: \'%s\'. Used undefined instead.',
118-
formDefaultDestroyStrategy);
119-
DEFAULT_DESTROY_STRATEGY = undefined;
120-
}
121-
}
122-
123-
// Clean up the model when the corresponding form field is $destroy-ed.
124-
// Default behavior can be supplied as a formDefault, and behavior can be overridden in the form definition.
125-
scope.$on('$destroy', function() {
126-
var form = getForm();
127-
var destroyStrategy = form.destroyStrategy; // Either set in form definition, or as part of formDefaults.
128-
var schemaType = getSchemaType();
129-
130-
if (destroyStrategy && destroyStrategy !== 'retain' ) {
131-
// Don't recognize the strategy, so give a warning.
132-
console.warn('Unrecognized destroyStrategy: \'%s\'. Used default instead.', destroyStrategy);
133-
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
134-
}
135-
else if (schemaType !== 'string' && destroyStrategy === '') {
136-
// Only 'string' type fields can have an empty string value as a valid option.
137-
console.warn('Attempted to use empty string destroyStrategy on non-string form type. Used default instead.');
138-
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
139-
}
140-
141-
if (destroyStrategy === 'retain') {
142-
return; // Valid option to avoid destroying data in the model.
143-
}
144-
145-
destroyUsingStrategy(destroyStrategy);
146-
147-
function destroyUsingStrategy(strategy) {
148-
var strategyIsDefined = (strategy === null || strategy === '' || typeof strategy == undefined);
149-
if (!strategyIsDefined){
150-
strategy = DEFAULT_DESTROY_STRATEGY;
151-
}
152-
sfUnselect(scope.form.key, scope.model, strategy);
153-
}
154-
155-
function getSchemaType() {
156-
if (form.schema) {
157-
schemaType = form.schema.type;
158-
}
159-
else {
160-
schemaType = null;
161-
}
162-
}
163-
});
164-
165-
166-
167104
scope.schemaError = function() {
168105
return error;
169106
};

src/services/unselect.js

-83
This file was deleted.

0 commit comments

Comments
 (0)