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?
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;
}
}
});
}]);
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);
}
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With