From df749b52f9306172269d05e62fcc450beedf34d7 Mon Sep 17 00:00:00 2001 From: Citlalli Murillo Date: Wed, 10 Jun 2015 16:20:43 -0500 Subject: [PATCH 1/2] Ember CLI Pagination integration --- docs/pagination.md | 53 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/docs/pagination.md b/docs/pagination.md index 994c187..cc725a5 100644 --- a/docs/pagination.md +++ b/docs/pagination.md @@ -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; + } + }, +}); +``` From f94a587b2d5b279a815e0312cfbf66b3eb1bd4ff Mon Sep 17 00:00:00 2001 From: Citlalli Murillo Date: Wed, 10 Jun 2015 16:24:55 -0500 Subject: [PATCH 2/2] path for python file as comment --- docs/pagination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pagination.md b/docs/pagination.md index cc725a5..c2d4dec 100644 --- a/docs/pagination.md +++ b/docs/pagination.md @@ -105,7 +105,7 @@ REST_FRAMEWORK = { ``` ```python -//utils.pagination.py +# utils/pagination.py from rest_framework.compat import OrderedDict from rest_framework.pagination import PageNumberPagination from rest_framework.response import Response