Accessing meta information passed in a json server response

后端 未结 3 932
一整个雨季
一整个雨季 2020-12-23 10:46

I am using the Ember-Data Rest-Adapter and the JSON returned from my server looks basically like the one in the Active Model Serializers Documentation

{
  \"         


        
3条回答
  •  清歌不尽
    2020-12-23 10:58

    I figured out a way to access the meta information passed in a response. But unfortunately it really does not seem to be supported by ember-data out of the box and I am writing the meta information to a global variable that I am then accessing via bindings in the requesting controller.

    I ended up customizing the serializer the RESTAdapter is using:

    App.CustomRESTSerializer = DS.RESTSerializer.extend({
      extractMeta: function(loader, type, json) {
        var meta;
        meta = json[this.configOption(type, 'meta')];
        if (!meta) { return; }
        Ember.set('App.metaDataForLastRequest', meta);
        this._super(loader, type, json);
      }
    });
    
    App.Store = DS.Store.extend({
      revision: 11,
      adapter: DS.RESTAdapter.create({
        bulkCommit: false,
        serializer: App.CustomRESTSerializer
      })
    });
    

    I am aware that this is not particularly pretty and actually think that this is against what ember-data expects us to do but fot the time being it's working correctly.

    I will try to get this working with ember-data in a better way and submit a pull request when it is working or open an issue on github when anybody else is interested in getting this to work.

    If anybody finds a more clean solution to this please let me know.

提交回复
热议问题