Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

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

  });
}]);

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;
}
app.config([ "$resourceProvider", function($resourceProvider) {

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

}]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top