Skip to content

Instantly share code, notes, and snippets.

@lewisrodgers
Last active January 29, 2017 22:21
Show Gist options
  • Save lewisrodgers/5358702407df24fc3c97c0bef29230b5 to your computer and use it in GitHub Desktop.
Save lewisrodgers/5358702407df24fc3c97c0bef29230b5 to your computer and use it in GitHub Desktop.
Boilerplate service for getting 1 or all records of a type.
angular
.module('app')
.service('MyService', myService);
function myService($http) {
/**
* First, send a get request for all records of a type,
* then filter response down to specific record.
*/
this.getUser = function(id) {
return this.getAllUsers().then(function(users) {
return users.find(function(user) {
return user.id === id;
}
}
}
this.getAllUsers = function() {
return $http.get('/users').then(function(resp) {
return resp.data;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment