Skip to content

Commit f01c3d7

Browse files
committed
Add inData flag to distinguish records coming via includes.
1 parent d9f492c commit f01c3d7

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ Distinguishing between the `attributes` and `relationships` in the 'root' is sim
678678

679679
- `isRel` - a function which returns True/False for a given name.
680680

681+
- `inData` - this property is set to `true` on items returned as part of the `data` property, if `included` records were also returned. This is useful if you wish to filter out included records when displaying data, but still want to access them as related data.
682+
681683
These are particularly useful in `Vue` templates. For example to iterate over an item, picking out just the attributes:
682684

683685
```html

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"babel-polyfill": "^6.26.0",
1818
"chai": "^4.3.4",
1919
"chai-as-promised": "^7.1.1",
20-
"chromedriver": "^93",
20+
"chromedriver": "^95",
2121
"concurrently": "^6.2.1",
2222
"core-js": "^3.16.1",
2323
"eslint": "^7.32.0",
@@ -49,7 +49,7 @@
4949
"vuex": "^4.0.0"
5050
},
5151
"name": "jsonapi-vuex",
52-
"version": "5.6.0",
52+
"version": "5.7.0",
5353
"description": "Access restructured JSONAPI data from a Vuex Store.",
5454
"keywords": [
5555
"vue",

src/actions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ const actions = (api, conf) => {
8585
config['data'] = config['data'] || {}
8686
merge(apiConf, config)
8787
return api(apiConf).then((results) => {
88-
let resData = utils.jsonapiToNorm(results.data.data)
88+
// If there is included data, set 'inData' flag on all 'root' records
89+
let setInData = false
90+
if (get(results, ['data', 'included'])) {
91+
setInData = true
92+
}
93+
let resData = utils.jsonapiToNorm(results.data.data, setInData)
8994
context.commit('addRecords', resData)
9095
let [type, id] = utils.getTypeId(data)
9196
if (!id && conf.clearOnUpdate) {

src/lib.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,20 +361,21 @@ const Utils = class {
361361
* Convert JSONAPI record(s) to restructured data
362362
* @memberof module:jsonapi-vuex.utils
363363
* @param {object} data - The `data` object from a JSONAPI record
364+
* @param {boolean} inData - Flag which if true, will mark these records as coming from 'data' not via 'included'
364365
* @return {object} Restructured data
365366
*/
366-
jsonapiToNorm(data) {
367+
jsonapiToNorm(data, inData) {
367368
const norm = {}
368369
if (Array.isArray(data)) {
369370
data.forEach((item) => {
370371
let { id } = item
371372
if (!this.hasProperty(norm, id)) {
372373
norm[id] = {}
373374
}
374-
Object.assign(norm[id], this.jsonapiToNormItem(item))
375+
Object.assign(norm[id], this.jsonapiToNormItem(item, inData))
375376
})
376377
} else {
377-
Object.assign(norm, this.jsonapiToNormItem(data))
378+
Object.assign(norm, this.jsonapiToNormItem(data, inData))
378379
}
379380
return norm
380381
}
@@ -383,9 +384,10 @@ const Utils = class {
383384
* Restructure a single jsonapi item. Used internally by {@link module:jsonapi-vuex.utils.jsonapiToNorm}
384385
* @memberof module:jsonapi-vuex._internal
385386
* @param {object} data - JSONAPI record
387+
* @param {boolean} inData - Flag, which if true will mark this record as coming from 'data', not via 'included'
386388
* @return {object} Restructured data
387389
*/
388-
jsonapiToNormItem(data) {
390+
jsonapiToNormItem(data, inData = false) {
389391
if (!data) {
390392
return {}
391393
}
@@ -394,6 +396,9 @@ const Utils = class {
394396
// Create a new object omitting attributes
395397
const { attributes, ...normNoAttrs } = norm[this.jvtag] // eslint-disable-line no-unused-vars
396398
norm[this.jvtag] = normNoAttrs
399+
if (inData) {
400+
norm[this.jvtag].inData = inData
401+
}
397402
return norm
398403
}
399404

tests/unit/jsonapi-vuex.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ describe('jsonapi-vuex tests', function () {
356356
it("should preserve deeply nested '_jv' keys", function () {
357357
expect(utils.jsonapiToNormItem(jsonWidget1)).to.deep.equal(normWidget1)
358358
})
359+
it("should set the 'inData' property if inData param is true", function () {
360+
// Set inData param to true
361+
normWidget1._jv.inData = true
362+
expect(utils.jsonapiToNormItem(jsonWidget1, true)).to.deep.equal(normWidget1)
363+
})
359364
})
360365

361366
describe('utils.jsonapiToNorm', function () {

0 commit comments

Comments
 (0)