|
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) { |
3 | 3 |
|
4 | 4 | return {
|
5 | 5 | restrict: 'A',
|
@@ -128,48 +128,53 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
|
128 | 128 | // Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
|
129 | 129 | scope.$on('$destroy', function() {
|
130 | 130 | var form = getForm();
|
| 131 | + var conditionResult = $parse(form.condition); |
| 132 | + console.log(conditionResult(scope)); |
131 | 133 |
|
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. |
136 | 135 |
|
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(); |
149 | 140 |
|
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 | + } |
155 | 153 |
|
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. |
160 | 156 | }
|
161 |
| - sfUnselect(scope.form.key, scope.model, strategy); |
162 |
| - } |
163 | 157 |
|
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); |
168 | 166 | }
|
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; |
171 | 177 | }
|
172 |
| - return sType; |
173 | 178 | }
|
174 | 179 | });
|
175 | 180 |
|
|
0 commit comments