-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathfragment.js
80 lines (64 loc) · 2.23 KB
/
fragment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { assert } from '@ember/debug';
import Transform from '@ember-data/serializer/transform';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import { inject as service } from '@ember/service';
/**
@module ember-data-model-fragments
*/
/**
Transform for `MF.fragment` fragment attribute which delegates work to
the fragment type's serializer
@class FragmentTransform
@namespace MF
@extends DS.Transform
*/
// eslint-disable-next-line ember/no-classic-classes
const FragmentTransform = Transform.extend({
store: service(),
type: null,
polymorphicTypeProp: null,
deserialize: function deserializeFragment(data, options, parentData) {
if (data == null) {
return null;
}
return this.deserializeSingle(data, options, parentData);
},
serialize: function serializeFragment(snapshot) {
if (!snapshot) {
return null;
}
const store = this.store;
const realSnapshot = snapshot._createSnapshot
? snapshot._createSnapshot()
: snapshot;
const serializer = store.serializerFor(
realSnapshot.modelName || realSnapshot.constructor.modelName
);
return serializer.serialize(realSnapshot);
},
modelNameFor(data, options, parentData) {
let modelName = this.type;
const polymorphicTypeProp = this.polymorphicTypeProp;
if (data && polymorphicTypeProp && data[polymorphicTypeProp]) {
modelName = data[polymorphicTypeProp];
} else if (options && typeof options.typeKey === 'function') {
modelName = options.typeKey(data, parentData);
}
return modelName;
},
deserializeSingle(data, options, parentData) {
const store = this.store;
const modelName = this.modelNameFor(data, options, parentData);
const serializer = store.serializerFor(modelName);
assert(
'The `JSONAPISerializer` is not suitable for model fragments, please use `JSONSerializer`',
!(serializer instanceof JSONAPISerializer)
);
const typeClass = store.modelFor(modelName);
const serialized = serializer.normalize(typeClass, data);
// `JSONSerializer#normalize` returns a full JSON API document, but we only
// need the attributes hash
return serialized?.data?.attributes;
},
});
export default FragmentTransform;