Skip to content

Instantly share code, notes, and snippets.

@marbiano
Last active December 28, 2015 01:29
Show Gist options
  • Save marbiano/7420787 to your computer and use it in GitHub Desktop.
Save marbiano/7420787 to your computer and use it in GitHub Desktop.
Angular $http & $resource
// With $http:
myAppModule.factory('CreditCard', ['$http', function($http) {
var baseUrl = '/user/123/card';
return {
get: function(cardId) {
return $http.get(baseUrl + '/' + cardId);
},
save: function(card) {
var url = card.id ? baseUrl + '/' + card.id : baseUrl;
return $http.post(url, card);
},
query: function() {
return $http.get(baseUrl);
},
charge: function(card) {
return $http.post(baseUrl + '/' + card.id, card, {params: {charge: true}});
}
};
}]);
// With $resource:
myAppModule.factory('CreditCard', ['$resource', function($resource) {
return $resource('/user/:userId/card/:cardId',
{userId: 123, cardId: '@id'},
{charge: {method:'POST', params:{charge:true}, isArray:false});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment