How to access top level meta key from json-api server's 200 response when calling ember data's destroyRecord()

后端 未结 2 389
栀梦
栀梦 2021-01-18 13:29

I\'m working on an Ember app that is using Ember Data and the now default json-api adapter.

According to the json-api spec (http://jsonapi.org/format/#crud-deleting)

2条回答
  •  被撕碎了的回忆
    2021-01-18 14:00

    Ember does not support meta for single model requests (find,save and destroyRecord) at the moment!

    If you want this you have to hook into ember internals.

    The following code uses ember internals from ember 2.3 and may break in future versions!

    There is the undocumented _metadataFor function on the store that gives you the last metadata for a given type. I use a custom initializer to always save it to the Model:

    import Ember from 'ember'; 
    import DS from 'ember-data';
    const {set} = Ember;
    export function initialize(application) {
      DS.Model.reopen({
        meta: null,
        didCommit() {
          this._super(...arguments);
          set(this, 'meta', this.store._metadataFor(this.constructor.modelName));
        }
      });
    };
    
    export default {
      name: 'meta',
      initialize: initialize
    };
    

    After this you can do model.save().then(() => console.log(model.get('meta'))) or model.destroyRecord.then(() => console.log(model.get('meta'))).

    Maybe checkout this ember-twiddle.

提交回复
热议问题