Search code examples
ember-datajson-api

How to use jsonapi pagination with ember-data 1.13


Is there any possibility at the moment to use pagination with ember-data 1.13?

I have a jsonapi.org compatible API with a next link in the top-level links object. How can I tell my store/response array to load this page?

What I wanna do is something like:

actions: {
  loadMore() {
    this.get('model').loadNextPage().then((data) => {
      this.set('model', data);
    });
  }
}

Thanks


Solution

  • My working client-side hack around:

    I override ajaxOptions on the adapter to make it possible to directly access a URL with store.find('model', { loadNext: '/loadMoarLink' }):

    ajaxOptions(url, type, options) {
        if(options && options.data && options.data.loadNext) {
            url = options.data.loadNext;
            delete options.data.loadNext
        }
        return this._super(url, type, options);
    }
    

    To access the links I override the normalizeResponse on the adapter to write the links on the meta object:

    normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {
        let links = payload.links;
        let response = this._super(...arguments);
        if(!get(response, 'meta')) {
            set(response, 'meta', Ember.Object.create());
        }
        set(response, 'meta.links', links);
        return response;
    }
    

    Then I can access the metadata from the setupController:

    controller.set('meta', Ember.copy(this.store.metadataFor('model'), true));
    

    And always query the next data:

    this.store.query('model', {
      loadNext: this.get('controller.meta.links.next')
    })