Lux
Lux

Reputation: 18240

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

Upvotes: 3

Views: 1293

Answers (2)

Lux
Lux

Reputation: 18240

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')
})

Upvotes: 3

shicholas
shicholas

Reputation: 6153

Pagination is coming soon https://github.com/emberjs/data/issues/2905.

For a temp fix, I redid my server logic so that my pagination links were returned in the meta key instead of links one. That way I could tap into the extractMeta method already present in the JSONAPISerializer. Not the best solution, but I think it will hold me over until I can implement the official way.

Upvotes: 0

Related Questions