Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Created April 24, 2014 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/11272344 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/11272344 to your computer and use it in GitHub Desktop.
Using URLLookups with <xxx>Services to centralize all RESTful dataservice urls.
/**
* 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 ]
));
}
}
@ThomasBurleson
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment