Last active
August 29, 2015 14:18
-
-
Save aysbg/ff8a1792047d4a4a0ccd to your computer and use it in GitHub Desktop.
http example
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
(function () { | |
'use strict'; | |
var UserService = [ | |
'$http', '$q', | |
function ($http, $q) { | |
return { | |
getOne: getOne | |
}; | |
// Get one user | |
function getOne(userId) { | |
var request = $http({ | |
method: 'GET', | |
url: Config.apiRoute + '/users/' + userId | |
}); | |
return request.then( handleSuccess, handleError ); | |
} | |
// Private methods | |
// ------------------------- | |
// Handle error part of promise callback | |
function handleError(response) { | |
// if response is not object or response object does not have message param | |
if (!angular.isObject(response) || !response.message) { | |
// return unknown eror | |
return $q.reject('An unknown error occurred.'); | |
} | |
// Otherwise, use expected error message. | |
return $q.reject( response.message ); | |
} | |
// Handle success part of promise callback | |
function handleSuccess(response) { | |
return response.data; | |
} | |
} | |
]; | |
angular.module('yourApp').factory('UserService', UserService); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment