-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.js
More file actions
83 lines (73 loc) · 2.67 KB
/
services.js
File metadata and controls
83 lines (73 loc) · 2.67 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
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
var appServices = angular.module('anglpointApp.services', []);
/* provides data (as promise) to requesting controller
@ function: getData(list name, relative site URL, cache flag, cache reference)
returns promise object
*/
appServices.service('DataService', ['$q', '$http', 'CacheService', function($q, $http, CacheService) {
this.getData = function(name, site, cache, url) {
var updated = $q.defer(); // updated date
var flag = $q.defer(); // flag for cache update
var data = $q.defer(); // results of operation
ExecuteOrDelayUntilScriptLoaded(function() {
var ctx = new SP.ClientContext(site);
var web = ctx.get_web();
var list = web.get_lists().getByTitle(name);
ctx.load(list);
ctx.executeQueryAsync( function() {
var dateUpdated = Date.parse(list.get_lastItemModifiedDate());
var dateStored = CacheService.getCache(url + "-updated");
updated.resolve(dateUpdated);
// determine if situation is favorable to retrieve cached data
if(cache && CacheService.getCache(url + "-data") && (dateStored - dateUpdated) == 0) {
// cache meets criteria
flag.resolve(false);
data.resolve(JSON.parse(CacheService.getCache(url + "-data")));
} else {
// no caching or data out-of-date, de facto operation
flag.resolve(true);
data.resolve($http.get(url));
}
}, function() {
// error handling for failed async operation
alert("something went horribly wrong! :(");
});
}, 'sp.js');
return [data.promise, updated.promise, flag.promise];
}
}]);
/* provides caching operations to requesting service
@ function: getCache(key)
return value for key
@ function: setCache(key, value)
no return
*/
appServices.service('CacheService', [function() {
this.getCache = function(key) {
return localStorage.getItem(key);
}
this.setCache = function(key, value) {
localStorage.setItem(key, value);
}
}]);
/* fills data into an array, cache if enabled
@ function: fillData(data object, cache flag, cache reference, data date, data validity)
return promise resolving to data array
*/
appServices.service('FillService', ['$q', 'CacheService', function($q, CacheService) {
this.fillData = function(data, cache, ref, date, flag) {
// iterate over data and fill array and cache if enabled
var arr = [];
var dpromise = $q.defer();
for (var i = 0; i < data.data.d.results.length; i++) {
arr.push(data.data.d.results[i]);
}
// cache data if flag has been set
if (cache && flag) {
CacheService.setCache(ref + "-updated", date); // store date for reference
CacheService.setCache(ref + "-data", JSON.stringify(data)); // store data
}
dpromise.resolve(arr);
return dpromise.promise;
}
}]);