Skip to content

Commit

Permalink
do not require count attribute in response
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinwmerritt committed Oct 21, 2015
1 parent 0b07df4 commit a03d135
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 0 additions & 1 deletion addon/serializers/drf.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export default DS.RESTSerializer.extend({
let convertedPayload = {};

if (!Ember.isNone(payload) &&
payload.hasOwnProperty('count') &&
payload.hasOwnProperty('next') &&
payload.hasOwnProperty('previous') &&
payload.hasOwnProperty('results')) {
Expand Down
20 changes: 20 additions & 0 deletions docs/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ export default DRFSerializer.extend({
});
```

## Cursor Pagination

To use [`CursorPagination`](http://www.django-rest-framework.org/api-guide/pagination/#cursorpagination), override `extractPageNumber` in the serializer to extract the `cursor`.

```js
// app/serializer/drf.js

import DRFSerializer from 'ember-django-adapter/serializers/drf';

export default DRFSerializer.extend({
extractPageNumber: function(url) {
var match = /.*?[\?&]cursor=([A-Za-z0-9]+).*?/.exec(url);
if (match) {
return match[1];
}
return null;
}
});
```

If you don't use the `PageNumberPagination` for pagination with DRF 3.1 you can also add
the metadata for the pagination scheme you use here. We may add support for the other
pagination classes in the future. If this is something you are interested in contributing,
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/serializers/drf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ test('normalizeResponse - results', function(assert) {
assert.equal(modifiedPayload.meta['other'], 'stuff');
});

test('normalizeResponse - results (cursor pagination)', function(assert) {
let serializer = this.subject();
serializer._super = sinon.spy();
let primaryModelClass = {modelName: 'person'};
let payload = {
next: '/api/posts/?page=3',
previous: '/api/posts/?page=1',
other: 'stuff',
results: ['result']
};

serializer.normalizeResponse('store', primaryModelClass, payload, 1, 'requestType');
assert.equal(serializer._super.callCount, 1);
assert.equal(serializer._super.lastCall.args[0],'store');
assert.propEqual(serializer._super.lastCall.args[1], primaryModelClass);
assert.equal(serializer._super.lastCall.args[3], 1);
assert.equal(serializer._super.lastCall.args[4], 'requestType');

let modifiedPayload = serializer._super.lastCall.args[2];
assert.equal('result', modifiedPayload[primaryModelClass.modelName][0]);

assert.ok(modifiedPayload.meta);
assert.equal(modifiedPayload.meta['next'], 3);
assert.equal(modifiedPayload.meta['previous'], 1);
// Unknown metadata has been passed along to the meta object.
assert.equal(modifiedPayload.meta['other'], 'stuff');
});

test('normalizeResponse - no results', function(assert) {
let serializer = this.subject();
serializer._super = sinon.stub().returns('extracted array');
Expand Down

0 comments on commit a03d135

Please sign in to comment.