Skip to content

Commit

Permalink
Add pageSize to metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
benkonrath committed Jan 7, 2015
1 parent 28fcd8c commit a1e1b70
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion addon/serializers/drf.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export default DS.RESTSerializer.extend({
store.metaForType(type, {
count: payload.count,
next: this.extractPageNumber(payload.next),
previous: this.extractPageNumber(payload.previous)
previous: this.extractPageNumber(payload.previous),
pageSize: payload.results.length

This comment has been minimized.

Copy link
@benkonrath

benkonrath Jan 7, 2015

Author Collaborator

This is what I meant by adding pageSize to the metadata. We can still remove this if we think it's not a good idea.

The only issue I can see it that pageSize will not be accurate on the last page. It's possible to detect the last page (e.g. when next is null) and do something special like set pageSize to null or don't include pageSize in the metadata. I'm not sure which is the best approach without writing some application code. Thoughts?

});

// Keep ember data from trying to parse the metadata as a records
Expand Down
10 changes: 7 additions & 3 deletions tests/integration/pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module('Pagination', {
* https://stackoverflow.com/questions/26317855/ember-cli-how-to-do-asynchronous-model-unit-testing-with-restadapter
*/
test('Retrieve list of paginated records', function() {
expect(8);
expect(9);

stop();
Ember.run(function() {
Expand All @@ -133,6 +133,7 @@ test('Retrieve list of paginated records', function() {
equal(metadata.count, 6);
equal(metadata.next, 2);
equal(metadata.previous, null);
equal(metadata.pageSize, 4);

// No metadata on results when using find without query params.
ok(!response.get('meta'));
Expand All @@ -144,7 +145,7 @@ test('Retrieve list of paginated records', function() {


test("Type metadata doesn't have previous", function() {
expect(5);
expect(6);

stop();
Ember.run(function() {
Expand All @@ -156,6 +157,7 @@ test("Type metadata doesn't have previous", function() {
equal(metadata.count, 6);
equal(metadata.next, 2);
equal(metadata.previous, null);
equal(metadata.pageSize, 4);

// No metadata on results when using find without query params.
ok(!response.get('meta'));
Expand Down Expand Up @@ -194,7 +196,7 @@ test("Type metadata doesn't have next", function() {


test("Test page_size query param", function() {
expect(8);
expect(10);

stop();
Ember.run(function() {
Expand All @@ -207,12 +209,14 @@ test("Test page_size query param", function() {
equal(typeMetadata.count, 6);
equal(typeMetadata.previous, 1);
equal(typeMetadata.next, 3);
equal(typeMetadata.pageSize, 2);

// Test the results metadata.
var resultsMetadata = response.get('meta');
equal(resultsMetadata.count, 6);
equal(resultsMetadata.previous, 1);
equal(resultsMetadata.next, 3);
equal(resultsMetadata.pageSize, 2);

start();
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/serializers/drf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ test('extractMeta', function() {
var serializer = this.subject();
var store = { metaForType: sinon.spy() };
var payload = {
results: 'mock',
results: ['mock', 'results'],
count: 'count',
next: '/api/posts/?page=3',
previous: '/api/posts/?page=1'
};

serializer.extractMeta(store, 'type', payload);

ok(store.metaForType.calledWith('type', {count: 'count', next: 3, previous: 1}),
ok(store.metaForType.calledWith('type', {count: 'count', next: 3, previous: 1, pageSize: 2}),
'metaForType not called properly');
ok(!payload.count, 'payload.count not removed');
ok(!payload.next, 'payload.next not removed');
Expand Down

0 comments on commit a1e1b70

Please sign in to comment.