javascript - How to use angular 1.6.5 call $http request recursively when data set returned is limited -
i use angular 1.6.5 project rebuild, i'm not sure how use $http.get request in factory when source returns limited number of records @ time (1000 returned per request) , there on 2000 records need get.
in current code use jquery ajax , in .done method check presence of value "__next", , if exists, recall function passing value "__next". when "__next" value isn't returned, data.
function getspecifiedlist(url){ var specurl = url; $.ajax({ url: specurl, type: "get", headers:{"accept":"application/json;odata=verbose", error: function(xhr){ console.log(xhr.status + " " + xhr.statustext); } } }).done(function (results){ $("#wc_report_holder").text(results.length); //buildobjects processes results , adds array buildobject(results); if(results.d.__next){ getspecifiedlist(results.d.__next); }else{ buildgridview(); } }).fail(function(error){ $("#wc_report_holder").text("there error: " + error); }); } i figure out how implement same value check , recursive call in angular 1.6.5 using best practice , efficient haven't had luck figuring out based on angular docs , googling.
here short version of have using angular 1.6.5.
<script> var sitesapp = angular.module("sitesapp", ['ngroute']); sitesapp.controller('siteslistctrl', ['$scope', 'sites', function ($scope, sites) { sites.list().then(function (response) { $scope.sites = response.data.value; }); } ]); sitesapp.controller("sitedetailsctrl", ['$scope', '$routeparams', 'sites', function ($scope, $routeparams, sites) { sites.find($routeparams.sitecodepc, function (site) { $scope.site = site; }); } ]); sitesapp.config(function ($routeprovider, $locationprovider) { $locationprovider.hashprefix('!'); $routeprovider. when('/', { templateurl: 'https://machine/sites/site-list.html', controller: 'siteslistctrl' }). when('/:sitecodepc', { templateurl: 'https://machine/sites/site-details.html', controller: 'sitedetailsctrl' }). otherwise({ redirectto: '/' }); }); sitesapp.factory('sites', ['$http', function ($http) { var urlbase = "https://some-endpoint-for-data"; var cacheddata; function getdata(callback) { if (cacheddata) { callback(cacheddata); } else { return $http({ method: 'get', url: urlbase }) .then(function (response) { //here think solution needs implemented cacheddata = response; return cacheddata; }); } } return { list: getdata, find: function (sitecodepc, callback) { getdata(function (response) { var site = response.data.value.filter(function (entry) { //debugger; return entry.sitecodepc === sitecodepc; }); callback(site[0]); }); } }; }]); </script> <div ng-app="sitesapp"> <div ng-view></div> </div> thanks in advance
it looks can simple recursion accept second (optional) parameter. if calling getdata() first time can first 1000 results. if find __next call again sending current 1000 results have , concat next 1000 results previous 1000.
sitesapp.factory('sites', ['$http', function ($http) { var urlbase = "https://some-endpoint-for-data"; function getdata(callback, results) { return $http({ method: 'get', url: urlbase }) .then(function (response) { // if have found previous batch of results concat 2 arrays if(results) { response = response.concat(results); } // if there more results found recursively call same function passing batched results if(response.__next) { return getdata(callback, response); } // if there no more results found trigger callback function else { callback(response); } }); } return { list: getdata, find: function (sitecodepc, callback) { getdata(function (response) { var site = response.data.value.filter(function (entry) { //debugger; return entry.sitecodepc === sitecodepc; }); callback(site[0]); }); } }; }]);
Comments
Post a Comment