Skip to content

Commit

Permalink
Added hacky method for accessing meta value of single-record requests
Browse files Browse the repository at this point in the history
no issue

- Ember Data does not support accessing meta data included in the response to single-record requests such as save/delete
- approach to allow it taken from emberjs/data#4077 (comment)
  • Loading branch information
kevinansfield authored and naz committed Dec 18, 2019
1 parent 71c0cd1 commit 13d529c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/models/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Model from 'ember-data/model';

export default Model.extend({
// this is a hack that gives us access to meta data in single resource responses
// allows similar response.get('_meta') as with multi-resource responses but can
// suffer from race conditions
// TODO: review once the record links and meta RFC lands
// https://github.com/emberjs/rfcs/blob/master/text/0332-ember-data-record-links-and-meta.md
get _meta() {
return this._internalModel.type.___meta;
}
});
4 changes: 2 additions & 2 deletions app/models/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import Model from 'ember-data/model';
import BaseModel from './base';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import attr from 'ember-data/attr';
import {computed} from '@ember/object';
Expand All @@ -8,7 +8,7 @@ import {hasMany} from 'ember-data/relationships';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';

export default Model.extend(ValidationEngine, {
export default BaseModel.extend(ValidationEngine, {
validationType: 'user',

name: attr('string'),
Expand Down
10 changes: 10 additions & 0 deletions app/serializers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import {camelize, decamelize, underscore} from '@ember/string';
import {pluralize} from 'ember-inflector';

export default RESTSerializer.extend({
// hacky method for getting access to meta data for single-resource responses
// https://github.com/emberjs/data/pull/4077#issuecomment-200780097
// TODO: review once the record links and meta RFC lands
// https://github.com/emberjs/rfcs/blob/master/text/0332-ember-data-record-links-and-meta.md
extractMeta(store, typeClass) {
let meta = this._super(...arguments);
typeClass.___meta = meta;
return meta;
},

serialize(/*snapshot, options*/) {
let json = this._super(...arguments);

Expand Down

0 comments on commit 13d529c

Please sign in to comment.