Skip to content

Commit 5203479

Browse files
author
Daan van der Kallen
committed
Do not parse model again if it came from cache
1 parent 7d6a09a commit 5203479

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/Model.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export default class Model {
195195
this.__parseRelations(options.relations, options.relsFromCache);
196196
}
197197
if (data) {
198-
this.parse(data);
198+
this.parse(data, options.relsFromCache);
199199
}
200200
this.initialize();
201201

@@ -599,7 +599,7 @@ export default class Model {
599599
}
600600

601601
@action
602-
parse(data) {
602+
parse(data, relsFromCache = {}) {
603603
invariant(
604604
isPlainObject(data),
605605
`Parameter supplied to \`parse()\` is not an object, got: ${JSON.stringify(
@@ -612,10 +612,17 @@ export default class Model {
612612
if (this.__attributes.includes(attr)) {
613613
this[attr] = this.__parseAttr(attr, value);
614614
} else if (this.__activeCurrentRelations.includes(attr)) {
615+
const cacheData = relsFromCache[attr];
616+
617+
// Model came from cache so we do not have to parse it again
618+
if (cacheData && cacheData.model) {
619+
return
620+
}
621+
615622
// In Binder, a relation property is an `int` or `[int]`, referring to its ID.
616623
// However, it can also be an object if there are nested relations (non flattened).
617624
if (isPlainObject(value) || (Array.isArray(value) && value.every(isPlainObject))) {
618-
this[attr].parse(value);
625+
this[attr].parse(value, cacheData && cacheData.rels);
619626
} else if (value === null) {
620627
// The relation is cleared.
621628
this[attr].clear();

0 commit comments

Comments
 (0)