Created
December 23, 2014 13:59
-
-
Save cyclopslabs/0194d2c454a234494448 to your computer and use it in GitHub Desktop.
Angular Service Template
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'; | |
angular | |
.module('app.component.nameOfComponent') | |
.factory('sampleAngularService', sampleAngularService); | |
/** | |
* @ngdoc service | |
* @name app.component.nameOfComponent.services:sampleAngularService | |
* | |
* @requires $http | |
* @requires $q | |
* @requires $log | |
* @requires core.login.services:userFactory | |
* @requires core.tools.constants:apiEndpoint | |
* | |
* @description | |
* #sampleAngularService Service | |
* This interacts with the backend API to return data | |
**/ | |
sampleAngularService.$inject = ['$http', '$q','$log','userFactory', 'apiEndpoint']; | |
function sampleAngularService($http, $q, $log,userFactory, apiEndpoint) { | |
var service = { | |
getDataA: getDataA, | |
getDataB: getDataB | |
}; | |
return service; | |
//////// | |
function getDataA() { | |
var dfd = $q.defer(); | |
$http.post(apiEndpoint.dataAUrl, { | |
// post object | |
session_id: userFactory.getSessionId() | |
}) | |
.success(function(data) { | |
// assigning the data to a name that we use in the controller | |
dfd.resolve({ | |
// if we don't need the full dataload that is returned we can do this: | |
smartNameOfData: data.smallerSubsetOfReturnedData | |
}); | |
//this might be something that we should do in the controller, but can be done here | |
//depending on how we intend to expose the service, and how it's being returned. | |
//**This would indicate that the endpoint isn't RESTful. :) | |
/* | |
dfd.resolve({ | |
someData : data.subsetPartOne, | |
otherData: data.fooSubset, | |
barData: data.barSubsetOfData | |
}) | |
*/ | |
}) | |
.error(function(msg, code) { | |
//in the event that there is an error we just want to return the reason. | |
dfd.reject(msg); | |
// angular's logging of the error and the associated server code. | |
$log.error(msg, code); | |
}); | |
return dfd.promise; | |
} | |
function getDataB() { | |
var dfd = $q.defer(); | |
$http.post(apiEndpoint.dataBUrl, { | |
// post object | |
session_id: userFactory.getSessionId() | |
}) | |
.success(function(data) { | |
// this time we just resolve the entire data set | |
dfd.resolve(data); | |
//we could also do this: | |
/* | |
dfd.resolve(data.smallerSubsetOfReturnedData); | |
*/ | |
}) | |
.error(function(msg, code) { | |
//in the event that there is an error we just want to return the reason. | |
dfd.reject(msg); | |
// angular's logging of the error and the associated server code. | |
$log.error(msg, code); | |
}); | |
return dfd.promise; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment