Skip to content

Commit 4568337

Browse files
committed
Only clean model if the form's condition is no longer satisfied. This prevents the form from wiping itself when a $destroy event occurs that is not connected to the condition logic (such as if the FormController contains an element that wraps the form with an ng-if).
1 parent 0d6f417 commit 4568337

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

src/directives/schema-validate.js

+41-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect',
2-
function(sfValidator, sfSelect, sfUnselect) {
1+
angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', 'sfUnselect', '$parse',
2+
function(sfValidator, sfSelect, sfUnselect, $parse) {
33

44
return {
55
restrict: 'A',
@@ -128,48 +128,53 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
128128
// Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
129129
scope.$on('$destroy', function() {
130130
var form = getForm();
131+
var conditionResult = $parse(form.condition);
132+
console.log(conditionResult(scope));
131133

132-
// Either set in form definition, or as part of globalOptions.
133-
var destroyStrategy =
134-
!form.hasOwnProperty('destroyStrategy') ? DEFAULT_DESTROY_STRATEGY : form.destroyStrategy;
135-
var schemaType = getSchemaType();
134+
if (form.hasOwnProperty('condition') && !conditionResult(scope)) { // If condition is defined and not satisfied.
136135

137-
if (destroyStrategy && destroyStrategy !== 'retain' ) {
138-
// Don't recognize the strategy, so give a warning.
139-
console.warn('%s has defined unrecognized destroyStrategy: \'%s\'. Used default instead.',
140-
attrs.name, destroyStrategy);
141-
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
142-
}
143-
else if (schemaType !== 'string' && destroyStrategy === '') {
144-
// Only 'string' type fields can have an empty string value as a valid option.
145-
console.warn('%s attempted to use empty string destroyStrategy on non-string form type. ' +
146-
'Used default instead.', attrs.name);
147-
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
148-
}
136+
// Either set in form definition, or as part of globalOptions.
137+
var destroyStrategy =
138+
!form.hasOwnProperty('destroyStrategy') ? DEFAULT_DESTROY_STRATEGY : form.destroyStrategy;
139+
var schemaType = getSchemaType();
149140

150-
if (destroyStrategy === 'retain') {
151-
return; // Valid option to avoid destroying data in the model.
152-
}
153-
154-
destroyUsingStrategy(destroyStrategy);
141+
if (destroyStrategy && destroyStrategy !== 'retain') {
142+
// Don't recognize the strategy, so give a warning.
143+
console.warn('%s has defined unrecognized destroyStrategy: \'%s\'. Used default instead.',
144+
attrs.name, destroyStrategy);
145+
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
146+
}
147+
else if (schemaType !== 'string' && destroyStrategy === '') {
148+
// Only 'string' type fields can have an empty string value as a valid option.
149+
console.warn('%s attempted to use empty string destroyStrategy on non-string form type. ' +
150+
'Used default instead.', attrs.name);
151+
destroyStrategy = DEFAULT_DESTROY_STRATEGY;
152+
}
155153

156-
function destroyUsingStrategy(strategy) {
157-
var strategyIsDefined = (strategy === null || strategy === '' || strategy === undefined);
158-
if (!strategyIsDefined){
159-
strategy = DEFAULT_DESTROY_STRATEGY;
154+
if (destroyStrategy === 'retain') {
155+
return; // Valid option to avoid destroying data in the model.
160156
}
161-
sfUnselect(scope.form.key, scope.model, strategy);
162-
}
163157

164-
function getSchemaType() {
165-
var sType;
166-
if (form.schema) {
167-
sType = form.schema.type;
158+
destroyUsingStrategy(destroyStrategy);
159+
160+
function destroyUsingStrategy(strategy) {
161+
var strategyIsDefined = (strategy === null || strategy === '' || strategy === undefined);
162+
if (!strategyIsDefined) {
163+
strategy = DEFAULT_DESTROY_STRATEGY;
164+
}
165+
sfUnselect(scope.form.key, scope.model, strategy);
168166
}
169-
else {
170-
sType = null;
167+
168+
function getSchemaType() {
169+
var sType;
170+
if (form.schema) {
171+
sType = form.schema.type;
172+
}
173+
else {
174+
sType = null;
175+
}
176+
return sType;
171177
}
172-
return sType;
173178
}
174179
});
175180

0 commit comments

Comments
 (0)