Skip to content

Commit 3df4092

Browse files
committed
clearOnUpdate should only apply to collection gets, not item gets.
Use a new object for more atomic store updates.
1 parent 43bf627 commit 3df4092

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ As `addRecords`, but explicitly merges onto existing records.
659659

660660
#### clearRecords
661661

662-
Will remove all records from the store (of a given type) which aren't contained in a given response. Can be set as he default behaviour on updates - see [clearOnUpdate](#usage).
662+
Will remove all records from the store (of a given type) which aren't contained in a given response. Can be set as the default behaviour on updates - see [clearOnUpdate](#usage).
663663

664664
```js
665665
// Remove all records of type 'widget' from the store
@@ -761,7 +761,7 @@ For many of these options, more information is provided in the [Usage](#usage) s
761761
- `followRelationshipsData` - Whether to follow and expand relationships and store them alongside attributes in the item 'root' (defaults to `true`).
762762
- `preserveJson` - Whether actions should return the API response json (minus `data`) in `_jv/json` (for access to `meta` etc) (defaults to `false`)
763763
- `mergeRecords` - Whether new records should be merged onto existing records in the store, or just replace them (defaults to `false`).
764-
- `clearOnUpdate` - Whether the store should clear old records and only keep new records when updating. Applies to the `type(s)` associated with the new records. (defaults to false).
764+
- `clearOnUpdate` - Whether the store should clear old records and only keep new records when updating from a 'collection' get. Applies to the `type(s)` associated with the new records. (defaults to false).
765765
- `cleanPatch` - If enabled, patch object is compared to the record in the store, and only unique or modified attributes are kept in the patch. (defaults to false).
766766
- `cleanPatchProps` - If cleanPatch is enabled, an array of `_jv` properties that should be preserved - `links`, `meta`, and/or `relationships`. (defaults to `[]`).
767767
- `recurseRelationships` - If `false`, replaces recursive relationships with a normalised resource identifier (i.e `{ _jv: { type: 'x', id: 'y' } }`). (defaults to `false`).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"vuex": "^4.0.0"
5050
},
5151
"name": "jsonapi-vuex",
52-
"version": "5.4.0",
52+
"version": "5.5.0",
5353
"description": "Access restructured JSONAPI data from a Vuex Store.",
5454
"keywords": [
5555
"vue",

src/actions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ const actions = (api, conf) => {
8787
return api(apiConf).then((results) => {
8888
let resData = utils.jsonapiToNorm(results.data.data)
8989
context.commit('addRecords', resData)
90-
if (conf.clearOnUpdate) {
90+
let [type, id] = utils.getTypeId(data)
91+
if (!id && conf.clearOnUpdate) {
9192
let record = resData
9293
if (Object.keys(resData).length === 0) {
9394
// No records - assume type == endpoint
94-
let [type] = utils.getTypeId(data)
9595
record = { _jv: { type: type } }
9696
}
9797
context.commit('clearRecords', record)

src/lib.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,10 @@ const Utils = class {
529529
* @param {boolean} merging - Whether or not to merge or overwrite records
530530
*/
531531
updateRecords(state, records, merging = this.conf.mergeRecords) {
532+
let newState = {}
532533
const storeRecords = this.normToStore(records)
533534
for (let [type, item] of Object.entries(storeRecords)) {
535+
newState[type] = {}
534536
if (!this.hasProperty(state, type)) {
535537
state[type] = {}
536538
// If there's no type, then there are no existing records to merge
@@ -543,11 +545,11 @@ const Utils = class {
543545
data = merge(oldRecord, data)
544546
}
545547
}
546-
state[type][id] = data
548+
newState[type][id] = data
547549
}
548550
// FIXME: Review with release of Vuex5 to see if there is a new ref()/reactive() approach
549551
// Maintain reactivity by 'touching' the 'root' state property
550-
state[type] = { ...state[type] }
552+
state[type] = Object.assign({}, state[type], newState[type])
551553
}
552554
}
553555
}

tests/unit/actions/get.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,22 @@ describe('get', function () {
183183
expect(res).to.not.have.key('_jv')
184184
})
185185

186-
it('should call clearRecords if clearOnUpdate is set', async function () {
186+
it('should call clearRecords if clearOnUpdate is set for collections', async function () {
187+
this.mockApi.onAny().reply(200, { data: [] })
188+
189+
config.clearOnUpdate = true
190+
191+
await jsonapiModule.actions.get(stubContext, '/widgets')
192+
expect(stubContext.commit).to.have.been.calledWith('clearRecords')
193+
})
194+
195+
it('should not call clearRecords if clearOnUpdate is set for items', async function () {
187196
this.mockApi.onAny().reply(200, { data: jsonWidget1 })
188197

189198
config.clearOnUpdate = true
190199

191200
await jsonapiModule.actions.get(stubContext, normWidget1)
192-
expect(stubContext.commit).to.have.been.calledWith('clearRecords', normWidget1)
201+
expect(stubContext.commit).to.not.have.been.calledWith('clearRecords')
193202
})
194203

195204
it('should call clearRecords with endpoint if clearOnUpdate is set and no data', async function () {

0 commit comments

Comments
 (0)