@ember-data/serializer 4.10.0-alpha.2 → 4.10.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/addon/-private.js +210 -0
- package/addon/-private.js.map +1 -0
- package/addon/{-private/embedded-records-mixin.js → embedded-records-mixin-0a9e9148.js} +87 -146
- package/addon/embedded-records-mixin-0a9e9148.js.map +1 -0
- package/addon/index.js +178 -0
- package/addon/index.js.map +1 -0
- package/addon/json-api.js +96 -233
- package/addon/json-api.js.map +1 -0
- package/addon/json.js +198 -432
- package/addon/json.js.map +1 -0
- package/addon/rest.js +133 -270
- package/addon/rest.js.map +1 -0
- package/addon/{-private/transforms/transform.js → transform-63fba437.js} +9 -15
- package/addon/transform-63fba437.js.map +1 -0
- package/addon/transform.js +3 -4
- package/addon/transform.js.map +1 -0
- package/addon-main.js +90 -0
- package/package.json +39 -8
- package/addon/-private/index.js +0 -11
- package/addon/-private/transforms/boolean.js +0 -70
- package/addon/-private/transforms/date.js +0 -59
- package/addon/-private/transforms/number.js +0 -57
- package/addon/-private/transforms/string.js +0 -38
- package/addon/index.ts +0 -259
- package/blueprints/serializer/files/__root__/__path__/__name__.js +0 -4
- package/blueprints/serializer/index.js +0 -14
- package/blueprints/serializer/native-files/__root__/__path__/__name__.js +0 -4
- package/blueprints/serializer-test/index.js +0 -29
- package/blueprints/serializer-test/mocha-files/__root__/__path__/__test__.js +0 -20
- package/blueprints/serializer-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -25
- package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +0 -24
- package/blueprints/transform/files/__root__/__path__/__name__.js +0 -11
- package/blueprints/transform/index.js +0 -7
- package/blueprints/transform/native-files/__root__/__path__/__name__.js +0 -11
- package/blueprints/transform-test/index.js +0 -29
- package/blueprints/transform-test/mocha-files/__root__/__path__/__test__.js +0 -17
- package/blueprints/transform-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -14
- package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +0 -13
- package/index.js +0 -25
package/addon/json.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @ember-data/serializer/json
|
|
3
|
-
*/
|
|
4
1
|
import { getOwner } from '@ember/application';
|
|
5
2
|
import { assert, warn } from '@ember/debug';
|
|
6
3
|
import { dasherize } from '@ember/string';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import Serializer from '@ember-data/serializer';
|
|
4
|
+
import { typeOf, isNone } from '@ember/utils';
|
|
10
5
|
import { coerceId } from '@ember-data/store/-private';
|
|
6
|
+
import _class from "./index";
|
|
11
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @module @ember-data/serializer/json
|
|
10
|
+
*/
|
|
12
11
|
const SOURCE_POINTER_REGEXP = /^\/?data\/(attributes|relationships)\/(.*)/;
|
|
13
12
|
const SOURCE_POINTER_PRIMARY_REGEXP = /^\/?data/;
|
|
14
13
|
const PRIMARY_ATTRIBUTE_KEY = 'base';
|
|
@@ -84,7 +83,7 @@ const PRIMARY_ATTRIBUTE_KEY = 'base';
|
|
|
84
83
|
@public
|
|
85
84
|
@extends Serializer
|
|
86
85
|
*/
|
|
87
|
-
const JSONSerializer =
|
|
86
|
+
const JSONSerializer = _class.extend({
|
|
88
87
|
/**
|
|
89
88
|
The `primaryKey` is used when serializing and deserializing
|
|
90
89
|
data. Ember Data always uses the `id` property to store the id of
|
|
@@ -92,101 +91,80 @@ const JSONSerializer = Serializer.extend({
|
|
|
92
91
|
convention. In these cases it is useful to override the
|
|
93
92
|
`primaryKey` property to match the `primaryKey` of your external
|
|
94
93
|
store.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
```app/serializers/application.js
|
|
94
|
+
Example
|
|
95
|
+
```app/serializers/application.js
|
|
99
96
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
100
|
-
|
|
101
|
-
export default class ApplicationSerializer extends JSONSerializer {
|
|
97
|
+
export default class ApplicationSerializer extends JSONSerializer {
|
|
102
98
|
primaryKey = '_id'
|
|
103
99
|
}
|
|
104
100
|
```
|
|
105
|
-
|
|
106
|
-
@property primaryKey
|
|
101
|
+
@property primaryKey
|
|
107
102
|
@type {String}
|
|
108
103
|
@public
|
|
109
104
|
@default 'id'
|
|
110
105
|
*/
|
|
111
106
|
primaryKey: 'id',
|
|
112
|
-
|
|
113
107
|
/**
|
|
114
108
|
The `attrs` object can be used to declare a simple mapping between
|
|
115
109
|
property names on `Model` records and payload keys in the
|
|
116
110
|
serialized JSON object representing the record. An object with the
|
|
117
111
|
property `key` can also be used to designate the attribute's key on
|
|
118
112
|
the response payload.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
```app/models/person.js
|
|
113
|
+
Example
|
|
114
|
+
```app/models/person.js
|
|
123
115
|
import Model, { attr } from '@ember-data/model';
|
|
124
|
-
|
|
125
|
-
export default class PersonModel extends Model {
|
|
116
|
+
export default class PersonModel extends Model {
|
|
126
117
|
@attr('string') firstName;
|
|
127
118
|
@attr('string') lastName;
|
|
128
119
|
@attr('string') occupation;
|
|
129
120
|
@attr('boolean') admin;
|
|
130
121
|
}
|
|
131
122
|
```
|
|
132
|
-
|
|
133
|
-
```app/serializers/person.js
|
|
123
|
+
```app/serializers/person.js
|
|
134
124
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
135
|
-
|
|
136
|
-
export default class PersonSerializer extends JSONSerializer {
|
|
125
|
+
export default class PersonSerializer extends JSONSerializer {
|
|
137
126
|
attrs = {
|
|
138
127
|
admin: 'is_admin',
|
|
139
128
|
occupation: { key: 'career' }
|
|
140
129
|
}
|
|
141
130
|
}
|
|
142
131
|
```
|
|
143
|
-
|
|
144
|
-
You can also remove attributes and relationships by setting the `serialize`
|
|
132
|
+
You can also remove attributes and relationships by setting the `serialize`
|
|
145
133
|
key to `false` in your mapping object.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
```app/serializers/person.js
|
|
134
|
+
Example
|
|
135
|
+
```app/serializers/person.js
|
|
150
136
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
151
|
-
|
|
152
|
-
export default class PostSerializer extends JSONSerializer {
|
|
137
|
+
export default class PostSerializer extends JSONSerializer {
|
|
153
138
|
attrs = {
|
|
154
139
|
admin: { serialize: false },
|
|
155
140
|
occupation: { key: 'career' }
|
|
156
141
|
}
|
|
157
142
|
}
|
|
158
143
|
```
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
```javascript
|
|
144
|
+
When serialized:
|
|
145
|
+
```javascript
|
|
163
146
|
{
|
|
164
147
|
"firstName": "Harry",
|
|
165
148
|
"lastName": "Houdini",
|
|
166
149
|
"career": "magician"
|
|
167
150
|
}
|
|
168
151
|
```
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
Setting `serialize` to `true` enforces serialization for hasMany
|
|
152
|
+
Note that the `admin` is now not included in the payload.
|
|
153
|
+
Setting `serialize` to `true` enforces serialization for hasMany
|
|
173
154
|
relationships even if it's neither a many-to-many nor many-to-none
|
|
174
155
|
relationship.
|
|
175
|
-
|
|
176
|
-
@property attrs
|
|
156
|
+
@property attrs
|
|
177
157
|
@public
|
|
178
158
|
@type {Object}
|
|
179
159
|
*/
|
|
180
160
|
mergedProperties: ['attrs'],
|
|
181
|
-
|
|
182
161
|
/**
|
|
183
162
|
Given a subclass of `Model` and a JSON object this method will
|
|
184
163
|
iterate through each attribute of the `Model` and invoke the
|
|
185
164
|
`Transform#deserialize` method on the matching property of the
|
|
186
165
|
JSON object. This method is typically called after the
|
|
187
166
|
serializer's `normalize` method.
|
|
188
|
-
|
|
189
|
-
@method applyTransforms
|
|
167
|
+
@method applyTransforms
|
|
190
168
|
@private
|
|
191
169
|
@param {Model} typeClass
|
|
192
170
|
@param {Object} data The data to transform
|
|
@@ -194,47 +172,36 @@ const JSONSerializer = Serializer.extend({
|
|
|
194
172
|
*/
|
|
195
173
|
applyTransforms(typeClass, data) {
|
|
196
174
|
let attributes = typeClass.attributes;
|
|
197
|
-
|
|
198
175
|
typeClass.eachTransformedAttribute((key, typeClass) => {
|
|
199
176
|
if (data[key] === undefined) {
|
|
200
177
|
return;
|
|
201
178
|
}
|
|
202
|
-
|
|
203
179
|
let transform = this.transformFor(typeClass);
|
|
204
180
|
let transformMeta = attributes.get(key);
|
|
205
181
|
data[key] = transform.deserialize(data[key], transformMeta.options);
|
|
206
182
|
});
|
|
207
|
-
|
|
208
183
|
return data;
|
|
209
184
|
},
|
|
210
|
-
|
|
211
185
|
/**
|
|
212
186
|
The `normalizeResponse` method is used to normalize a payload from the
|
|
213
187
|
server to a JSON-API Document.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
This method delegates to a more specific normalize method based on
|
|
188
|
+
http://jsonapi.org/format/#document-structure
|
|
189
|
+
This method delegates to a more specific normalize method based on
|
|
218
190
|
the `requestType`.
|
|
219
|
-
|
|
220
|
-
To override this method with a custom one, make sure to call
|
|
191
|
+
To override this method with a custom one, make sure to call
|
|
221
192
|
`return super.normalizeResponse(store, primaryModelClass, payload, id, requestType)` with your
|
|
222
193
|
pre-processed data.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
```javascript
|
|
194
|
+
Here's an example of using `normalizeResponse` manually:
|
|
195
|
+
```javascript
|
|
227
196
|
socket.on('message', function(message) {
|
|
228
197
|
let data = message.data;
|
|
229
198
|
let modelClass = store.modelFor(data.modelName);
|
|
230
199
|
let serializer = store.serializerFor(data.modelName);
|
|
231
200
|
let normalized = serializer.normalizeSingleResponse(store, modelClass, data, data.id);
|
|
232
|
-
|
|
233
|
-
store.push(normalized);
|
|
201
|
+
store.push(normalized);
|
|
234
202
|
});
|
|
235
203
|
```
|
|
236
|
-
|
|
237
|
-
@since 1.13.0
|
|
204
|
+
@since 1.13.0
|
|
238
205
|
@method normalizeResponse
|
|
239
206
|
@public
|
|
240
207
|
@param {Store} store
|
|
@@ -268,12 +235,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
268
235
|
return this.normalizeUpdateRecordResponse(...arguments);
|
|
269
236
|
}
|
|
270
237
|
},
|
|
271
|
-
|
|
272
238
|
/**
|
|
273
239
|
Called by the default normalizeResponse implementation when the
|
|
274
240
|
type of request is `findRecord`
|
|
275
|
-
|
|
276
|
-
@since 1.13.0
|
|
241
|
+
@since 1.13.0
|
|
277
242
|
@method normalizeFindRecordResponse
|
|
278
243
|
@public
|
|
279
244
|
@param {Store} store
|
|
@@ -286,12 +251,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
286
251
|
normalizeFindRecordResponse(store, primaryModelClass, payload, id, requestType) {
|
|
287
252
|
return this.normalizeSingleResponse(...arguments);
|
|
288
253
|
},
|
|
289
|
-
|
|
290
254
|
/**
|
|
291
255
|
Called by the default normalizeResponse implementation when the
|
|
292
256
|
type of request is `queryRecord`
|
|
293
|
-
|
|
294
|
-
@since 1.13.0
|
|
257
|
+
@since 1.13.0
|
|
295
258
|
@method normalizeQueryRecordResponse
|
|
296
259
|
@public
|
|
297
260
|
@param {Store} store
|
|
@@ -304,12 +267,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
304
267
|
normalizeQueryRecordResponse(store, primaryModelClass, payload, id, requestType) {
|
|
305
268
|
return this.normalizeSingleResponse(...arguments);
|
|
306
269
|
},
|
|
307
|
-
|
|
308
270
|
/**
|
|
309
271
|
Called by the default normalizeResponse implementation when the
|
|
310
272
|
type of request is `findAll`
|
|
311
|
-
|
|
312
|
-
@since 1.13.0
|
|
273
|
+
@since 1.13.0
|
|
313
274
|
@method normalizeFindAllResponse
|
|
314
275
|
@public
|
|
315
276
|
@param {Store} store
|
|
@@ -322,12 +283,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
322
283
|
normalizeFindAllResponse(store, primaryModelClass, payload, id, requestType) {
|
|
323
284
|
return this.normalizeArrayResponse(...arguments);
|
|
324
285
|
},
|
|
325
|
-
|
|
326
286
|
/**
|
|
327
287
|
Called by the default normalizeResponse implementation when the
|
|
328
288
|
type of request is `findBelongsTo`
|
|
329
|
-
|
|
330
|
-
@since 1.13.0
|
|
289
|
+
@since 1.13.0
|
|
331
290
|
@method normalizeFindBelongsToResponse
|
|
332
291
|
@public
|
|
333
292
|
@param {Store} store
|
|
@@ -340,12 +299,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
340
299
|
normalizeFindBelongsToResponse(store, primaryModelClass, payload, id, requestType) {
|
|
341
300
|
return this.normalizeSingleResponse(...arguments);
|
|
342
301
|
},
|
|
343
|
-
|
|
344
302
|
/**
|
|
345
303
|
Called by the default normalizeResponse implementation when the
|
|
346
304
|
type of request is `findHasMany`
|
|
347
|
-
|
|
348
|
-
@since 1.13.0
|
|
305
|
+
@since 1.13.0
|
|
349
306
|
@method normalizeFindHasManyResponse
|
|
350
307
|
@public
|
|
351
308
|
@param {Store} store
|
|
@@ -358,12 +315,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
358
315
|
normalizeFindHasManyResponse(store, primaryModelClass, payload, id, requestType) {
|
|
359
316
|
return this.normalizeArrayResponse(...arguments);
|
|
360
317
|
},
|
|
361
|
-
|
|
362
318
|
/**
|
|
363
319
|
Called by the default normalizeResponse implementation when the
|
|
364
320
|
type of request is `findMany`
|
|
365
|
-
|
|
366
|
-
@since 1.13.0
|
|
321
|
+
@since 1.13.0
|
|
367
322
|
@method normalizeFindManyResponse
|
|
368
323
|
@public
|
|
369
324
|
@param {Store} store
|
|
@@ -376,12 +331,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
376
331
|
normalizeFindManyResponse(store, primaryModelClass, payload, id, requestType) {
|
|
377
332
|
return this.normalizeArrayResponse(...arguments);
|
|
378
333
|
},
|
|
379
|
-
|
|
380
334
|
/**
|
|
381
335
|
Called by the default normalizeResponse implementation when the
|
|
382
336
|
type of request is `query`
|
|
383
|
-
|
|
384
|
-
@since 1.13.0
|
|
337
|
+
@since 1.13.0
|
|
385
338
|
@method normalizeQueryResponse
|
|
386
339
|
@public
|
|
387
340
|
@param {Store} store
|
|
@@ -394,12 +347,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
394
347
|
normalizeQueryResponse(store, primaryModelClass, payload, id, requestType) {
|
|
395
348
|
return this.normalizeArrayResponse(...arguments);
|
|
396
349
|
},
|
|
397
|
-
|
|
398
350
|
/**
|
|
399
351
|
Called by the default normalizeResponse implementation when the
|
|
400
352
|
type of request is `createRecord`
|
|
401
|
-
|
|
402
|
-
@since 1.13.0
|
|
353
|
+
@since 1.13.0
|
|
403
354
|
@method normalizeCreateRecordResponse
|
|
404
355
|
@public
|
|
405
356
|
@param {Store} store
|
|
@@ -412,12 +363,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
412
363
|
normalizeCreateRecordResponse(store, primaryModelClass, payload, id, requestType) {
|
|
413
364
|
return this.normalizeSaveResponse(...arguments);
|
|
414
365
|
},
|
|
415
|
-
|
|
416
366
|
/**
|
|
417
367
|
Called by the default normalizeResponse implementation when the
|
|
418
368
|
type of request is `deleteRecord`
|
|
419
|
-
|
|
420
|
-
@since 1.13.0
|
|
369
|
+
@since 1.13.0
|
|
421
370
|
@method normalizeDeleteRecordResponse
|
|
422
371
|
@public
|
|
423
372
|
@param {Store} store
|
|
@@ -430,12 +379,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
430
379
|
normalizeDeleteRecordResponse(store, primaryModelClass, payload, id, requestType) {
|
|
431
380
|
return this.normalizeSaveResponse(...arguments);
|
|
432
381
|
},
|
|
433
|
-
|
|
434
382
|
/**
|
|
435
383
|
Called by the default normalizeResponse implementation when the
|
|
436
384
|
type of request is `updateRecord`
|
|
437
|
-
|
|
438
|
-
@since 1.13.0
|
|
385
|
+
@since 1.13.0
|
|
439
386
|
@method normalizeUpdateRecordResponse
|
|
440
387
|
@public
|
|
441
388
|
@param {Store} store
|
|
@@ -448,12 +395,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
448
395
|
normalizeUpdateRecordResponse(store, primaryModelClass, payload, id, requestType) {
|
|
449
396
|
return this.normalizeSaveResponse(...arguments);
|
|
450
397
|
},
|
|
451
|
-
|
|
452
398
|
/**
|
|
453
399
|
normalizeUpdateRecordResponse, normalizeCreateRecordResponse and
|
|
454
400
|
normalizeDeleteRecordResponse delegate to this method by default.
|
|
455
|
-
|
|
456
|
-
@since 1.13.0
|
|
401
|
+
@since 1.13.0
|
|
457
402
|
@method normalizeSaveResponse
|
|
458
403
|
@public
|
|
459
404
|
@param {Store} store
|
|
@@ -466,12 +411,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
466
411
|
normalizeSaveResponse(store, primaryModelClass, payload, id, requestType) {
|
|
467
412
|
return this.normalizeSingleResponse(...arguments);
|
|
468
413
|
},
|
|
469
|
-
|
|
470
414
|
/**
|
|
471
415
|
normalizeQueryResponse and normalizeFindRecordResponse delegate to this
|
|
472
416
|
method by default.
|
|
473
|
-
|
|
474
|
-
@since 1.13.0
|
|
417
|
+
@since 1.13.0
|
|
475
418
|
@method normalizeSingleResponse
|
|
476
419
|
@public
|
|
477
420
|
@param {Store} store
|
|
@@ -484,12 +427,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
484
427
|
normalizeSingleResponse(store, primaryModelClass, payload, id, requestType) {
|
|
485
428
|
return this._normalizeResponse(store, primaryModelClass, payload, id, requestType, true);
|
|
486
429
|
},
|
|
487
|
-
|
|
488
430
|
/**
|
|
489
431
|
normalizeQueryResponse, normalizeFindManyResponse, and normalizeFindHasManyResponse delegate
|
|
490
432
|
to this method by default.
|
|
491
|
-
|
|
492
|
-
@since 1.13.0
|
|
433
|
+
@since 1.13.0
|
|
493
434
|
@method normalizeArrayResponse
|
|
494
435
|
@public
|
|
495
436
|
@param {Store} store
|
|
@@ -502,7 +443,6 @@ const JSONSerializer = Serializer.extend({
|
|
|
502
443
|
normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
|
|
503
444
|
return this._normalizeResponse(store, primaryModelClass, payload, id, requestType, false);
|
|
504
445
|
},
|
|
505
|
-
|
|
506
446
|
/**
|
|
507
447
|
@method _normalizeResponse
|
|
508
448
|
@param {Store} store
|
|
@@ -517,20 +457,18 @@ const JSONSerializer = Serializer.extend({
|
|
|
517
457
|
_normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
|
|
518
458
|
let documentHash = {
|
|
519
459
|
data: null,
|
|
520
|
-
included: []
|
|
460
|
+
included: []
|
|
521
461
|
};
|
|
522
|
-
|
|
523
462
|
let meta = this.extractMeta(store, primaryModelClass, payload);
|
|
524
463
|
if (meta) {
|
|
525
|
-
assert(
|
|
526
|
-
'The `meta` returned from `extractMeta` has to be an object, not "' + typeOf(meta) + '".',
|
|
527
|
-
typeOf(meta) === 'object'
|
|
528
|
-
);
|
|
464
|
+
assert('The `meta` returned from `extractMeta` has to be an object, not "' + typeOf(meta) + '".', typeOf(meta) === 'object');
|
|
529
465
|
documentHash.meta = meta;
|
|
530
466
|
}
|
|
531
|
-
|
|
532
467
|
if (isSingle) {
|
|
533
|
-
let {
|
|
468
|
+
let {
|
|
469
|
+
data,
|
|
470
|
+
included
|
|
471
|
+
} = this.normalize(primaryModelClass, payload);
|
|
534
472
|
documentHash.data = data;
|
|
535
473
|
if (included) {
|
|
536
474
|
documentHash.included = included;
|
|
@@ -539,56 +477,47 @@ const JSONSerializer = Serializer.extend({
|
|
|
539
477
|
let ret = new Array(payload.length);
|
|
540
478
|
for (let i = 0, l = payload.length; i < l; i++) {
|
|
541
479
|
let item = payload[i];
|
|
542
|
-
let {
|
|
480
|
+
let {
|
|
481
|
+
data,
|
|
482
|
+
included
|
|
483
|
+
} = this.normalize(primaryModelClass, item);
|
|
543
484
|
if (included) {
|
|
544
485
|
documentHash.included = documentHash.included.concat(included);
|
|
545
486
|
}
|
|
546
487
|
ret[i] = data;
|
|
547
488
|
}
|
|
548
|
-
|
|
549
489
|
documentHash.data = ret;
|
|
550
490
|
}
|
|
551
|
-
|
|
552
491
|
return documentHash;
|
|
553
492
|
},
|
|
554
|
-
|
|
555
493
|
/**
|
|
556
494
|
Normalizes a part of the JSON payload returned by
|
|
557
495
|
the server. You should override this method, munge the hash
|
|
558
496
|
and call super if you have generic normalization to do.
|
|
559
|
-
|
|
560
|
-
It takes the type of the record that is being normalized
|
|
497
|
+
It takes the type of the record that is being normalized
|
|
561
498
|
(as a Model class), the property where the hash was
|
|
562
499
|
originally found, and the hash to normalize.
|
|
563
|
-
|
|
564
|
-
You can use this method, for example, to normalize underscored keys to camelized
|
|
500
|
+
You can use this method, for example, to normalize underscored keys to camelized
|
|
565
501
|
or other general-purpose normalizations.
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
```app/serializers/application.js
|
|
502
|
+
Example
|
|
503
|
+
```app/serializers/application.js
|
|
570
504
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
571
505
|
import { underscore } from '<app-name>/utils/string-utils';
|
|
572
506
|
import { get } from '@ember/object';
|
|
573
|
-
|
|
574
|
-
export default class ApplicationSerializer extends JSONSerializer {
|
|
507
|
+
export default class ApplicationSerializer extends JSONSerializer {
|
|
575
508
|
normalize(typeClass, hash) {
|
|
576
509
|
let fields = typeClass.fields;
|
|
577
|
-
|
|
578
|
-
fields.forEach(function(type, field) {
|
|
510
|
+
fields.forEach(function(type, field) {
|
|
579
511
|
let payloadField = underscore(field);
|
|
580
512
|
if (field === payloadField) { return; }
|
|
581
|
-
|
|
582
|
-
hash[field] = hash[payloadField];
|
|
513
|
+
hash[field] = hash[payloadField];
|
|
583
514
|
delete hash[payloadField];
|
|
584
515
|
});
|
|
585
|
-
|
|
586
|
-
return super.normalize(...arguments);
|
|
516
|
+
return super.normalize(...arguments);
|
|
587
517
|
}
|
|
588
518
|
}
|
|
589
519
|
```
|
|
590
|
-
|
|
591
|
-
@method normalize
|
|
520
|
+
@method normalize
|
|
592
521
|
@public
|
|
593
522
|
@param {Model} typeClass
|
|
594
523
|
@param {Object} hash
|
|
@@ -596,30 +525,26 @@ const JSONSerializer = Serializer.extend({
|
|
|
596
525
|
*/
|
|
597
526
|
normalize(modelClass, resourceHash) {
|
|
598
527
|
let data = null;
|
|
599
|
-
|
|
600
528
|
if (resourceHash) {
|
|
601
529
|
this.normalizeUsingDeclaredMapping(modelClass, resourceHash);
|
|
602
530
|
if (typeOf(resourceHash.links) === 'object') {
|
|
603
531
|
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.links);
|
|
604
532
|
}
|
|
605
|
-
|
|
606
533
|
data = {
|
|
607
534
|
id: this.extractId(modelClass, resourceHash),
|
|
608
535
|
type: modelClass.modelName,
|
|
609
536
|
attributes: this.extractAttributes(modelClass, resourceHash),
|
|
610
|
-
relationships: this.extractRelationships(modelClass, resourceHash)
|
|
537
|
+
relationships: this.extractRelationships(modelClass, resourceHash)
|
|
611
538
|
};
|
|
612
|
-
|
|
613
539
|
this.applyTransforms(modelClass, data.attributes);
|
|
614
540
|
}
|
|
615
|
-
|
|
616
|
-
|
|
541
|
+
return {
|
|
542
|
+
data
|
|
543
|
+
};
|
|
617
544
|
},
|
|
618
|
-
|
|
619
545
|
/**
|
|
620
546
|
Returns the resource's ID.
|
|
621
|
-
|
|
622
|
-
@method extractId
|
|
547
|
+
@method extractId
|
|
623
548
|
@public
|
|
624
549
|
@param {Object} modelClass
|
|
625
550
|
@param {Object} resourceHash
|
|
@@ -630,13 +555,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
630
555
|
let id = resourceHash[primaryKey];
|
|
631
556
|
return coerceId(id);
|
|
632
557
|
},
|
|
633
|
-
|
|
634
558
|
/**
|
|
635
559
|
Returns the resource's attributes formatted as a JSON-API "attributes object".
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
@method extractAttributes
|
|
560
|
+
http://jsonapi.org/format/#document-resource-object-attributes
|
|
561
|
+
@method extractAttributes
|
|
640
562
|
@public
|
|
641
563
|
@param {Object} modelClass
|
|
642
564
|
@param {Object} resourceHash
|
|
@@ -645,23 +567,18 @@ const JSONSerializer = Serializer.extend({
|
|
|
645
567
|
extractAttributes(modelClass, resourceHash) {
|
|
646
568
|
let attributeKey;
|
|
647
569
|
let attributes = {};
|
|
648
|
-
|
|
649
|
-
modelClass.eachAttribute((key) => {
|
|
570
|
+
modelClass.eachAttribute(key => {
|
|
650
571
|
attributeKey = this.keyForAttribute(key, 'deserialize');
|
|
651
572
|
if (resourceHash[attributeKey] !== undefined) {
|
|
652
573
|
attributes[key] = resourceHash[attributeKey];
|
|
653
574
|
}
|
|
654
575
|
});
|
|
655
|
-
|
|
656
576
|
return attributes;
|
|
657
577
|
},
|
|
658
|
-
|
|
659
578
|
/**
|
|
660
579
|
Returns a relationship formatted as a JSON-API "relationship object".
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
@method extractRelationship
|
|
580
|
+
http://jsonapi.org/format/#document-resource-object-relationships
|
|
581
|
+
@method extractRelationship
|
|
665
582
|
@public
|
|
666
583
|
@param {Object} relationshipModelName
|
|
667
584
|
@param {Object} relationshipHash
|
|
@@ -680,31 +597,28 @@ const JSONSerializer = Serializer.extend({
|
|
|
680
597
|
if (relationshipHash.id) {
|
|
681
598
|
relationshipHash.id = coerceId(relationshipHash.id);
|
|
682
599
|
}
|
|
683
|
-
|
|
684
600
|
let modelClass = this.store.modelFor(relationshipModelName);
|
|
685
601
|
if (relationshipHash.type && !modelClass.fields.has('type')) {
|
|
686
602
|
relationshipHash.type = this.modelNameFromPayloadKey(relationshipHash.type);
|
|
687
603
|
}
|
|
688
|
-
|
|
689
604
|
return relationshipHash;
|
|
690
605
|
}
|
|
691
|
-
return {
|
|
606
|
+
return {
|
|
607
|
+
id: coerceId(relationshipHash),
|
|
608
|
+
type: relationshipModelName
|
|
609
|
+
};
|
|
692
610
|
},
|
|
693
|
-
|
|
694
611
|
/**
|
|
695
612
|
Returns a polymorphic relationship formatted as a JSON-API "relationship object".
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
`relationshipOptions` is a hash which contains more information about the
|
|
613
|
+
http://jsonapi.org/format/#document-resource-object-relationships
|
|
614
|
+
`relationshipOptions` is a hash which contains more information about the
|
|
700
615
|
polymorphic relationship which should be extracted:
|
|
701
616
|
- `resourceHash` complete hash of the resource the relationship should be
|
|
702
617
|
extracted from
|
|
703
618
|
- `relationshipKey` key under which the value for the relationship is
|
|
704
619
|
extracted from the resourceHash
|
|
705
620
|
- `relationshipMeta` meta information about the relationship
|
|
706
|
-
|
|
707
|
-
@method extractPolymorphicRelationship
|
|
621
|
+
@method extractPolymorphicRelationship
|
|
708
622
|
@public
|
|
709
623
|
@param {Object} relationshipModelName
|
|
710
624
|
@param {Object} relationshipHash
|
|
@@ -714,13 +628,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
714
628
|
extractPolymorphicRelationship(relationshipModelName, relationshipHash, relationshipOptions) {
|
|
715
629
|
return this.extractRelationship(relationshipModelName, relationshipHash);
|
|
716
630
|
},
|
|
717
|
-
|
|
718
631
|
/**
|
|
719
632
|
Returns the resource's relationships formatted as a JSON-API "relationships object".
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
@method extractRelationships
|
|
633
|
+
http://jsonapi.org/format/#document-resource-object-relationships
|
|
634
|
+
@method extractRelationships
|
|
724
635
|
@public
|
|
725
636
|
@param {Object} modelClass
|
|
726
637
|
@param {Object} resourceHash
|
|
@@ -728,7 +639,6 @@ const JSONSerializer = Serializer.extend({
|
|
|
728
639
|
*/
|
|
729
640
|
extractRelationships(modelClass, resourceHash) {
|
|
730
641
|
let relationships = {};
|
|
731
|
-
|
|
732
642
|
modelClass.eachRelationship((key, relationshipMeta) => {
|
|
733
643
|
let relationship = null;
|
|
734
644
|
let relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
|
|
@@ -744,7 +654,7 @@ const JSONSerializer = Serializer.extend({
|
|
|
744
654
|
data = this.extractPolymorphicRelationship(relationshipMeta.type, relationshipHash, {
|
|
745
655
|
key,
|
|
746
656
|
resourceHash,
|
|
747
|
-
relationshipMeta
|
|
657
|
+
relationshipMeta
|
|
748
658
|
});
|
|
749
659
|
} else {
|
|
750
660
|
data = this.extractRelationship(relationshipMeta.type, relationshipHash);
|
|
@@ -758,7 +668,7 @@ const JSONSerializer = Serializer.extend({
|
|
|
758
668
|
data[i] = this.extractPolymorphicRelationship(relationshipMeta.type, item, {
|
|
759
669
|
key,
|
|
760
670
|
resourceHash,
|
|
761
|
-
relationshipMeta
|
|
671
|
+
relationshipMeta
|
|
762
672
|
});
|
|
763
673
|
}
|
|
764
674
|
} else {
|
|
@@ -769,28 +679,27 @@ const JSONSerializer = Serializer.extend({
|
|
|
769
679
|
}
|
|
770
680
|
}
|
|
771
681
|
}
|
|
772
|
-
relationship = {
|
|
682
|
+
relationship = {
|
|
683
|
+
data
|
|
684
|
+
};
|
|
773
685
|
}
|
|
774
|
-
|
|
775
686
|
let linkKey = this.keyForLink(key, relationshipMeta.kind);
|
|
776
687
|
if (resourceHash.links && resourceHash.links[linkKey] !== undefined) {
|
|
777
688
|
let related = resourceHash.links[linkKey];
|
|
778
689
|
relationship = relationship || {};
|
|
779
|
-
relationship.links = {
|
|
690
|
+
relationship.links = {
|
|
691
|
+
related
|
|
692
|
+
};
|
|
780
693
|
}
|
|
781
|
-
|
|
782
694
|
if (relationship) {
|
|
783
695
|
relationships[key] = relationship;
|
|
784
696
|
}
|
|
785
697
|
});
|
|
786
|
-
|
|
787
698
|
return relationships;
|
|
788
699
|
},
|
|
789
|
-
|
|
790
700
|
/**
|
|
791
701
|
Dasherizes the model name in the payload
|
|
792
|
-
|
|
793
|
-
@method modelNameFromPayloadKey
|
|
702
|
+
@method modelNameFromPayloadKey
|
|
794
703
|
@public
|
|
795
704
|
@param {String} key
|
|
796
705
|
@return {String} the model's modelName
|
|
@@ -798,14 +707,12 @@ const JSONSerializer = Serializer.extend({
|
|
|
798
707
|
modelNameFromPayloadKey(key) {
|
|
799
708
|
return dasherize(key);
|
|
800
709
|
},
|
|
801
|
-
|
|
802
710
|
/**
|
|
803
711
|
@method normalizeRelationships
|
|
804
712
|
@private
|
|
805
713
|
*/
|
|
806
714
|
normalizeRelationships(typeClass, hash) {
|
|
807
715
|
let payloadKey;
|
|
808
|
-
|
|
809
716
|
if (this.keyForRelationship) {
|
|
810
717
|
typeClass.eachRelationship((key, relationship) => {
|
|
811
718
|
payloadKey = this.keyForRelationship(key, relationship.kind, 'deserialize');
|
|
@@ -815,13 +722,11 @@ const JSONSerializer = Serializer.extend({
|
|
|
815
722
|
if (hash[payloadKey] === undefined) {
|
|
816
723
|
return;
|
|
817
724
|
}
|
|
818
|
-
|
|
819
725
|
hash[key] = hash[payloadKey];
|
|
820
726
|
delete hash[payloadKey];
|
|
821
727
|
});
|
|
822
728
|
}
|
|
823
729
|
},
|
|
824
|
-
|
|
825
730
|
/**
|
|
826
731
|
@method normalizeUsingDeclaredMapping
|
|
827
732
|
@private
|
|
@@ -830,23 +735,18 @@ const JSONSerializer = Serializer.extend({
|
|
|
830
735
|
let attrs = this.attrs;
|
|
831
736
|
let normalizedKey;
|
|
832
737
|
let payloadKey;
|
|
833
|
-
|
|
834
738
|
if (attrs) {
|
|
835
739
|
for (let key in attrs) {
|
|
836
740
|
normalizedKey = payloadKey = this._getMappedKey(key, modelClass);
|
|
837
|
-
|
|
838
741
|
if (hash[payloadKey] === undefined) {
|
|
839
742
|
continue;
|
|
840
743
|
}
|
|
841
|
-
|
|
842
744
|
if (modelClass.attributes.has(key)) {
|
|
843
745
|
normalizedKey = this.keyForAttribute(key, 'deserialize');
|
|
844
746
|
}
|
|
845
|
-
|
|
846
747
|
if (modelClass.relationshipsByName.has(key)) {
|
|
847
748
|
normalizedKey = this.keyForRelationship(key, modelClass, 'deserialize');
|
|
848
749
|
}
|
|
849
|
-
|
|
850
750
|
if (payloadKey !== normalizedKey) {
|
|
851
751
|
hash[normalizedKey] = hash[payloadKey];
|
|
852
752
|
delete hash[payloadKey];
|
|
@@ -854,29 +754,18 @@ const JSONSerializer = Serializer.extend({
|
|
|
854
754
|
}
|
|
855
755
|
}
|
|
856
756
|
},
|
|
857
|
-
|
|
858
757
|
/**
|
|
859
758
|
Looks up the property key that was set by the custom `attr` mapping
|
|
860
759
|
passed to the serializer.
|
|
861
|
-
|
|
862
|
-
@method _getMappedKey
|
|
760
|
+
@method _getMappedKey
|
|
863
761
|
@private
|
|
864
762
|
@param {String} key
|
|
865
763
|
@return {String} key
|
|
866
764
|
*/
|
|
867
765
|
_getMappedKey(key, modelClass) {
|
|
868
|
-
warn(
|
|
869
|
-
'
|
|
870
|
-
|
|
871
|
-
'` on `' +
|
|
872
|
-
modelClass.modelName +
|
|
873
|
-
'`. Check your serializers attrs hash.',
|
|
874
|
-
modelClass.attributes.has(key) || modelClass.relationshipsByName.has(key),
|
|
875
|
-
{
|
|
876
|
-
id: 'ds.serializer.no-mapped-attrs-key',
|
|
877
|
-
}
|
|
878
|
-
);
|
|
879
|
-
|
|
766
|
+
warn('There is no attribute or relationship with the name `' + key + '` on `' + modelClass.modelName + '`. Check your serializers attrs hash.', modelClass.attributes.has(key) || modelClass.relationshipsByName.has(key), {
|
|
767
|
+
id: 'ds.serializer.no-mapped-attrs-key'
|
|
768
|
+
});
|
|
880
769
|
let attrs = this.attrs;
|
|
881
770
|
let mappedKey;
|
|
882
771
|
if (attrs && attrs[key]) {
|
|
@@ -890,48 +779,38 @@ const JSONSerializer = Serializer.extend({
|
|
|
890
779
|
key = mappedKey;
|
|
891
780
|
}
|
|
892
781
|
}
|
|
893
|
-
|
|
894
782
|
return key;
|
|
895
783
|
},
|
|
896
|
-
|
|
897
784
|
/**
|
|
898
785
|
Check attrs.key.serialize property to inform if the `key`
|
|
899
786
|
can be serialized
|
|
900
|
-
|
|
901
|
-
@method _canSerialize
|
|
787
|
+
@method _canSerialize
|
|
902
788
|
@private
|
|
903
789
|
@param {String} key
|
|
904
790
|
@return {boolean} true if the key can be serialized
|
|
905
791
|
*/
|
|
906
792
|
_canSerialize(key) {
|
|
907
793
|
let attrs = this.attrs;
|
|
908
|
-
|
|
909
794
|
return !attrs || !attrs[key] || attrs[key].serialize !== false;
|
|
910
795
|
},
|
|
911
|
-
|
|
912
796
|
/**
|
|
913
797
|
When attrs.key.serialize is set to true then
|
|
914
798
|
it takes priority over the other checks and the related
|
|
915
799
|
attribute/relationship will be serialized
|
|
916
|
-
|
|
917
|
-
@method _mustSerialize
|
|
800
|
+
@method _mustSerialize
|
|
918
801
|
@private
|
|
919
802
|
@param {String} key
|
|
920
803
|
@return {boolean} true if the key must be serialized
|
|
921
804
|
*/
|
|
922
805
|
_mustSerialize(key) {
|
|
923
806
|
let attrs = this.attrs;
|
|
924
|
-
|
|
925
807
|
return attrs && attrs[key] && attrs[key].serialize === true;
|
|
926
808
|
},
|
|
927
|
-
|
|
928
809
|
/**
|
|
929
810
|
Check if the given hasMany relationship should be serialized
|
|
930
|
-
|
|
931
|
-
By default only many-to-many and many-to-none relationships are serialized.
|
|
811
|
+
By default only many-to-many and many-to-none relationships are serialized.
|
|
932
812
|
This could be configured per relationship by Serializer's `attrs` object.
|
|
933
|
-
|
|
934
|
-
@method shouldSerializeHasMany
|
|
813
|
+
@method shouldSerializeHasMany
|
|
935
814
|
@public
|
|
936
815
|
@param {Snapshot} snapshot
|
|
937
816
|
@param {String} key
|
|
@@ -946,156 +825,117 @@ const JSONSerializer = Serializer.extend({
|
|
|
946
825
|
}
|
|
947
826
|
return this._canSerialize(key) && (relationshipType === 'manyToNone' || relationshipType === 'manyToMany');
|
|
948
827
|
},
|
|
949
|
-
|
|
950
828
|
// SERIALIZE
|
|
951
829
|
/**
|
|
952
830
|
Called when a record is saved in order to convert the
|
|
953
831
|
record into JSON.
|
|
954
|
-
|
|
955
|
-
By default, it creates a JSON object with a key for
|
|
832
|
+
By default, it creates a JSON object with a key for
|
|
956
833
|
each attribute and belongsTo relationship.
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
```app/models/comment.js
|
|
834
|
+
For example, consider this model:
|
|
835
|
+
```app/models/comment.js
|
|
961
836
|
import Model, { attr, belongsTo } from '@ember-data/model';
|
|
962
|
-
|
|
963
|
-
export default class CommentModel extends Model {
|
|
837
|
+
export default class CommentModel extends Model {
|
|
964
838
|
@attr title;
|
|
965
839
|
@attr body;
|
|
966
|
-
|
|
967
|
-
@belongsTo('user') author;
|
|
840
|
+
@belongsTo('user') author;
|
|
968
841
|
}
|
|
969
842
|
```
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
```javascript
|
|
843
|
+
The default serialization would create a JSON object like:
|
|
844
|
+
```javascript
|
|
974
845
|
{
|
|
975
846
|
"title": "Rails is unagi",
|
|
976
847
|
"body": "Rails? Omakase? O_O",
|
|
977
848
|
"author": 12
|
|
978
849
|
}
|
|
979
850
|
```
|
|
980
|
-
|
|
981
|
-
By default, attributes are passed through as-is, unless
|
|
851
|
+
By default, attributes are passed through as-is, unless
|
|
982
852
|
you specified an attribute type (`attr('date')`). If
|
|
983
853
|
you specify a transform, the JavaScript value will be
|
|
984
854
|
serialized when inserted into the JSON hash.
|
|
985
|
-
|
|
986
|
-
By default, belongs-to relationships are converted into
|
|
855
|
+
By default, belongs-to relationships are converted into
|
|
987
856
|
IDs when inserted into the JSON hash.
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
`serialize` takes an options hash with a single option:
|
|
857
|
+
## IDs
|
|
858
|
+
`serialize` takes an options hash with a single option:
|
|
992
859
|
`includeId`. If this option is `true`, `serialize` will,
|
|
993
860
|
by default include the ID in the JSON object it builds.
|
|
994
|
-
|
|
995
|
-
The adapter passes in `includeId: true` when serializing
|
|
861
|
+
The adapter passes in `includeId: true` when serializing
|
|
996
862
|
a record for `createRecord`, but not for `updateRecord`.
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
Your server may expect a different JSON format than the
|
|
863
|
+
## Customization
|
|
864
|
+
Your server may expect a different JSON format than the
|
|
1001
865
|
built-in serialization format.
|
|
1002
|
-
|
|
1003
|
-
In that case, you can implement `serialize` yourself and
|
|
866
|
+
In that case, you can implement `serialize` yourself and
|
|
1004
867
|
return a JSON hash of your choosing.
|
|
1005
|
-
|
|
1006
|
-
```app/serializers/post.js
|
|
868
|
+
```app/serializers/post.js
|
|
1007
869
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1008
|
-
|
|
1009
|
-
export default class PostSerializer extends JSONSerializer {
|
|
870
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1010
871
|
serialize(snapshot, options) {
|
|
1011
872
|
let json = {
|
|
1012
873
|
POST_TTL: snapshot.attr('title'),
|
|
1013
874
|
POST_BDY: snapshot.attr('body'),
|
|
1014
875
|
POST_CMS: snapshot.hasMany('comments', { ids: true })
|
|
1015
876
|
};
|
|
1016
|
-
|
|
1017
|
-
if (options.includeId) {
|
|
877
|
+
if (options.includeId) {
|
|
1018
878
|
json.POST_ID_ = snapshot.id;
|
|
1019
879
|
}
|
|
1020
|
-
|
|
1021
|
-
return json;
|
|
880
|
+
return json;
|
|
1022
881
|
}
|
|
1023
882
|
}
|
|
1024
883
|
```
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
If you want to define a serializer for your entire
|
|
884
|
+
## Customizing an App-Wide Serializer
|
|
885
|
+
If you want to define a serializer for your entire
|
|
1029
886
|
application, you'll probably want to use `eachAttribute`
|
|
1030
887
|
and `eachRelationship` on the record.
|
|
1031
|
-
|
|
1032
|
-
```app/serializers/application.js
|
|
888
|
+
```app/serializers/application.js
|
|
1033
889
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1034
890
|
import { singularize } from '<app-name>/utils/string-utils';
|
|
1035
|
-
|
|
1036
|
-
export default class ApplicationSerializer extends JSONSerializer {
|
|
891
|
+
export default class ApplicationSerializer extends JSONSerializer {
|
|
1037
892
|
serialize(snapshot, options) {
|
|
1038
893
|
let json = {};
|
|
1039
|
-
|
|
1040
|
-
snapshot.eachAttribute((name) => {
|
|
894
|
+
snapshot.eachAttribute((name) => {
|
|
1041
895
|
json[serverAttributeName(name)] = snapshot.attr(name);
|
|
1042
896
|
});
|
|
1043
|
-
|
|
1044
|
-
snapshot.eachRelationship((name, relationship) => {
|
|
897
|
+
snapshot.eachRelationship((name, relationship) => {
|
|
1045
898
|
if (relationship.kind === 'hasMany') {
|
|
1046
899
|
json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
|
|
1047
900
|
}
|
|
1048
901
|
});
|
|
1049
|
-
|
|
1050
|
-
if (options.includeId) {
|
|
902
|
+
if (options.includeId) {
|
|
1051
903
|
json.ID_ = snapshot.id;
|
|
1052
904
|
}
|
|
1053
|
-
|
|
1054
|
-
return json;
|
|
905
|
+
return json;
|
|
1055
906
|
}
|
|
1056
907
|
}
|
|
1057
|
-
|
|
1058
|
-
function serverAttributeName(attribute) {
|
|
908
|
+
function serverAttributeName(attribute) {
|
|
1059
909
|
return attribute.underscore().toUpperCase();
|
|
1060
910
|
}
|
|
1061
|
-
|
|
1062
|
-
function serverHasManyName(name) {
|
|
911
|
+
function serverHasManyName(name) {
|
|
1063
912
|
return serverAttributeName(singularize(name)) + "_IDS";
|
|
1064
913
|
}
|
|
1065
914
|
```
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
```javascript
|
|
915
|
+
This serializer will generate JSON that looks like this:
|
|
916
|
+
```javascript
|
|
1070
917
|
{
|
|
1071
918
|
"TITLE": "Rails is omakase",
|
|
1072
919
|
"BODY": "Yep. Omakase.",
|
|
1073
920
|
"COMMENT_IDS": [ "1", "2", "3" ]
|
|
1074
921
|
}
|
|
1075
922
|
```
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
If you just want to do some small tweaks on the default JSON,
|
|
923
|
+
## Tweaking the Default JSON
|
|
924
|
+
If you just want to do some small tweaks on the default JSON,
|
|
1080
925
|
you can call `super.serialize` first and make the tweaks on
|
|
1081
926
|
the returned JSON.
|
|
1082
|
-
|
|
1083
|
-
```app/serializers/post.js
|
|
927
|
+
```app/serializers/post.js
|
|
1084
928
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1085
|
-
|
|
1086
|
-
export default class PostSerializer extends JSONSerializer {
|
|
929
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1087
930
|
serialize(snapshot, options) {
|
|
1088
931
|
let json = super.serialize(...arguments);
|
|
1089
|
-
|
|
1090
|
-
json.subject = json.title;
|
|
932
|
+
json.subject = json.title;
|
|
1091
933
|
delete json.title;
|
|
1092
|
-
|
|
1093
|
-
return json;
|
|
934
|
+
return json;
|
|
1094
935
|
}
|
|
1095
936
|
}
|
|
1096
937
|
```
|
|
1097
|
-
|
|
1098
|
-
@method serialize
|
|
938
|
+
@method serialize
|
|
1099
939
|
@public
|
|
1100
940
|
@param {Snapshot} snapshot
|
|
1101
941
|
@param {Object} options
|
|
@@ -1103,18 +943,15 @@ const JSONSerializer = Serializer.extend({
|
|
|
1103
943
|
*/
|
|
1104
944
|
serialize(snapshot, options) {
|
|
1105
945
|
let json = {};
|
|
1106
|
-
|
|
1107
946
|
if (options && options.includeId) {
|
|
1108
947
|
const id = snapshot.id;
|
|
1109
948
|
if (id) {
|
|
1110
949
|
json[this.primaryKey] = id;
|
|
1111
950
|
}
|
|
1112
951
|
}
|
|
1113
|
-
|
|
1114
952
|
snapshot.eachAttribute((key, attribute) => {
|
|
1115
953
|
this.serializeAttribute(snapshot, json, key, attribute);
|
|
1116
954
|
});
|
|
1117
|
-
|
|
1118
955
|
snapshot.eachRelationship((key, relationship) => {
|
|
1119
956
|
if (relationship.kind === 'belongsTo') {
|
|
1120
957
|
this.serializeBelongsTo(snapshot, json, relationship);
|
|
@@ -1122,10 +959,8 @@ const JSONSerializer = Serializer.extend({
|
|
|
1122
959
|
this.serializeHasMany(snapshot, json, relationship);
|
|
1123
960
|
}
|
|
1124
961
|
});
|
|
1125
|
-
|
|
1126
962
|
return json;
|
|
1127
963
|
},
|
|
1128
|
-
|
|
1129
964
|
/**
|
|
1130
965
|
You can use this method to customize how a serialized record is added to the complete
|
|
1131
966
|
JSON hash to be sent to the server. By default the JSON Serializer does not namespace
|
|
@@ -1133,22 +968,18 @@ const JSONSerializer = Serializer.extend({
|
|
|
1133
968
|
If your server expects namespaced keys, you should consider using the RESTSerializer.
|
|
1134
969
|
Otherwise you can override this method to customize how the record is added to the hash.
|
|
1135
970
|
The hash property should be modified by reference.
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
```app/serializers/application.js
|
|
971
|
+
For example, your server may expect underscored root objects.
|
|
972
|
+
```app/serializers/application.js
|
|
1140
973
|
import RESTSerializer from '@ember-data/serializer/rest';
|
|
1141
974
|
import { decamelize } from '<app-name>/utils/string-utils';
|
|
1142
|
-
|
|
1143
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
975
|
+
export default class ApplicationSerializer extends RESTSerializer {
|
|
1144
976
|
serializeIntoHash(data, type, snapshot, options) {
|
|
1145
977
|
let root = decamelize(type.modelName);
|
|
1146
978
|
data[root] = this.serialize(snapshot, options);
|
|
1147
979
|
}
|
|
1148
980
|
}
|
|
1149
981
|
```
|
|
1150
|
-
|
|
1151
|
-
@method serializeIntoHash
|
|
982
|
+
@method serializeIntoHash
|
|
1152
983
|
@public
|
|
1153
984
|
@param {Object} hash
|
|
1154
985
|
@param {Model} typeClass
|
|
@@ -1158,27 +989,22 @@ const JSONSerializer = Serializer.extend({
|
|
|
1158
989
|
serializeIntoHash(hash, typeClass, snapshot, options) {
|
|
1159
990
|
Object.assign(hash, this.serialize(snapshot, options));
|
|
1160
991
|
},
|
|
1161
|
-
|
|
1162
992
|
/**
|
|
1163
993
|
`serializeAttribute` can be used to customize how `attr`
|
|
1164
994
|
properties are serialized
|
|
1165
|
-
|
|
1166
|
-
For example if you wanted to ensure all your attributes were always
|
|
995
|
+
For example if you wanted to ensure all your attributes were always
|
|
1167
996
|
serialized as properties on an `attributes` object you could
|
|
1168
997
|
write:
|
|
1169
|
-
|
|
1170
|
-
```app/serializers/application.js
|
|
998
|
+
```app/serializers/application.js
|
|
1171
999
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1172
|
-
|
|
1173
|
-
export default class ApplicationSerializer extends JSONSerializer {
|
|
1000
|
+
export default class ApplicationSerializer extends JSONSerializer {
|
|
1174
1001
|
serializeAttribute(snapshot, json, key, attributes) {
|
|
1175
1002
|
json.attributes = json.attributes || {};
|
|
1176
1003
|
super.serializeAttribute(snapshot, json.attributes, key, attributes);
|
|
1177
1004
|
}
|
|
1178
1005
|
}
|
|
1179
1006
|
```
|
|
1180
|
-
|
|
1181
|
-
@method serializeAttribute
|
|
1007
|
+
@method serializeAttribute
|
|
1182
1008
|
@public
|
|
1183
1009
|
@param {Snapshot} snapshot
|
|
1184
1010
|
@param {Object} json
|
|
@@ -1198,38 +1024,29 @@ const JSONSerializer = Serializer.extend({
|
|
|
1198
1024
|
// the serializer
|
|
1199
1025
|
let schema = this.store.modelFor(snapshot.modelName);
|
|
1200
1026
|
let payloadKey = this._getMappedKey(key, schema);
|
|
1201
|
-
|
|
1202
1027
|
if (payloadKey === key && this.keyForAttribute) {
|
|
1203
1028
|
payloadKey = this.keyForAttribute(key, 'serialize');
|
|
1204
1029
|
}
|
|
1205
|
-
|
|
1206
1030
|
json[payloadKey] = value;
|
|
1207
1031
|
}
|
|
1208
1032
|
},
|
|
1209
|
-
|
|
1210
1033
|
/**
|
|
1211
1034
|
`serializeBelongsTo` can be used to customize how `belongsTo`
|
|
1212
1035
|
properties are serialized.
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
```app/serializers/post.js
|
|
1036
|
+
Example
|
|
1037
|
+
```app/serializers/post.js
|
|
1217
1038
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1218
1039
|
import { isNone } from '@ember/utils';
|
|
1219
|
-
|
|
1220
|
-
export default class PostSerializer extends JSONSerializer {
|
|
1040
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1221
1041
|
serializeBelongsTo(snapshot, json, relationship) {
|
|
1222
1042
|
let key = relationship.key;
|
|
1223
1043
|
let belongsTo = snapshot.belongsTo(key);
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
json[key] = isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON();
|
|
1044
|
+
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key;
|
|
1045
|
+
json[key] = isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON();
|
|
1228
1046
|
}
|
|
1229
1047
|
}
|
|
1230
1048
|
```
|
|
1231
|
-
|
|
1232
|
-
@method serializeBelongsTo
|
|
1049
|
+
@method serializeBelongsTo
|
|
1233
1050
|
@public
|
|
1234
1051
|
@param {Snapshot} snapshot
|
|
1235
1052
|
@param {Object} json
|
|
@@ -1237,9 +1054,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
1237
1054
|
*/
|
|
1238
1055
|
serializeBelongsTo(snapshot, json, relationship) {
|
|
1239
1056
|
let key = relationship.key;
|
|
1240
|
-
|
|
1241
1057
|
if (this._canSerialize(key)) {
|
|
1242
|
-
let belongsToId = snapshot.belongsTo(key, {
|
|
1058
|
+
let belongsToId = snapshot.belongsTo(key, {
|
|
1059
|
+
id: true
|
|
1060
|
+
});
|
|
1243
1061
|
|
|
1244
1062
|
// if provided, use the mapping provided by `attrs` in
|
|
1245
1063
|
// the serializer
|
|
@@ -1255,23 +1073,18 @@ const JSONSerializer = Serializer.extend({
|
|
|
1255
1073
|
} else {
|
|
1256
1074
|
json[payloadKey] = belongsToId;
|
|
1257
1075
|
}
|
|
1258
|
-
|
|
1259
1076
|
if (relationship.options.polymorphic) {
|
|
1260
1077
|
this.serializePolymorphicType(snapshot, json, relationship);
|
|
1261
1078
|
}
|
|
1262
1079
|
}
|
|
1263
1080
|
},
|
|
1264
|
-
|
|
1265
1081
|
/**
|
|
1266
1082
|
`serializeHasMany` can be used to customize how `hasMany`
|
|
1267
1083
|
properties are serialized.
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
```app/serializers/post.js
|
|
1084
|
+
Example
|
|
1085
|
+
```app/serializers/post.js
|
|
1272
1086
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1273
|
-
|
|
1274
|
-
export default class PostSerializer extends JSONSerializer {
|
|
1087
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1275
1088
|
serializeHasMany(snapshot, json, relationship) {
|
|
1276
1089
|
let key = relationship.key;
|
|
1277
1090
|
if (key === 'comments') {
|
|
@@ -1282,8 +1095,7 @@ const JSONSerializer = Serializer.extend({
|
|
|
1282
1095
|
}
|
|
1283
1096
|
}
|
|
1284
1097
|
```
|
|
1285
|
-
|
|
1286
|
-
@method serializeHasMany
|
|
1098
|
+
@method serializeHasMany
|
|
1287
1099
|
@public
|
|
1288
1100
|
@param {Snapshot} snapshot
|
|
1289
1101
|
@param {Object} json
|
|
@@ -1291,9 +1103,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
1291
1103
|
*/
|
|
1292
1104
|
serializeHasMany(snapshot, json, relationship) {
|
|
1293
1105
|
let key = relationship.key;
|
|
1294
|
-
|
|
1295
1106
|
if (this.shouldSerializeHasMany(snapshot, key, relationship)) {
|
|
1296
|
-
let hasMany = snapshot.hasMany(key, {
|
|
1107
|
+
let hasMany = snapshot.hasMany(key, {
|
|
1108
|
+
ids: true
|
|
1109
|
+
});
|
|
1297
1110
|
if (hasMany !== undefined) {
|
|
1298
1111
|
// if provided, use the mapping provided by `attrs` in
|
|
1299
1112
|
// the serializer
|
|
@@ -1302,7 +1115,6 @@ const JSONSerializer = Serializer.extend({
|
|
|
1302
1115
|
if (payloadKey === key && this.keyForRelationship) {
|
|
1303
1116
|
payloadKey = this.keyForRelationship(key, 'hasMany', 'serialize');
|
|
1304
1117
|
}
|
|
1305
|
-
|
|
1306
1118
|
json[payloadKey] = hasMany;
|
|
1307
1119
|
// TODO support for polymorphic manyToNone and manyToMany relationships
|
|
1308
1120
|
}
|
|
@@ -1314,21 +1126,16 @@ const JSONSerializer = Serializer.extend({
|
|
|
1314
1126
|
serialized. Objects are considered to be polymorphic if
|
|
1315
1127
|
`{ polymorphic: true }` is pass as the second argument to the
|
|
1316
1128
|
`belongsTo` function.
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
```app/serializers/comment.js
|
|
1129
|
+
Example
|
|
1130
|
+
```app/serializers/comment.js
|
|
1321
1131
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1322
1132
|
import { isNone } from '@ember/utils';
|
|
1323
|
-
|
|
1324
|
-
export default class CommentSerializer extends JSONSerializer {
|
|
1133
|
+
export default class CommentSerializer extends JSONSerializer {
|
|
1325
1134
|
serializePolymorphicType(snapshot, json, relationship) {
|
|
1326
1135
|
let key = relationship.key;
|
|
1327
1136
|
let belongsTo = snapshot.belongsTo(key);
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
if (isNone(belongsTo)) {
|
|
1137
|
+
key = this.keyForAttribute ? this.keyForAttribute(key, 'serialize') : key;
|
|
1138
|
+
if (isNone(belongsTo)) {
|
|
1332
1139
|
json[key + '_type'] = null;
|
|
1333
1140
|
} else {
|
|
1334
1141
|
json[key + '_type'] = belongsTo.modelName;
|
|
@@ -1336,26 +1143,21 @@ const JSONSerializer = Serializer.extend({
|
|
|
1336
1143
|
}
|
|
1337
1144
|
}
|
|
1338
1145
|
```
|
|
1339
|
-
|
|
1340
|
-
@method serializePolymorphicType
|
|
1146
|
+
@method serializePolymorphicType
|
|
1341
1147
|
@public
|
|
1342
1148
|
@param {Snapshot} snapshot
|
|
1343
1149
|
@param {Object} json
|
|
1344
1150
|
@param {Object} relationship
|
|
1345
1151
|
*/
|
|
1346
1152
|
serializePolymorphicType() {},
|
|
1347
|
-
|
|
1348
1153
|
/**
|
|
1349
1154
|
`extractMeta` is used to deserialize any meta information in the
|
|
1350
1155
|
adapter payload. By default Ember Data expects meta information to
|
|
1351
1156
|
be located on the `meta` property of the payload object.
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
```app/serializers/post.js
|
|
1157
|
+
Example
|
|
1158
|
+
```app/serializers/post.js
|
|
1356
1159
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1357
|
-
|
|
1358
|
-
export default class PostSerializer extends JSONSerializer {
|
|
1160
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1359
1161
|
extractMeta(store, typeClass, payload) {
|
|
1360
1162
|
if (payload && payload.hasOwnProperty('_pagination')) {
|
|
1361
1163
|
let meta = payload._pagination;
|
|
@@ -1365,8 +1167,7 @@ const JSONSerializer = Serializer.extend({
|
|
|
1365
1167
|
}
|
|
1366
1168
|
}
|
|
1367
1169
|
```
|
|
1368
|
-
|
|
1369
|
-
@method extractMeta
|
|
1170
|
+
@method extractMeta
|
|
1370
1171
|
@public
|
|
1371
1172
|
@param {Store} store
|
|
1372
1173
|
@param {Model} modelClass
|
|
@@ -1379,17 +1180,14 @@ const JSONSerializer = Serializer.extend({
|
|
|
1379
1180
|
return meta;
|
|
1380
1181
|
}
|
|
1381
1182
|
},
|
|
1382
|
-
|
|
1383
1183
|
/**
|
|
1384
1184
|
`extractErrors` is used to extract model errors when a call
|
|
1385
1185
|
to `Model#save` fails with an `InvalidError`. By default
|
|
1386
1186
|
Ember Data expects error information to be located on the `errors`
|
|
1387
1187
|
property of the payload object.
|
|
1388
|
-
|
|
1389
|
-
This serializer expects this `errors` object to be an Array similar
|
|
1188
|
+
This serializer expects this `errors` object to be an Array similar
|
|
1390
1189
|
to the following, compliant with the https://jsonapi.org/format/#errors specification:
|
|
1391
|
-
|
|
1392
|
-
```js
|
|
1190
|
+
```js
|
|
1393
1191
|
{
|
|
1394
1192
|
"errors": [
|
|
1395
1193
|
{
|
|
@@ -1406,18 +1204,14 @@ const JSONSerializer = Serializer.extend({
|
|
|
1406
1204
|
]
|
|
1407
1205
|
}
|
|
1408
1206
|
```
|
|
1409
|
-
|
|
1410
|
-
The key `detail` provides a textual description of the problem.
|
|
1207
|
+
The key `detail` provides a textual description of the problem.
|
|
1411
1208
|
Alternatively, the key `title` can be used for the same purpose.
|
|
1412
|
-
|
|
1413
|
-
The nested keys `source.pointer` detail which specific element
|
|
1209
|
+
The nested keys `source.pointer` detail which specific element
|
|
1414
1210
|
of the request data was invalid.
|
|
1415
|
-
|
|
1416
|
-
Note that JSON-API also allows for object-level errors to be placed
|
|
1211
|
+
Note that JSON-API also allows for object-level errors to be placed
|
|
1417
1212
|
in an object with pointer `data`, signifying that the problem
|
|
1418
1213
|
cannot be traced to a specific attribute:
|
|
1419
|
-
|
|
1420
|
-
```javascript
|
|
1214
|
+
```javascript
|
|
1421
1215
|
{
|
|
1422
1216
|
"errors": [
|
|
1423
1217
|
{
|
|
@@ -1429,25 +1223,20 @@ const JSONSerializer = Serializer.extend({
|
|
|
1429
1223
|
]
|
|
1430
1224
|
}
|
|
1431
1225
|
```
|
|
1432
|
-
|
|
1433
|
-
When turn into a `Errors` object, you can read these errors
|
|
1226
|
+
When turn into a `Errors` object, you can read these errors
|
|
1434
1227
|
through the property `base`:
|
|
1435
|
-
|
|
1436
|
-
```handlebars
|
|
1228
|
+
```handlebars
|
|
1437
1229
|
{{#each @model.errors.base as |error|}}
|
|
1438
1230
|
<div class="error">
|
|
1439
1231
|
{{error.message}}
|
|
1440
1232
|
</div>
|
|
1441
1233
|
{{/each}}
|
|
1442
1234
|
```
|
|
1443
|
-
|
|
1444
|
-
Example of alternative implementation, overriding the default
|
|
1235
|
+
Example of alternative implementation, overriding the default
|
|
1445
1236
|
behavior to deal with a different format of errors:
|
|
1446
|
-
|
|
1447
|
-
```app/serializers/post.js
|
|
1237
|
+
```app/serializers/post.js
|
|
1448
1238
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1449
|
-
|
|
1450
|
-
export default class PostSerializer extends JSONSerializer {
|
|
1239
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1451
1240
|
extractErrors(store, typeClass, payload, id) {
|
|
1452
1241
|
if (payload && typeof payload === 'object' && payload._problems) {
|
|
1453
1242
|
payload = payload._problems;
|
|
@@ -1457,8 +1246,7 @@ const JSONSerializer = Serializer.extend({
|
|
|
1457
1246
|
}
|
|
1458
1247
|
}
|
|
1459
1248
|
```
|
|
1460
|
-
|
|
1461
|
-
@method extractErrors
|
|
1249
|
+
@method extractErrors
|
|
1462
1250
|
@public
|
|
1463
1251
|
@param {Store} store
|
|
1464
1252
|
@param {Model} typeClass
|
|
@@ -1470,17 +1258,14 @@ const JSONSerializer = Serializer.extend({
|
|
|
1470
1258
|
if (payload && typeof payload === 'object' && payload.errors) {
|
|
1471
1259
|
// the default assumption is that errors is already in JSON:API format
|
|
1472
1260
|
const extracted = {};
|
|
1473
|
-
|
|
1474
|
-
payload.errors.forEach((error) => {
|
|
1261
|
+
payload.errors.forEach(error => {
|
|
1475
1262
|
if (error.source && error.source.pointer) {
|
|
1476
1263
|
let key = error.source.pointer.match(SOURCE_POINTER_REGEXP);
|
|
1477
|
-
|
|
1478
1264
|
if (key) {
|
|
1479
1265
|
key = key[2];
|
|
1480
1266
|
} else if (error.source.pointer.search(SOURCE_POINTER_PRIMARY_REGEXP) !== -1) {
|
|
1481
1267
|
key = PRIMARY_ATTRIBUTE_KEY;
|
|
1482
1268
|
}
|
|
1483
|
-
|
|
1484
1269
|
if (key) {
|
|
1485
1270
|
extracted[key] = extracted[key] || [];
|
|
1486
1271
|
extracted[key].push(error.detail || error.title);
|
|
@@ -1493,46 +1278,38 @@ const JSONSerializer = Serializer.extend({
|
|
|
1493
1278
|
|
|
1494
1279
|
// for each attr and relationship, make sure that we use
|
|
1495
1280
|
// the normalized key
|
|
1496
|
-
typeClass.eachAttribute(
|
|
1281
|
+
typeClass.eachAttribute(name => {
|
|
1497
1282
|
let key = this.keyForAttribute(name, 'deserialize');
|
|
1498
1283
|
if (key !== name && extracted[key] !== undefined) {
|
|
1499
1284
|
extracted[name] = extracted[key];
|
|
1500
1285
|
delete extracted[key];
|
|
1501
1286
|
}
|
|
1502
1287
|
});
|
|
1503
|
-
|
|
1504
|
-
typeClass.eachRelationship((name) => {
|
|
1288
|
+
typeClass.eachRelationship(name => {
|
|
1505
1289
|
let key = this.keyForRelationship(name, 'deserialize');
|
|
1506
1290
|
if (key !== name && extracted[key] !== undefined) {
|
|
1507
1291
|
extracted[name] = extracted[key];
|
|
1508
1292
|
delete extracted[key];
|
|
1509
1293
|
}
|
|
1510
1294
|
});
|
|
1511
|
-
|
|
1512
1295
|
return extracted;
|
|
1513
1296
|
}
|
|
1514
|
-
|
|
1515
1297
|
return payload;
|
|
1516
1298
|
},
|
|
1517
|
-
|
|
1518
1299
|
/**
|
|
1519
1300
|
`keyForAttribute` can be used to define rules for how to convert an
|
|
1520
1301
|
attribute name in your model to a key in your JSON.
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
```app/serializers/application.js
|
|
1302
|
+
Example
|
|
1303
|
+
```app/serializers/application.js
|
|
1525
1304
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1526
1305
|
import { underscore } from '<app-name>/utils/string-utils';
|
|
1527
|
-
|
|
1528
|
-
export default class ApplicationSerializer extends JSONSerializer {
|
|
1306
|
+
export default class ApplicationSerializer extends JSONSerializer {
|
|
1529
1307
|
keyForAttribute(attr, method) {
|
|
1530
1308
|
return underscore(attr).toUpperCase();
|
|
1531
1309
|
}
|
|
1532
1310
|
}
|
|
1533
1311
|
```
|
|
1534
|
-
|
|
1535
|
-
@method keyForAttribute
|
|
1312
|
+
@method keyForAttribute
|
|
1536
1313
|
@public
|
|
1537
1314
|
@param {String} key
|
|
1538
1315
|
@param {String} method
|
|
@@ -1541,26 +1318,21 @@ const JSONSerializer = Serializer.extend({
|
|
|
1541
1318
|
keyForAttribute(key, method) {
|
|
1542
1319
|
return key;
|
|
1543
1320
|
},
|
|
1544
|
-
|
|
1545
1321
|
/**
|
|
1546
1322
|
`keyForRelationship` can be used to define a custom key when
|
|
1547
1323
|
serializing and deserializing relationship properties. By default
|
|
1548
1324
|
`JSONSerializer` does not provide an implementation of this method.
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
```app/serializers/post.js
|
|
1325
|
+
Example
|
|
1326
|
+
```app/serializers/post.js
|
|
1553
1327
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1554
1328
|
import { underscore } from '<app-name>/utils/string-utils';
|
|
1555
|
-
|
|
1556
|
-
export default class PostSerializer extends JSONSerializer {
|
|
1329
|
+
export default class PostSerializer extends JSONSerializer {
|
|
1557
1330
|
keyForRelationship(key, relationship, method) {
|
|
1558
1331
|
return `rel_${underscore(key)}`;
|
|
1559
1332
|
}
|
|
1560
1333
|
}
|
|
1561
1334
|
```
|
|
1562
|
-
|
|
1563
|
-
@method keyForRelationship
|
|
1335
|
+
@method keyForRelationship
|
|
1564
1336
|
@public
|
|
1565
1337
|
@param {String} key
|
|
1566
1338
|
@param {String} typeClass
|
|
@@ -1570,12 +1342,10 @@ const JSONSerializer = Serializer.extend({
|
|
|
1570
1342
|
keyForRelationship(key, typeClass, method) {
|
|
1571
1343
|
return key;
|
|
1572
1344
|
},
|
|
1573
|
-
|
|
1574
1345
|
/**
|
|
1575
1346
|
`keyForLink` can be used to define a custom key when deserializing link
|
|
1576
1347
|
properties.
|
|
1577
|
-
|
|
1578
|
-
@method keyForLink
|
|
1348
|
+
@method keyForLink
|
|
1579
1349
|
@public
|
|
1580
1350
|
@param {String} key
|
|
1581
1351
|
@param {String} kind `belongsTo` or `hasMany`
|
|
@@ -1584,7 +1354,6 @@ const JSONSerializer = Serializer.extend({
|
|
|
1584
1354
|
keyForLink(key, kind) {
|
|
1585
1355
|
return key;
|
|
1586
1356
|
},
|
|
1587
|
-
|
|
1588
1357
|
// HELPERS
|
|
1589
1358
|
|
|
1590
1359
|
/**
|
|
@@ -1596,11 +1365,8 @@ const JSONSerializer = Serializer.extend({
|
|
|
1596
1365
|
*/
|
|
1597
1366
|
transformFor(attributeType, skipAssertion) {
|
|
1598
1367
|
let transform = getOwner(this).lookup('transform:' + attributeType);
|
|
1599
|
-
|
|
1600
1368
|
assert(`Unable to find the transform for \`attr('${attributeType}')\``, skipAssertion || !!transform);
|
|
1601
|
-
|
|
1602
1369
|
return transform;
|
|
1603
|
-
}
|
|
1370
|
+
}
|
|
1604
1371
|
});
|
|
1605
|
-
|
|
1606
|
-
export default JSONSerializer;
|
|
1372
|
+
export { JSONSerializer as default };
|