0

Currently, I'm doing things like this:

var albumList = $resource('https://api.imgur.com/3/account/guy123/albums').get(function () {
    albumList.data.forEach(function (album) {
        albums.push(album);
    });
});

How do I turn that into a function that I can call in both my service and controller like:

factory('Imgur', function($resource, $http) {
    var albumsService = {};
    var albums = [];

    albumsService.getAlbumList = function() {
        var albumList = $resource('https://api.imgur.com/3/account/guy123/albums').get(function () {
            albumList.data.forEach(function (album) {
                albums.push(album);
            });
        });
    };

    albumsService.albumList = function() {
        albumsService.getAlbumList();
        return albums;
    };

    return albumsService;
});


.controller('Filters', ['$scope','Imgur', function($scope, Imgur) {
    $scope.imgur = Imgur;
    $scope.imgur.albumList();
    //OR
    $scope.imgur.getAlbumList();

    //Some good context here is what if a user wanted to "refresh" the data.
    $scope.updateFilter = function() {
        $scope.imgur.getAlbumList();
    }; 


}]);

Ultimately the goal here is to be able to call a resource service as many times as I want. The service should be a function callable by both inside the service and inside the controller.

14
  • Might help to see a plunkr/fiddle of the entire code/controller etc. Commented May 6, 2013 at 17:30
  • I am not sure what you're trying to accomplish here. I think you're overcomplicating things. Are you just trying to access the imgur api, but have the results in a different format? It's unclear what your problem is. Commented May 6, 2013 at 17:45
  • The 2nd to last line $scope.imgur.albumList(); doesn't make sense. You're not assigning the result of the function to anything. Commented May 6, 2013 at 17:46
  • @jessegavin Yea sorry syntax from copying. So, I am trying to wrap var albumList as a function. Ultimately the goal here is to be able to call a resource service as many times as I want. The service should be a function callable by both inside the service and inside the controller. Commented May 6, 2013 at 17:49
  • 1
    Still doesn't make sense. Are you attempting to display any of the data from Imgur? If so, you'll need to assign the results from your service function to a property on the scope. Commented May 6, 2013 at 18:03

2 Answers 2

1
angular.module("MyApp", ['ng-resource']).
  service("Database", function() {
    return {
      albums : $resource('https://api.imgur.com/3/account/guy123/albums')
    }
  }).
  controller("MyCtrl", function(Database) {
    $scope.albums = Database.albums.query();
  })

Then in your Html

<html ng-app="MyApp">
<head></head>
<body ng-controller="MyCtrl">
  <ul>
    <li ng-repeat="album in albums">
      {{album.name}}
    </li>
  </ul>
</body> 
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

I would need to be able to make service calls within the service itself, thus the need to execute the service as a function.
Why do you need to do that?
I made some edits to post hopefully that will clear up confusion.
Ok it seems like what I am trying to do isn't going to work out. I need to rethink my app structure. However, using this method, how do I impliment success and error handlers in both the controller? In other words - assign $scope.albums = data on success.
1

The Service

var service = angular.module("yourApp.service", ['ngResource']);
service.factory('albumsService', [$resource',function ($resource){
    return $resource('https://api.imgur.com/3/account/guy123/albums',{},{
         query: {method: "GET", isArray:true}
    });
}]);

Then in the controller

$scope.albumList = albumsService.query();

5 Comments

I would need to be able to make service calls within the service itself, thus the need to execute the service as a function.
Could you possibly use params: ? Depending on what type of service calls you are doing. I know in one of my use cases I have several types of requests that I manage through parameters.
I made some edits to post hopefully that will clear up confusion.
Ok it seems like what I am trying to do isn't going to work out. I need to rethink my app structure. However, using this method, how do I impliment success and error handlers in both the controller? In other words - assign $scope.albumList = data on success.
As per the angular doc "Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most case one never has to write a callback function for the action methods." docs.angularjs.org/api/ngResource.$resource#Returns

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.