Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an AngularJS function to service

I have the following function that I'd like to convert to a service.

How would I go extracting the $resource call into an AngularJS service, and how would I call this service?

self.select = function (email) {
    var Player = $resource('/player/get',{email:email});
    var player = Player.get(function() {
        self.selectedPlayer.email = player.email;
        self.selectedPlayer.fuel = player.fuel;
        self.selectedPlayer.cargo = player.cargo;
        self.selectedPlayer.food = player.food;
    });
}

Thanks!

like image 652
Deyang Avatar asked Apr 06 '12 13:04

Deyang


People also ask

How do you create a service in AngularJS?

Application developers are free to define their own services by registering the service's name and service factory function, with an AngularJS module. The service factory function generates the single object or function that represents the service to the rest of the application.

What method is used to implement a service in AngularJS?

Services are normally injected using the dependency injection mechanism of AngularJS.

What is the difference between factory and service in AngularJS?

The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not. That is why, in the case of a factory, we return an object literal instead of using this.

What is $q in AngularJS?

$q is integrated with the $rootScope. Scope Scope model observation mechanism in AngularJS, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI. Q has many more features than $q, but that comes at a cost of bytes.


2 Answers

I suspect that you are working off version 0.9.19. I would first suggest that you move over to version 1.0rc4. Version 1.0 is close to going live and is stable but it has many breaking changes over version 0.9. You can see the Angular documentation for more information.

In v1.0 you wrap everything in a module: controllers, services, directives and so on. Create a module like so:

var module = angular.module('ModuleName', ['ngResource']);

In version 1.0 the resource service is factored out into its own class, so you have to give it as a dependency. You will also have to include the resource js file in your app.

Now to create a service you can simply use the module API. In this case:

module.factory('Player', ['$resource', function($resource) {
   return $resource('/player/get');}]);

Notice here that the dependency on $resource is injected by angular.

Personally I don't like to mess with the scope inside my service so I would have the following inside my controller:

module.controller('MyController', ['$scope', 'Player', function($scope, Player) {
    $scope.select = function(email) {
        console.log("selecting");
        Player.get({
        email: email
        }, function(player) {
            $scope.selectedPlayer = player;
        });
    };
}]);​

Notice that in v1.0 the scope is also injected into the controller so no using self any more. Also I took the liberty of assuming that the selectedPlayer would only contain the fields from player so I just wrote player straight over the top of selectedPlayer. You could also do it field by field manually or use angular.extend($scope.selectedPlayer, player); to merge the two objects.

Here is a fiddle: http://jsfiddle.net/DukvU/

like image 160
Pete BD Avatar answered Sep 30 '22 11:09

Pete BD


angular.module('myModule', []).
  factory('Player', function($resource) {
    return $resource('/player/get',{email:email});
  });

function MyController($scope, Player) {
   $scope.select = function(email) {
     Player.get(....);
   }
}
like image 40
Misko Hevery Avatar answered Sep 30 '22 11:09

Misko Hevery