ThomasBurleson
4/24/2014 - 10:57 PM

Using URLLookups with <xxx>Services to centralize all RESTful dataservice urls.

Using URLLookups with 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 ]
    ));
  }
}