forked from michaelbromley/angular-wordpress-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogService.js
More file actions
70 lines (61 loc) · 1.98 KB
/
Copy pathBlogService.js
File metadata and controls
70 lines (61 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* The BlogService retrieves and processes the json response from WP-API into a form that Angular can use.
*
* @param $http
* @param $sce
* @param config
* @returns {{allPosts: allPosts, allPostsByTag: allPostsByTag, allPostsBySearchTerm: allPostsBySearchTerm, featuredPosts: featuredPosts, post: post}}
* @constructor
*/
function BlogService($http, $sce, config) {
function allPosts() {
return getData('posts?filter[category_name]=post');
}
function allPostsByTag(tag) {
return getData('posts?filter[category_name]=post&filter[tag]=' + tag);
}
function allPostsBySearchTerm(searchTerm) {
return getData('posts?filter[category_name]=post&filter[s]=' + searchTerm);
}
function featuredPosts() {
return getData('posts?filter[category_name]=post%2Bfeatured');
}
function post(id) {
return getData('posts/' + id);
}
function getData(url) {
return $http
.get(config.API_URL + url, { cache: true })
.then(function(response) {
if (response.data instanceof Array) {
var items = response.data.map(function(item) {
return decorateResult(item);
});
return items;
} else {
return decorateResult(response.data);
}
});
}
/**
* Decorate a post to make it play nice with AngularJS
* @param result
* @returns {*}
*/
function decorateResult(result) {
result.excerpt = $sce.trustAsHtml(result.excerpt);
result.date = Date.parse(result.date);
result.content = $sce.trustAsHtml(result.content);
return result;
}
return {
allPosts: allPosts,
allPostsByTag: allPostsByTag,
allPostsBySearchTerm: allPostsBySearchTerm,
featuredPosts: featuredPosts,
post: post
};
}
angular
.module('app')
.factory('BlogService', BlogService);