Skip to content

Commit

Permalink
Check parsed results for total property
Browse files Browse the repository at this point in the history
Fixes #113
  • Loading branch information
kriszyp committed Mar 25, 2015
1 parent c89b909 commit 6a999ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
20 changes: 13 additions & 7 deletions Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,31 @@ define([
headers: headers
});
var collection = this;
var parsedResponse = response.then(function (response) {
return collection.parse(response);
});
return {
data: response.then(function (response) {
var results = collection.parse(response);
data: parsedResponse.then(function (data) {
// support items in the results
results = results.items || results;
var results = data.items || data;
for (var i = 0, l = results.length; i < l; i++) {
results[i] = collection._restore(results[i], true);
}
return results;
}),
total: response.response.then(function (response) {
var total = response.data.total;
total: parsedResponse.then(function (data) {
// check for a total property
var total = data.total;
if (total > -1) {
// if we have a valid positive number from the data,
// we can use that
return total;
}
var range = response.getHeader('Content-Range');
return range && (range = range.match(/\/(.*)/)) && +range[1];
// else use headers
return response.response.then(function (response) {
var range = response.getHeader('Content-Range');
return range && (range = range.match(/\/(.*)/)) && +range[1];
});
}),
response: response.response
};
Expand Down
9 changes: 8 additions & 1 deletion tests/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ define([
expected = rangeArgs;
results = collection.fetch();
} else {
mockRequest.setResponseHeaders({
'Content-Range': rangeArgs.start + '-' + rangeArgs.end + '/' + expectedResults.length
});
results = collection.fetchRange(rangeArgs);
results = results.totalLength.then(function (totalLength) {
assert.strictEqual(totalLength, expectedResults.length);
return results;
});
}
return when(results).then(function (results) {
return results.then(function (results) {
expected.headers && mockRequest.assertRequestHeaders(expected.headers);
expected.queryParams && mockRequest.assertQueryParams(expected.queryParams);

Expand Down

0 comments on commit 6a999ab

Please sign in to comment.