@@ -103,7 +103,7 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
103103 } ) ;
104104
105105
106- var DEFAULT_DESTROY_STRATEGY = getGlobalOptionsDestroyStrategy ( ) ;
106+ /* var DEFAULT_DESTROY_STRATEGY = getGlobalOptionsDestroyStrategy();
107107
108108 function getGlobalOptionsDestroyStrategy() {
109109 var defaultStrategy = undefined;
@@ -122,14 +122,49 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
122122 }
123123 }
124124 return defaultStrategy;
125- }
125+ }*/
126+
126127
127128 // Clean up the model when the corresponding form field is $destroy-ed.
128129 // Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
129130 scope . $on ( '$destroy' , function ( ) {
131+ console . log ( 'Schema validate destroy' , scope . externalDestructionInProgress ) ;
132+
133+ // If the entire schema form is destroyed we don't touch the model
134+ if ( ! scope . externalDestructionInProgress ) {
135+ var form = getForm ( ) ;
136+ var destroyStrategy = form . destroyStrategy ||
137+ ( scope . options && scope . options . destroyStrategy ) || 'remove' ;
138+ console . log ( 'going with destroy strategy ' , destroyStrategy , scope . options )
139+ // No key no model, and we might have strategy 'retain'
140+ if ( form . key && destroyStrategy !== 'retain' ) {
141+
142+ // Get the object that has the property we wan't to clear.
143+ var obj = scope . model ;
144+ if ( form . key . length > 1 ) {
145+ obj = sfSelect ( form . key . slice ( 0 , form . key . length - 1 ) , obj ) ;
146+ }
147+
148+ // Type can also be a list in JSON Schema
149+ var type = ( form . schema && form . schema . type ) || '' ;
150+
151+ // Empty means '',{} and [] for appropriate types and undefined for the rest
152+ if ( destroyStrategy === 'empty' && type . indexOf ( 'string' ) !== - 1 ) {
153+ obj [ form . key . slice ( - 1 ) ] = '' ;
154+ } else if ( destroyStrategy === 'empty' && type . indexOf ( 'object' ) !== - 1 ) {
155+ obj [ form . key . slice ( - 1 ) ] = { } ;
156+ } else if ( destroyStrategy === 'empty' && type . indexOf ( 'array' ) !== - 1 ) {
157+ obj [ form . key . slice ( - 1 ) ] = [ ] ;
158+ } else if ( destroyStrategy === 'null' ) {
159+ obj [ form . key . slice ( - 1 ) ] = null ;
160+ } else {
161+ delete obj [ form . key . slice ( - 1 ) ] ;
162+ }
163+ }
164+ }
165+
130166
131- var form = getForm ( ) ;
132- var conditionResult = $parse ( form . condition ) ;
167+ /*var conditionResult = $parse(form.condition);
133168 var formModelNotRetained = !sfRetainModel.getFlag();
134169
135170 // If condition is defined and not satisfied and the sfSchema model should not be retained.
@@ -166,18 +201,8 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
166201 }
167202 sfUnselect(scope.form.key, scope.model, strategy);
168203 }
169-
170- function getSchemaType ( ) {
171- var sType ;
172- if ( form . schema ) {
173- sType = form . schema . type ;
174- }
175- else {
176- sType = null ;
177- }
178- return sType ;
179- }
180204 }
205+ */
181206 } ) ;
182207
183208
0 commit comments