0

I have this code in my controller.js file in AngularJS:

app.controller('MetaDetailGroupList', ['$scope', '$http', function($scope, $http) {
        $http.get(Routing.generate('meta-detail-group-list')).success(function(data) {
            $scope.MetaDetailGroup = data;
            $scope.orderProp = 'name';
            $scope.currPage = 0;
            $scope.pageSize = 10;

            $scope.totalMetaDetailGroup = function() {
                return Math.ceil($scope.MetaDetailGroup.entities.length / $scope.pageSize);
            };

        }).error(function(data, status, headers, config) {
            $scope.MetaDetailGroup.message = "Ocurrieron errores al procesar los datos, por favor vuelva a intentarlo.";
        });
    }]);

I use this function to build a list of items, it works fine. Some of those items has parent >> children relationship so I need to call the same function once again but this time passing and ID as a optional parameter to get the right childrens so the only change is this line:

From: $http.get(Routing.generate('meta-detail-group-list')).success(function(data)

To: $http.get(Routing.generate('meta-detail-group-list' + '/'+id)).success(function(data) 

How I can do this without write another function just for this?

1 Answer 1

1

I would suggest you move your $http call to a service and handle the id processing stuff there:

app.service('MetaDetailGroupListService',function($http) {
    return {
        metaDetailGroupList : function(_id) {
            var _s = (typeof _id === 'undefined') ?
                'meta-detail-group-list' :
                'meta-detail-group-list' + '/' + _id;
            return $http.get(Routing.generate(_s));
        }
    }
});

app.controller('MetaDetailGroupList', ['$scope', 'MetaDetailGroupListService',
function($scope, MetaDetailGroupListService) {
    MetaDetailGroupListService.metaDetailGroupList(id).success(function(data) {
        $scope.MetaDetailGroup = data;
        $scope.orderProp = 'name';
        $scope.currPage = 0;
        $scope.pageSize = 10;

        $scope.totalMetaDetailGroup = function() {
            return Math.ceil($scope.MetaDetailGroup.entities.length / $scope.pageSize);
        };

    }).error(function(data, status, headers, config) {
        $scope.MetaDetailGroup.message = "Ocurrieron errores al procesar los datos, por favor vuelva a intentarlo.";
    });
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.