0

I have this call. I would like to manipulate the data returned by the REST service before the controller uses it. How can this be accomplished? Now I get the raw data. I understand that I can but this logic ApiFactory.get(function(data)); but I prefer to have the logic in the server. How can I do this?

In Service:

app.factory('ApiFactory', ['$resource', function($resource) {
  return $resource('http://localhost:8080/rest/forum/categories/1');
}]);

In controller:

app.controller('SubjectCntrl', ['$scope', 'categoryService', 'ApiFactory', function($scope, categoryService, ApiFactory) {
    ApiFactory.get(function(data) {
        console.log(data);
    });
}]);
1
  • You might be looking for $locationProvider's resolve. Commented Nov 28, 2013 at 11:45

1 Answer 1

1

You can create another factory that parses the data as you want. Something like that:

app.factory('ApiFactory', ['$resource', function($resource) {
    return $resource('http://localhost:8080/rest/forum/categories/1');
}]);

app.factory('ApiParser', ['ApiFactory', function(ApiFactory) {
    return {
        get: function() {
            var data = ApiFactory.get(function(d) {
                /* do whatever you need to do with the data */
                return parsedData;
            });
            return data;
        }
    }
}]);

app.controller('SubjectCntrl', ['$scope', 'categoryService', 'ApiParser', function($scope, categoryService, ApiParser) {
    $scope.parsedData = ApiParser.get();
}]);
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.