Skip to content

Instantly share code, notes, and snippets.

@deadmann
Forked from vladikoff/resource.js
Last active April 4, 2016 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deadmann/1337e8f6229a38d8cc67e882a20a1613 to your computer and use it in GitHub Desktop.
Save deadmann/1337e8f6229a38d8cc67e882a20a1613 to your computer and use it in GitHub Desktop.
Angular $resource and transformResponse
angular.module('itemServices', ['ngResource'])
.factory('Item', ['$resource',
function ($resource) {
return $resource('items/:id',
{id: '@id'},
{
query: {
isArray: true,
method: 'GET',
params: {},
transformResponse: function (data) {
var wrapped = angular.fromJson(data);
//If contains exceptions, it doesn't contains data and and only errors, and normally should return directly without modification
if (!wrapped.ExceptionType && !wrapped.ExceptionMessage) {
return wrapped.body.rows
}
return wrapped;
}
},
get: {method: 'GET', params: {id: '@id'}},
save: {method: 'POST'},
update: {method: 'PUT', params: {id: '@id'}},
delete: { method: 'DELETE', params: {} }
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment