Last active
June 1, 2017 11:22
-
-
Save vitaliy-bobrov/fef30b17a142d63914b9 to your computer and use it in GitHub Desktop.
Tutorial App services.js part 4
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
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