javascriptangularjsdependency-injectiondeployd

AngularJS: How to use or inject thirdparty lib in AngularJS


I'm new to Angular and Deployd and wondering how to use them together.

I found the example in Deployd website nice, but it's only consuming rest API data and I'd like to understand how to have Deployd as a service inside AngularJS. For example, keeping all of the clients API up-to-date with the collection's latest data with collection events available in deployd.

I came up with the example below, where we can see that I'm using $resource to consume the rest api, but inside the controller "MyCtrl", I'm calling dpd, that I'd like to use to take advantage of features such as http://docs.deployd.com/docs/collections/notifying-clients.md

I'd really like to see some examples, or any advice concerning this!

Thanks for looking :)

angular.module('questions', ['ngResource'])

.factory('Deployd', function(dpd){
  return dpd;
})

.factory('EntriesService', function($resource){
  return $resource('/entries', {});
})

.controller('MainCtrl', ['$scope', 'EntriesService', function($scope, EntriesService) {

  $scope.title = "Q&A Module";

  $scope.entries = [];

  EntriesService.query(function(response){
    $scope.entries = response;       
  });

  $scope.addMessage = function() {
    $scope.entries.push({
        author: "myAuthor",
        message: $scope.message
    });

    EntriesService.save({
        author: "myAuthor",
        message: $scope.message
    });

  };

  dpd.comments.get(function(comments, error) {
    comments.forEach(function(comment) {
      console.log(comment);
    });
  });

}]);

Solution

  • I found a solution. This may be helpful in the future for someone else:

    angular.module('questions', ['ngResource'])
    
    .factory('Deployd', function(){
      return dpd;
    })
    
    .factory('EntriesService', function($resource){
      return $resource('/entries', {});
    })
    
    .controller('MainCtrl', ['$scope', 'EntriesService', 'Deployd', function($scope, EntriesService, Deployd) {
    
      $scope.title = "Q&A Module";
    
      $scope.entries = [];
    
      EntriesService.query(function(response){
        $scope.entries = response;        
      });
    
      $scope.addMessage = function() {
    
        var author = "myAuthor";
        var message = $scope.message;
    
        Deployd.entries.post({
          author: author,
          message: message
        }, function(comment, error) {
    
          if (error) {
            return showError(error);
          }
    
          $scope.entries.push({
            author: author,
            message: message
          });
    
        });
    
      };
    
      Deployd.entries.on('create', function() {
        EntriesService.query(function(response){
          $scope.entries = response;        
        });
      });
    
    }]);