Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow limiting amount of data returned #20

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions angular-django-rest-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
* - `$resolved`: true if the promise has been resolved (either with success or rejection);
* Knowing if the DjangoRESTResource has been resolved is useful in data-binding.
*/
angular.module('djangoRESTResources', ['ng']).
var djangoRESTResources = angular.module('djangoRESTResources', ['ng']).
factory('djResource', ['$http', '$parse', function($http, $parse) {
var DEFAULT_ACTIONS = {
'get': {method:'GET'},
Expand Down Expand Up @@ -315,6 +315,9 @@ angular.module('djangoRESTResources', ['ng']).
arguments.length + " arguments.";
}

var paginationLimit = params.paginationLimit || null;
params.paginationLimit = undefined;

var value = this instanceof DjangoRESTResource ? this : (action.isArray ? [] : new DjangoRESTResource(data));
var httpConfig = {},
promise;
Expand Down Expand Up @@ -357,18 +360,25 @@ angular.module('djangoRESTResources', ['ng']).
deferSuccess = true;

var paginator = function recursivePaginator(data) {
// Ok, now load this page's results:
forEach(data.results, function(item) {
value.push(new DjangoRESTResource(item));
});
var morePages = paginationLimit && value.length < paginationLimit;

// If there is a next page, go ahead and request it before parsing our results. Less wasted time.
if (data.next !== null) {
if (data.next !== null && morePages) {
var next_config = copy(httpConfig);
next_config.params = {};
next_config.url = data.next;
$http(next_config).success(function(next_data) { recursivePaginator(next_data); }).error(error);
var http_promise = $http(next_config).then(function(next_data) { recursivePaginator(next_data); })
if (error) {
http_promise.catch(error);
}
}
// Ok, now load this page's results:
forEach(data.results, function(item) {
value.push(new DjangoRESTResource(item));
});
if (data.next == null) {


if (data.next == null || !morePages) {
// We've reached the last page, call the original success callback with the concatenated pages of data.
(success||noop)(value, response.headers);
}
Expand Down Expand Up @@ -397,7 +407,7 @@ angular.module('djangoRESTResources', ['ng']).

response.resource = value;
return response;
}, error).then;
}, error).then.bind(promise);

return value;
};
Expand Down Expand Up @@ -438,3 +448,5 @@ angular.module('djangoRESTResources', ['ng']).

return DjangoRESTResourceFactory;
}]);

module.exports = djangoRESTResources;
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "angular-django-rest-resource",
"main": "angular-django-rest-resource.js",
"version": "1.0.5",
"homepage": "https://github.com/blacklocus/angular-django-rest-resource",
"authors": [
"BlackLocus",
"Justin Turner Arthur",
"Contributors"
],
"description": "An AngularJS module that provides a resource-generation service similar to ngResource, but optimized for the Django REST Framework.",
"keywords": [
"AngularJS",
"Django",
"REST",
"Framework",
"Django",
"Angular",
"paginate",
"pagination"
],
"license": "MIT",
"ignore": [
"**/.*",
"**/.md",
"node_modules",
"bower_components",
"test",
"tests"
],
"repository": {
"type": "git",
"url": "git://github.com/cfrantow/angular-django-rest-resource.git"
},
"bugs": {
"url": "https://github.com/cfrantow/angular-django-rest-resource/issues"
},
"directories": {
"test": "test"
},
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Charlie <cfrantow@gmail.com>"
}