Created
April 24, 2014 22:57
-
-
Save ThomasBurleson/11272344 to your computer and use it in GitHub Desktop.
Using URLLookups with <xxx>Services to centralize all RESTful dataservice urls.
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
/** | |
* URLS before prefixing with ENDPOINT | |
* | |
* var URLS = { * | |
* STORY : { | |
* FIND_ALL : "/clients/{1}/stories.json" | |
* , LOAD_STORY : "/clients/{1}/stories/{2}.json" | |
* } | |
* USER : { | |
* LOAD_ALL : "clients/{0}/users.json" | |
* } | |
* }; | |
*/ | |
// Register the StoryService with AngularJS | |
myModule.factory( | |
'storyService', | |
[ 'authService', 'serviceURLs', '$http', StoryService ] | |
); | |
function StoryService( authService, serviceURLs, $http ) | |
{ | |
// Published `promise-returning` API | |
return { | |
findAllStories : findAllStories, | |
loadStory : loadStory | |
} | |
// ********************************** | |
// Internal Methods | |
// ********************************** | |
function findAllStories( ) | |
{ | |
return $http.get( supplant( | |
serviceURLs.STORY.FIND_ALL, [ AuthService.getCurrentUserId() ] | |
)); | |
} | |
function loadStory( storyID ) | |
{ | |
return $http.get(supplant( | |
serviceURLs.STORY.LOAD_STORY, [ AuthService.getCurrentUserId(), storyID ] | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you externalize all dataservice endpoint/urls into a
serviceURLs
factory, you can inject this service map instead of injecting the flat ENDPOINT_URL constant. You could also configure your ENDPOINT_URL during config() time and update all urls in the serviceURLs instance before the service map is injected in other Services.