Last active
January 29, 2017 22:21
-
-
Save lewisrodgers/5358702407df24fc3c97c0bef29230b5 to your computer and use it in GitHub Desktop.
Boilerplate service for getting 1 or all records of a type.
This file contains hidden or 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
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