Adding PUT to default NG-Resource Actions in AngularJS
Asked Answered
G

4

5

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?

Grizelda answered 5/1, 2014 at 16:33 Comment(0)
N
6

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);
  }
}]);
Nares answered 5/1, 2014 at 16:46 Comment(3)
And then use $myResouce instead of $resource, I'm assuming?Grizelda
Yes, then use $myResource, but the API remains the sameNares
@Magne The only reason I can imagine is the conflict over which HTTP method to use for update (PUT vs PATCH)Buckshee
T
11

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;
      }
    }

  });
}]);
Truong answered 23/1, 2015 at 20:31 Comment(0)
N
6

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);
  }
}]);
Nares answered 5/1, 2014 at 16:46 Comment(3)
And then use $myResouce instead of $resource, I'm assuming?Grizelda
Yes, then use $myResource, but the API remains the sameNares
@Magne The only reason I can imagine is the conflict over which HTTP method to use for update (PUT vs PATCH)Buckshee
F
2

In your application configuration function, customize the $resourceProvider to add your own set of http actions parameter, just like so:

    angular.module('app')
           .config(configureResourceProvider);

    function configureResourceProvider($resourceProvider){

    // Provide your own set of actions on $resource factory.
    // The following comments are Angular's default actions which are being
    // replaced by your customized set that includes a PUT method.
    //{ 'get':    {method:'GET'},
    //  'save':   {method:'POST'},
    //  'query':  {method:'GET', isArray:true},
    //  'remove': {method:'DELETE'},
    //  'delete': {method:'DELETE'} };

    $resourceProvider.defaults.actions = {
        create: {method: 'POST'},
        save:   {method: 'POST'},
        update: {method: 'PUT'},
        get:    {method: 'GET'},
        query:  {method: 'GET', isArray:true},
        remove: {method: 'DELETE'},
        delete: {method: 'DELETE'}
    };

    // Of course, you can customize other parameters too, like: 
    // Don't strip trailing slashes from calculated URLs
    $resourceProvider.defaults.stripTrailingSlashes = false;
}
Fineness answered 12/5, 2015 at 17:7 Comment(0)
W
2
app.config([ "$resourceProvider", function($resourceProvider) {

  $resourceProvider.defaults.actions['update'] = { method: 'PUT', params: { id: '@id' }, isArray: false }

}]);
Whiffen answered 20/7, 2015 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.