@@ -501,11 +501,10 @@ const Utils = class {
501
501
* @param {object } results - JSONAPI record
502
502
*/
503
503
processIncludedRecords ( context , results ) {
504
- for ( let item of get ( results , [ 'data' , 'included' ] , [ ] ) ) {
505
- // Mark record as coming from included
506
- const includedItem = this . jsonapiToNormItem ( item , 'isIncluded' )
507
- context . commit ( 'mergeRecords' , includedItem )
508
- }
504
+ context . commit (
505
+ 'mergeRecords' ,
506
+ get ( results , [ 'data' , 'included' ] , [ ] ) . map ( ( item ) => this . jsonapiToNormItem ( item , 'isIncluded' ) )
507
+ )
509
508
}
510
509
511
510
/**
@@ -532,28 +531,34 @@ const Utils = class {
532
531
* @param {object } records - Restructured records to be updated
533
532
* @param {boolean } merging - Whether or not to merge or overwrite records
534
533
*/
535
- updateRecords ( state , records , merging = this . conf . mergeRecords ) {
536
- let newState = { }
534
+ updateRecords ( state , records , mergeDefault = this . conf . mergeRecords ) {
537
535
const storeRecords = this . normToStore ( records )
536
+ let merging = { }
538
537
for ( let [ type , item ] of Object . entries ( storeRecords ) ) {
539
- newState [ type ] = { }
540
- if ( ! this . hasProperty ( state , type ) ) {
541
- state [ type ] = { }
542
- // If there's no type, then there are no existing records to merge
543
- merging = false
544
- }
545
- for ( let [ id , data ] of Object . entries ( item ) ) {
546
- if ( merging ) {
547
- const oldRecord = get ( state , [ type , id ] )
548
- if ( oldRecord ) {
549
- data = merge ( oldRecord , data )
538
+ let newRecords = item
539
+ if ( mergeDefault ) {
540
+ if ( ! ( type in merging ) ) {
541
+ merging [ type ] = true
542
+ if ( ! this . hasProperty ( state , type ) ) {
543
+ // If there's no type, then there are no existing records to merge with
544
+ merging [ type ] = false
550
545
}
551
546
}
552
- newState [ type ] [ id ] = data
547
+ if ( merging [ type ] ) {
548
+ newRecords = Object . fromEntries (
549
+ Object . entries ( item ) . map ( ( [ id , data ] ) => {
550
+ const oldRecord = get ( state , [ type , id ] )
551
+ if ( oldRecord ) {
552
+ data = merge ( oldRecord , data )
553
+ }
554
+ return [ id , data ]
555
+ } )
556
+ )
557
+ }
553
558
}
554
559
// FIXME: Review with release of Vuex5 to see if there is a new ref()/reactive() approach
555
560
// Maintain reactivity by 'touching' the 'root' state property
556
- state [ type ] = Object . assign ( { } , state [ type ] , newState [ type ] )
561
+ state [ type ] = Object . assign ( { } , state [ type ] , newRecords )
557
562
}
558
563
}
559
564
}
0 commit comments