@@ -308,44 +308,48 @@ export default function($parse, $compile, $interpolate, sfErrorMessage, sfPath,
308
308
309
309
// If the entire schema form is destroyed we don't touch the model
310
310
if ( ! scope . externalDestructionInProgress ) {
311
- let destroyStrategy = form . destroyStrategy ||
311
+ var destroyStrategy = form . destroyStrategy ||
312
312
( scope . options && scope . options . destroyStrategy ) || 'remove' ;
313
313
// No key no model, and we might have strategy 'retain'
314
314
if ( key && destroyStrategy !== 'retain' ) {
315
- // Get the object that has the property we wan't to clear.
316
- let obj = scope . model ;
317
- if ( key . length > 1 ) {
318
- obj = sfSelect ( key . slice ( 0 , key . length - 1 ) , obj ) ;
319
- }
320
-
321
- if ( obj && scope . destroyed && obj . $$hashKey && obj . $$hashKey !== scope . destroyed ) {
322
- return ;
323
- }
324
-
325
- // We can get undefined here if the form hasn't been filled out entirely
326
- if ( obj === undefined ) {
327
- return ;
328
- }
329
315
330
316
// Type can also be a list in JSON Schema
331
- let type = ( form . schema && form . schema . type ) || '' ;
317
+ var type = ( form . schema && form . schema . type ) || '' ;
332
318
333
319
// Empty means '',{} and [] for appropriate types and undefined for the rest
334
- // console.log('destroy', destroyStrategy, key, type, obj);
335
- if ( destroyStrategy === 'empty' && type . indexOf ( 'string' ) !== - 1 ) {
336
- obj [ key . slice ( - 1 ) ] = '' ;
337
- }
338
- else if ( destroyStrategy === 'empty' && type . indexOf ( 'object' ) !== - 1 ) {
339
- obj [ key . slice ( - 1 ) ] = { } ;
340
- }
341
- else if ( destroyStrategy === 'empty' && type . indexOf ( 'array' ) !== - 1 ) {
342
- obj [ key . slice ( - 1 ) ] = [ ] ;
343
- }
344
- else if ( destroyStrategy === 'null' ) {
345
- obj [ key . slice ( - 1 ) ] = null ;
320
+ let value ;
321
+ if ( destroyStrategy === 'empty' ) {
322
+ value = type . indexOf ( 'string' ) !== - 1 ? '' :
323
+ type . indexOf ( 'object' ) !== - 1 ? { } :
324
+ type . indexOf ( 'array' ) !== - 1 ? [ ] : undefined ;
325
+ } else if ( destroyStrategy === 'null' ) {
326
+ value = null ;
346
327
}
347
- else {
348
- delete obj [ key . slice ( - 1 ) ] ;
328
+
329
+ if ( value !== undefined ) {
330
+ sfSelect ( key , scope . model , value ) ;
331
+ } else {
332
+ // Get the object parent object
333
+ let obj = scope . model ;
334
+ if ( key . length > 1 ) {
335
+ obj = sfSelect ( key . slice ( 0 , key . length - 1 ) , obj )
336
+ }
337
+
338
+ // parent can be undefined if the form hasn't been filled out
339
+ // entirely
340
+ if ( obj === undefined ) {
341
+ return ;
342
+ }
343
+
344
+ // if parent is an array, then we have already been removed.
345
+ // set flag to all children (who are about to recieve a $destroy
346
+ // event as well) that we have already been destroyed
347
+ if ( angular . isArray ( obj ) ) {
348
+ scope . externalDestructionInProgress = true ;
349
+ return ;
350
+ }
351
+
352
+ delete obj [ key [ key . length - 1 ] ] ;
349
353
}
350
354
}
351
355
}
0 commit comments