Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding PUT to default NG-Resource Actions in AngularJS

I am trying to add PUT to the default methods in ng-resource. So far I modified the DEFAULT_ACTIONS to:

var DEFAULT_ACTIONS = {
      'get':    {method:'GET'},
      'save':   {method:'POST'},
      'update':   {method:'PUT'},
      'query':  {method:'GET', isArray:true},
      'remove': {method:'DELETE'},
      'delete': {method:'DELETE'}
    };

But this feels very hacky and obviously will not persist when I update the module. Is there a way that I can add update/put to all ng-resource objects that will persist with updates?

like image 931
Devin Dixon Avatar asked Jan 05 '14 16:01

Devin Dixon


2 Answers

Another option is to configure the $resourceProvider. This will take effect on ALL $resource's and you'll need to include this code in your tests as well, most likely.

// Config the $resourceProvider
app.config(["$resourceProvider",function ($resourceProvider) {

  // extend the default actions
  angular.extend($resourceProvider.defaults.actions,{

    // put your defaults here
    query : {
      method : "GET",
      isArray : false,
      transformResponse : function (data) {
        // my data is wrapped in an object under the property "results"
        return angular.fromJson(data).results;
      }
    }

  });
}]);
like image 57
robert Avatar answered Sep 19 '22 06:09

robert


The only simple way I can see is to create a wrapper around $resource:

module.factory('$myResource', ['$resource', function($resource){
  return function(url, paramDefaults, actions){
     var MY_ACTIONS = {
       'update':   {method:'PUT'}
     };
     actions = angular.extend({}, MY_ACTIONS , actions);
     return $resource(url, paramDefaults, actions);
  }
}]);
like image 24
Ilan Frumer Avatar answered Sep 21 '22 06:09

Ilan Frumer