Created
March 1, 2013 01:39
-
-
Save Sharondio/5061810 to your computer and use it in GitHub Desktop.
Sample AngularJS service using $http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Services */ | |
var services = angular.module('app.services', []) | |
services.factory( 'Drink', function($http) { | |
// Drink is a class which we can use for retrieving and | |
// updating data on the server | |
var Drink = function(data) { angular.extend(this, data); }; | |
// a static method to retrieve Drink by ID | |
Drink.get = function(drink) { | |
return $http.get('/drinks/' + drink.drinkid).then(function(response) { return response.data; }); | |
}; | |
Drink.list = function() { | |
return $http.get('/drinks/').then(function(response, status) { return response.data; }) | |
}; | |
Drink.save = function( drink ) { | |
if(drink.drinkid) { | |
return $http.put('/drinks/', data ).then(function(response) { return response.data; }); | |
} else { | |
return $http.post('/drinks/', data ).then(function(response) { return response.data; }); | |
} | |
}; | |
Drink.destroy = function( drink ) { | |
return $http.delete('/drinks/' + drink.drinkid).then(function(response) { return response.data; }) | |
} | |
return Drink; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment