Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
    }
};
});
share|improve this question
2  
Can't you just use response.data[countProperty] instead of using eval? –  RobH Apr 5 at 10:52
 
@RobH No, it is the name of the property in the json object returned by the server, which can of course vary. –  µBio Apr 5 at 15:54
 
Look at this fiddle: jsfiddle.net/3tP8Y You can access properties using a string without resorting to eval. –  RobH Apr 7 at 16:11
1  
@RobH palm meets forehead, thanks...evals removed. –  µBio Apr 8 at 17:12

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.