Skip to content

Commit 96d211b

Browse files
committed
Merge pull request #3867 from lostinpatterns/master
Allow serializers to normalize response, remove old internal serializers code
2 parents e8a2420 + d916c15 commit 96d211b

File tree

2 files changed

+3
-154
lines changed

2 files changed

+3
-154
lines changed

packages/ember-data/lib/system/store.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import {
2323
} from "ember-data/system/store/common";
2424

2525
import {
26-
convertResourceObject,
27-
normalizeResponseHelper,
28-
_normalizeSerializerPayload
26+
normalizeResponseHelper
2927
} from "ember-data/system/store/serializer-response";
3028

3129
import {
@@ -2093,9 +2091,9 @@ function _commit(adapter, store, operation, snapshot) {
20932091
if (payload.included) {
20942092
store.push({ data: payload.included });
20952093
}
2096-
data = convertResourceObject(payload.data);
2094+
data = payload.data;
20972095
}
2098-
store.didSaveRecord(internalModel, _normalizeSerializerPayload(internalModel.type, data));
2096+
store.didSaveRecord(internalModel, { data });
20992097
});
21002098

21012099
return internalModel;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import Model from 'ember-data/system/model/model';
2-
3-
const get = Ember.get;
4-
51
/*
62
This is a helper method that validates a JSON API top-level document
73
@@ -87,148 +83,3 @@ export function normalizeResponseHelper(serializer, store, modelClass, payload,
8783

8884
return normalizedResponse;
8985
}
90-
91-
/*
92-
Convert the payload from `serializer.extract` to a JSON-API Document.
93-
94-
@method _normalizeSerializerPayload
95-
@private
96-
@param {subclass of DS.Model} modelClass
97-
@param {Object} payload
98-
@return {Object} JSON-API Document
99-
*/
100-
export function _normalizeSerializerPayload(modelClass, payload) {
101-
let data = null;
102-
103-
if (payload) {
104-
if (Array.isArray(payload)) {
105-
data = payload.map((payload) => _normalizeSerializerPayloadItem(modelClass, payload));
106-
} else {
107-
data = _normalizeSerializerPayloadItem(modelClass, payload);
108-
}
109-
}
110-
111-
return { data };
112-
}
113-
114-
/*
115-
Convert the payload representing a single record from `serializer.extract` to
116-
a JSON-API Resource Object.
117-
118-
@method _normalizeSerializerPayloadItem
119-
@private
120-
@param {subclass of DS.Model} modelClass
121-
@param {Object} payload
122-
@return {Object} JSON-API Resource Object
123-
*/
124-
export function _normalizeSerializerPayloadItem(modelClass, itemPayload) {
125-
var item = {};
126-
127-
item.id = '' + itemPayload.id;
128-
item.type = modelClass.modelName;
129-
item.attributes = {};
130-
item.relationships = {};
131-
132-
modelClass.eachAttribute((name) => {
133-
if (itemPayload.hasOwnProperty(name)) {
134-
item.attributes[name] = itemPayload[name];
135-
}
136-
});
137-
138-
modelClass.eachRelationship((key, relationshipMeta) => {
139-
var relationship, value;
140-
141-
if (itemPayload.hasOwnProperty(key)) {
142-
relationship = {};
143-
value = itemPayload[key];
144-
145-
if (relationshipMeta.kind === 'belongsTo') {
146-
relationship.data = normalizeRelationshipData(key, value, relationshipMeta);
147-
//handle the belongsTo polymorphic case, where { post:1, postType: 'video' }
148-
if (relationshipMeta.options && relationshipMeta.options.polymorphic && itemPayload[key + 'Type']) {
149-
relationship.data.type = itemPayload[key + 'Type'];
150-
}
151-
} else if (relationshipMeta.kind === 'hasMany') {
152-
//|| [] because the hasMany could be === null
153-
Ember.assert(`A ${relationshipMeta.parentType} record was pushed into the store with the value of ${key} being ${Ember.inspect(value)}, but ${key} is a hasMany relationship so the value must be an array. You should probably check your data payload or serializer.`, Ember.isArray(value) || value === null);
154-
155-
relationship.data = (value || []).map((item) => normalizeRelationshipData(key, item, relationshipMeta));
156-
}
157-
}
158-
159-
if (itemPayload.links && itemPayload.links.hasOwnProperty(key)) {
160-
relationship = relationship || {};
161-
value = itemPayload.links[key];
162-
163-
relationship.links = {
164-
related: value
165-
};
166-
}
167-
168-
if (relationship) {
169-
relationship.meta = get(itemPayload, `meta.${key}`);
170-
item.relationships[key] = relationship;
171-
}
172-
});
173-
174-
return item;
175-
}
176-
177-
function normalizeRelationshipData(key, value, relationshipMeta) {
178-
if (Ember.isNone(value)) {
179-
return null;
180-
}
181-
//Temporary support for https://github.com/emberjs/data/issues/3271
182-
if (value instanceof Model) {
183-
value = { id: value.id, type: value.constructor.modelName };
184-
}
185-
if (Ember.typeOf(value) === 'object') {
186-
Ember.assert(`Ember Data expected a number or string to represent the record(s) in the '${key}' relationship instead it found an object. If this is a polymorphic relationship please specify a 'type' key. If this is an embedded relationship please include the 'DS.EmbeddedRecordsMixin' and specify the '${key}' property in your serializer's attrs object.`, value.type);
187-
if (value.id) {
188-
value.id = `${value.id}`;
189-
}
190-
return value;
191-
}
192-
193-
Ember.assert(`A ${relationshipMeta.parentType} record was pushed into the store with the value of ${key} being ${Ember.inspect(value)}, but ${key} is a belongsTo relationship so the value must not be an array. You should probably check your data payload or serializer.`, !Ember.isArray(value));
194-
return { id: `${value}`, type: relationshipMeta.type };
195-
}
196-
197-
/*
198-
This method converts a JSON-API Resource Object to a format that DS.Store
199-
understands.
200-
201-
TODO: This method works as an interim until DS.Store understands JSON-API.
202-
203-
@method convertResourceObject
204-
@param {Object} payload
205-
@return {Object} an object formatted the way DS.Store understands
206-
*/
207-
export function convertResourceObject(payload) {
208-
if (!payload) {
209-
return payload;
210-
}
211-
212-
var data = {
213-
id: payload.id,
214-
type: payload.type,
215-
links: {}
216-
};
217-
218-
if (payload.attributes) {
219-
var attributeKeys = Object.keys(payload.attributes);
220-
attributeKeys.forEach((key) => data[key] = payload.attributes[key]);
221-
}
222-
if (payload.relationships) {
223-
var relationshipKeys = Object.keys(payload.relationships);
224-
relationshipKeys.forEach((key) => {
225-
var relationship = payload.relationships[key];
226-
if (relationship.hasOwnProperty('data')) {
227-
data[key] = relationship.data;
228-
} else if (relationship.links && relationship.links.related) {
229-
data.links[key] = relationship.links.related;
230-
}
231-
});
232-
}
233-
return data;
234-
}

0 commit comments

Comments
 (0)