Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection into Angular Provider

I am wondering if there is any cleaner way of injecting into a provider. As I am doing it now, I have to have http = null, and then set http = $http in $get so that I am able to use it in my functions. Below is my code for the provider

CoffeeScript:

do ->
  githubProvider =() ->
    http = null
    getUser =(username) ->
      return  http.get("https://api.github.com/users/" + username)
                  .then (response)->
                    return response.data

    getRepos =(user) ->
      return http.get(user.repos_url)
                  .then (response)->
                    return response.data

    getRepoDetails =(username, repoName) ->
      return http.get("https://api.github.com/repos/" + username + "/" + repoName)
                  .then (response)->
                    return response.data

    getRepoCollaborators =(repo) ->
      return http.get(repo.contributors_url)
            .then (response)->
              return response.data

    this.$get =["$http", ($http) ->
      http = $http
      return {
      getUser: getUser, 
      getRepos: getRepos, 
      getRepoDetails: getRepoDetails,
      getRepoCollaborators: getRepoCollaborators
      }]
    return 
  app = angular.module("githubViewer")
  app.provider("githubProvider", [githubProvider])
  return

JavaScript:

  (function() {
    var app, githubProvider;
    githubProvider = function() {
      var getRepoCollaborators, getRepoDetails, getRepos, getUser, http;
      http = null;
      getUser = function(username) {
        return http.get("https://api.github.com/users/" + username).then(function(response) {
          return response.data;
        });
      };
      getRepos = function(user) {
        return http.get(user.repos_url).then(function(response) {
          return response.data;
        });
      };
      getRepoDetails = function(username, repoName) {
        return http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
          return response.data;
        });
      };
      getRepoCollaborators = function(repo) {
        return http.get(repo.contributors_url).then(function(response) {
          return response.data;
        });
      };
      this.$get = [
        "$http", function($http) {
          http = $http;
          return {
            getUser: getUser,
            getRepos: getRepos,
            getRepoDetails: getRepoDetails,
            getRepoCollaborators: getRepoCollaborators
          };
        }
      ];
    };
    app = angular.module("githubViewer");
    app.provider("githubProvider", [githubProvider]);
  })();
like image 954
user4029197 Avatar asked Nov 01 '22 17:11

user4029197


1 Answers

As what the AngularJS Developer's Guide mentioned:

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts

From what I see in your code, most of the functions can only be used after the configuration phase. You have two options to consider.

[1] If you don't have any configuration that you need to setup during the configuration phase, then how about consider creating a service instead of a provider.

.service('github', ['$http', function($http) {
      this.getUser = function(username) {
        return $http.get("https://api.github.com/users/" + username).then(function(response) {
          return response.data;
        });
      };
      this.getRepos = function(user) {
        return $http.get(user.repos_url).then(function(response) {
          return response.data;
        });
      };
      this.getRepoDetails = function(username, repoName) {
        return $http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
          return response.data;
        });
      };
      this.getRepoCollaborators = function(repo) {
        return $http.get(repo.contributors_url).then(function(response) {
          return response.data;
        });
      };
}]);

[2] If you do have any configuration, then simply copy the service above and have it defined in the provider's $get.

.provider('github', function() {
      var configuration = {};

      this.setConfiguration = function(configurationParams) {
         configuration = configurationParams;
      };

      this.$get = ['$http', function($http) {
        // you can choose to use your configuration here..

        this.getUser = function(username) {
          return $http.get("https://api.github.com/users/" + username).then(function(response) {
            return response.data;
          });
        };
        this.getRepos = function(user) {
          return $http.get(user.repos_url).then(function(response) {
            return response.data;
          });
        };
        this.getRepoDetails = function(username, repoName) {
          return $http.get("https://api.github.com/repos/" + username + "/" + repoName).then(function(response) {
            return response.data;
          });
        };
        this.getRepoCollaborators = function(repo) {
          return $http.get(repo.contributors_url).then(function(response) {
            return response.data;
          });
        };
      }];
});

This provider can then be use during the configuration phase like this:

.config(function(githubProvider) {
  githubProvider.setConfiguration({
    dummyData: 'Dummy Data'
  });
});

and during the run phase or in a controller:

.run(function(github) {
  github.getUser('ryebalar').then(function(data) {
    console.log(data);
  });
});

Here's a guide to help you with providers, developer's guide, note that the quote I have provided above is from that guide.

like image 186
ryeballar Avatar answered Nov 09 '22 06:11

ryeballar