How could I get the current page number when paginating?
Hi, thanks for contributing this useful Ember adapter!
I'm playing with integrating a project of mine based on DRF 3.3.3 and Ember.js with Ember Data 2.6.0 and ember-django-adapter 1.1.2.
Right now in an HBS template file that lists the instances of some given model I have something like this:
{{#if model.meta.previous}}
{{#link-to 'cars' (query-params page=model.meta.previous)}}Go Previous{{/link-to}}
{{/if}}
{{#if model.meta.next}}
{{#link-to 'cars' (query-params page=model.meta.next)}}Go Next{{/link-to}}
{{/if}}
However, I would like to know how to do something like this in the same HBS template file:
Your current page is: {{model.meta.WHAT_COULD_I_USE_HERE?}}
Thanks in advance for your time and attention.
By the way, I have another question.
Besides having a variable with the value of the current page number when paginating, do you foresee another way to get a link to each individual page available in the full pagination set? I mean, something like the example showed at Displaying links to individual page numbers.
Note: for the backend side provided by DRF 3.3.3 I'm using the PageNumberPagination class.
Again, thanks a lot!
Following Django Paginator, Page.number --> meta.number would be a sensible serialisation.
Unfortunately, the default django adapter doesn't support number (only next, previous).
The easiest work-around would be to manually include number in meta from within Ember. Can either grab from the query param, or compute from next/previous. Close example is in the docs.
Something like...
export default DRFSerializer.extend({
extractMeta: function(store, type, payload) {
let meta = this._super(store, type, payload);
if (!Ember.isNone(meta)) {
if ( (meta.next-1) == meta.previous +1 )
meta['number'] = meta.next-1;
}
return meta;
}
});
Has this workaround been sufficient for folks?
What's the feeling about including number as a computed property?
- https://github.com/dustinfarris/ember-django-adapter/blob/7231937ad5e34ed44e6cb29aa3bf5827aff08b2e/addon/serializers/drf.js
- https://github.com/dustinfarris/ember-django-adapter/blob/7231937ad5e34ed44e6cb29aa3bf5827aff08b2e/tests/acceptance/pagination-test.js
It makes sense to me that we include the current page number as a computed property. I'm a bit out of the loop on this project these days so I'll defer the final decision to @dustinfarris. If he agrees, then you're welcome to make a PR to add the feature.
I like it! PR would be welcome!