diff --git a/angular-django-rest-resource.js b/angular-django-rest-resource.js index d1d8f6b..890f11f 100644 --- a/angular-django-rest-resource.js +++ b/angular-django-rest-resource.js @@ -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'}, @@ -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; @@ -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); } @@ -397,7 +407,7 @@ angular.module('djangoRESTResources', ['ng']). response.resource = value; return response; - }, error).then; + }, error).then.bind(promise); return value; }; @@ -438,3 +448,5 @@ angular.module('djangoRESTResources', ['ng']). return DjangoRESTResourceFactory; }]); + +module.exports = djangoRESTResources; diff --git a/package.json b/package.json new file mode 100644 index 0000000..dbfad2e --- /dev/null +++ b/package.json @@ -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 " +}