Accessing meta information passed in a json server response

后端 未结 3 910
一整个雨季
一整个雨季 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:53

    I think the immediate fix for you would be to attach totalCount to your model(recordArray), see this thread.

    Another way to go would be to create your own adapter:

    DS.Adapter.create({
      find: function (store, type, id) {
        $.ajax({
          url:      type.url,
          dataType: 'jsonp',
          context:  store,
          success:  function(response){
            this.load(type, id, response.data);
          }
        });
      },
      findAll: function(store, type) {
        $.ajax({
          url:      type.url,
          dataType: 'jsonp',
          context:  store,
          success:  function(response){
            this.loadMany(type, response.data);
          }
        });
      }
    });
    

    Response parameter in success callback in findAll method, should be an object that you need:

    response: {
      meta: {
        totalCount: 10
      },
      posts: [{}, {}]
    }
    

    Hope this helps.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-23 11:04

    I found a cleaner approach for extracting meta information from the server response with ember-data.

    We have to tell the serializer which meta-information to expect (in this case pagination):

     App.serializer = DS.RESTSerializer.create();
    
     App.serializer.configure({ pagination: 'pagination' });
    
     App.CustomAdapter = DS.RESTAdapter.extend({
       serializer: App.serializer
     });
    
     App.Store = DS.Store.extend({
       adapter: 'App.CustomAdapter'
     });
    

    After that every time the server sends a meta-property with a pagination object this object will be added to the store's TypeMaps property for the requested Model-Class.

    For example with the following response:

      {
        'meta': {'pagination': { 'page': 1, 'total': 10 } },
        'posts':[
          ...
        ]
      }
    

    The TypeMap for the App.Post-Model would include the pagination object after the posts have loaded.

    You can't observe the TypeMaps-property of the store directly so I added an computed property to the PostsController to have access to the requests pagination meta information:

     App.PostsController = Ember.ArrayController.extend({
        pagination: function () {
          if (this.get('model.isLoaded')) {
            modelType = this.get('model.type');
            this.get('store').typeMapFor(modelType).metadata.pagination
          }
        }.property('model.isLoaded')
     });
    

    I really don't think that's a great solution to the meta information problem but this is the best solution I was able to come up with yet with Ember-Data. Maybe this will be easier in the future.

    0 讨论(0)
提交回复
热议问题