0

Folks,

I am having a hard time understanding how to write a resource in AngularJs. I have currently written a factory method as below:

var baseUrl = "http://www.myBaseUrl.com/cgi-bin/angular-portal/backend.pl";

   myApp.factory('Employee',['$http',function($http){
var employeeUrl = baseUrl + "?getemployeelist";
return{
query: function(){
    return $http.get(employeeUrl);
},
get: function(empId) {
    return $http.get(employeeUrl + '=' + empId)
}
}; 
]);

How do I include a POST call in this ? Currently I am making a POST call directly in the controller and as I understand this is not the way to do it:

function EmployeeAddCtrl($scope,Employee,$routeParams) {
// Add a new employee
$scope.addOrUpdate = function() {

var jsonString = angular.toJson($scope.employee);

    //Make the REST call
    $http.post(postUrl,jsonString)
        .success(function(data,status,headers,config) {
    // show success message here
         })
        .error(function(data,status,headers,config){
    // DO SOMETHING HERE.. FIGURE LATER         
        }); 

}
3
  • I don't get what the problem is exactly? You can call $http.post just like you do $http.get, but include data as the second parameter. In terms of using either of these from a controller and having them live in a service, you have two options as far as I can tell, either $watch from the controller on the services variables and update stuff in the scope, or get a promise from the service and use it within the scope (I believe this is the right way going forward and promises can somehow be used right the way down, via chaining). Commented Jul 15, 2013 at 19:07
  • You are right. I was able to simply include $http.post and it worked. However what I was looking for was an understanding of how to convert this into a resource Commented Jul 16, 2013 at 14:06
  • Using the $resource service is recommended for RESTful server implementations, if you have a RESTful service then $resource creates a higher abstraction above the $http service to deal with using the appropriate HTTP verbs and paths for interacting with the service. Commented Jul 16, 2013 at 18:15

1 Answer 1

2

Use Angular Resource

http://docs.angularjs.org/api/ngResource.$resource

this way you can do something like this in a service:

mainApp.factory('ObjectService', function($resource,$location) {

    var Object = $resource('/admin/object/:id',{id:'@id'},
        {update: {method:'PUT', params:{id:'@id'}}}
    );

    Object.redirect = function(data){
        $location.path('/index');
    };

    Object.fail = function (e){
        alert('Alert Something went wrong')
    };
    Object.new = function(){
        return new Object();
    }
    return Object;
});

And then Call it on a controller like this:

function PromocionController($scope,$routeParams,ObjectService){

    if($routeParams.id){
        $scope.object = ObjectService.get({id:$routeParams.id});
        $scope.save = function(){$scope.object.$update({id:$scope.object.id},ObjectService.redirect,ObjectService.fail)};
    } else {
        $scope.object = ObjectService.new();
        $scope.save = function(){$scope.object.$save({id:$scope.object.id},ObjectService.redirect,ObjectService.fail)};
    }

    $scope.delete = function(){$scope.object.$remove({id:$scope.object.id},ObjectService.redirect,ObjectService.fail)};

}

Please note 2 things. 1) You could make this a lot simpler I made custom methods like the update method and added parameters to the delete and save method to make it work with Laravel Resource Controller. 2) I added the fail and redirect functions in the service just for reusability purposes, if you want a simpler example please read this: AngularJS $resource RESTful example

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.