Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of $http improvements #23

Open
ThomasBurleson opened this issue Apr 17, 2014 · 1 comment
Open

Use of $http improvements #23

ThomasBurleson opened this issue Apr 17, 2014 · 1 comment

Comments

@ThomasBurleson
Copy link

Your do not need to wrap $http() calls with $q.defer() usages.

var find = function () {
            var deferred = $q.defer();
            var url = ENDPOINT_URI + 'clients/' + AuthService.getCurrentUserId() + '/stories.json';

            $http.get(url).success(deferred.resolve).error(deferred.reject);

            return deferred.promise;
        };

And consolidate all URLs into a url map. Consider this result:

var URLS = {
      STORY : {
          findAll           : "{0}/clients/{1}/stories.json"
        , loadStoryDetails  : "{0}/clients/{1}/stories/{2}.json"
      }
    };

function findAllStories( ) 
{
  return $http.get( supplant( 
    URLS.STORY.findAll, [ ENDPOINT_URI, AuthService.getCurrentUserId() ]
  ));
}

function loadStory( storyID ) 
{
  return $http.get( supplant( 
    URLS.STORY.loadStory, [ ENDPOINT_URI, 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.

/**
 * 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
Projects
None yet
Development

No branches or pull requests

1 participant