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 0e021a6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var posts = [
}
];

module('Acceptance: Pagination', {
module('Acceptance: Cursor Pagination', {
beforeEach: function() {
application = startApp();

Expand Down Expand Up @@ -98,7 +98,6 @@ module('Acceptance: Pagination', {

var offset = (page - 1) * pageSize;
return [200, {'Content-Type': 'application/json'}, JSON.stringify({
count: posts.length,
next: nextUrl,
previous: previousUrl,
results: posts.slice(offset, offset + pageSize)
Expand All @@ -114,9 +113,9 @@ module('Acceptance: Pagination', {
});

test('Retrieve list of paginated records', function(assert) {
assert.expect(7);
assert.expect(6);

return store.query('post', {page: 1}).then(function(response) {
return store.query('post', {}).then(function(response) {
assert.ok(response);

assert.equal(response.get('length'), 4);
Expand All @@ -127,7 +126,6 @@ test('Retrieve list of paginated records', function(assert) {
assert.equal(post.get('body'), 'post body 2');

var metadata = response.get('meta');
assert.equal(metadata.count, 6);
assert.equal(metadata.next, 2);
assert.equal(metadata.previous, null);
});
Expand All @@ -140,46 +138,3 @@ test('queryRecord with paginated results returns a single record', function(asse
assert.equal(post.get('body'), 'post body 1');
});
});

test("Type metadata doesn't have previous", function(assert) {
assert.expect(4);

return store.query('post', {page: 1}).then(function(response) {
assert.ok(response);

var metadata = response.get('meta');
assert.equal(metadata.count, 6);
assert.equal(metadata.next, 2);
assert.equal(metadata.previous, null);
});
});


test("Type metadata doesn't have next", function(assert) {
assert.expect(5);

return store.query('post', {page: 2}).then(function(response) {
assert.ok(response);
assert.equal(response.get('length'), 2);

var metadata = response.get('meta');
assert.equal(metadata.count, 6);
assert.equal(metadata.next, null);
assert.equal(metadata.previous, 1);
});
});


test("Test page_size query param", function(assert) {
assert.expect(5);

return store.query('post', {page: 2, page_size: 2}).then(function(response) {
assert.ok(response);
assert.equal(response.get('length'), 2);

var metadata = response.get('meta');
assert.equal(metadata.count, 6);
assert.equal(metadata.previous, 1);
assert.equal(metadata.next, 3);
});
});
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 0e021a6

Please sign in to comment.