Skip to content

Instantly share code, notes, and snippets.

@vitaliy-bobrov
Last active June 1, 2017 11:22
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 vitaliy-bobrov/fef30b17a142d63914b9 to your computer and use it in GitHub Desktop.
Save vitaliy-bobrov/fef30b17a142d63914b9 to your computer and use it in GitHub Desktop.
Tutorial App services.js part 4
angular.module('starter.services', [])
.factory('Categories', ['$http', '$q', 'config', function($http, $q, config) {
//Endpoints Variables.
var categoriesEndpoint = 'categories',
categoryEndpoint = 'category/';
return {
/*
* Return All Categories.
*/
all: function() {
var defer = $q.defer();
$http({
method : 'GET',
url : config.serviceBaseUrl + categoriesEndpoint,
dataType : 'json',
})
.success(function(data, status, headers, config) {
defer.resolve(data);
})
.error(function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
},
/*
* Return Category by ID.
*/
get: function(catId, page) {
var defer = $q.defer();
$http({
method : 'GET',
url : config.serviceBaseUrl + categoryEndpoint + catId,
dataType : 'json',
params : {
page: page,
},
})
.success(function(data, status, headers, config) {
defer.resolve(data);
})
.error(function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
}
}
}])
.factory('Articles', ['$http', '$q', 'config', function($http, $q, config) {
//Endpoints Variables.
var blogEndpoint = 'articles',
articleEndpoint = 'article/';
return {
/*
* Return All Articles.
*/
all: function(page) {
var defer = $q.defer();
$http({
method : 'GET',
url : config.serviceBaseUrl + blogEndpoint,
dataType : 'json',
params : {
page: page,
},
})
.success(function(data, status, headers, config) {
defer.resolve(data);
})
.error(function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
},
/*
* Return Article by ID.
*/
get: function(articleId) {
var defer = $q.defer();
$http({
method : 'GET',
url : config.serviceBaseUrl + articleEndpoint + articleId,
dataType : 'json',
})
.success(function(data, status, headers, config) {
defer.resolve(data);
})
.error(function(data, status, headers, config) {
defer.reject(data);
});
return defer.promise;
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment