Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ember CLI Pagination integration #101

Merged
merged 2 commits into from
Jun 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions docs/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,54 @@ The complete pagination configuration documentation is available in Django REST

[https://github.com/mharris717/ember-cli-pagination](https://github.com/mharris717/ember-cli-pagination)

It's should be possible to use the pagination metadata with Ember CLI Pagination. If you
get this working, please consider submitting a pull request documenting the configuration.
Add a total_pages key in API response by making a CustomPageNumberPagination class
```python
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'utils.pagination.CustomPageNumberPagination'
}
```

```python
# utils/pagination.py
from rest_framework.compat import OrderedDict
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

class CustomPageNumberPagination(PageNumberPagination):
def get_paginated_response(self, data):
return Response(OrderedDict([
('total_pages', self.page.paginator.num_pages),
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
```

Then override ```extractMeta``` function of the DRFSerializer in your model serializer

```js
//app/serializer/post.js
import DRFSerializer from './drf';
import DS from 'ember-data';

export default DRFSerializer.extend(DS.EmbeddedRecordsMixin, {
extractMeta: function(store, type, payload) {
if (payload && payload.results) {
// Sets the metadata for the type.
store.setMetadataFor(type, {
count: payload.count,
next: this.extractPageNumber(payload.next),
previous: this.extractPageNumber(payload.previous),
total_pages: payload.total_pages
});

// Keep ember data from trying to parse the metadata as a records
delete payload.count;
delete payload.next;
delete payload.previous;
delete payload.total_pages;
}
},
});
```