The following service repeatedly queries urlToStream with paging parameters and appends the results to an array in a scope (passed in as a parameter). It works but feels dirty (not the least of which is because of the evals)
usage
StreamService.stream("/api/something/", $scope.someArray, "TotalCount", "Jobs",
"PageSize");
definition
app.factory('StreamService', function($http) {
return {
stream: function(urlToStream, modelToAppend, countProperty, dataProperty, pageSizeProperty) {
var promise = $http.get(urlToStream).then(function(response) {
var count = response.data[countProperty];
var pageSize = response.data[pageSizeProperty];
for (var i = 0; i < pageSize; i++) {
var pageData = response.data[dataProperty];
modelToAppend.push(pageData[i]);
}
for (var j = 1; j < count / pageSize; j++) {
$http.get(urlToStream + j).then(function(inner) {
for (var k = 0; k < pageSize; k++) {
{
var innerPageData = inner.data[dataProperty];
modelToAppend.push(innerPageData[k]);
}
}
});
}
});
return promise;
}
};
});
response.data[countProperty]
instead of using eval? – RobH Apr 5 at 10:52