Take the 2-minute tour ×
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 '13 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 '13 at 15:54
 
Look at this fiddle: jsfiddle.net/3tP8Y You can access properties using a string without resorting to eval. –  RobH Apr 7 '13 at 16:11
1  
@RobH palm meets forehead, thanks...evals removed. –  µBio Apr 8 '13 at 17:12
add comment

closed as unclear what you're asking by Jamal Dec 16 '13 at 2:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.