-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixin_snippet_vue_method.js
More file actions
49 lines (41 loc) · 1.95 KB
/
mixin_snippet_vue_method.js
File metadata and controls
49 lines (41 loc) · 1.95 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
/*
N. Frick
Usage example for simple_js_cache
this is an entry in the methods section of a VueJS2 viewmodel (global var "vm" here)
(uses vue-resource for $http.post)
the scenario here is recObjL is a partially "populated" record (some fields or columns are missing) and you want to
mix in some other fields (or columns) that were previously saved in the cache
*/
,getRecCacheOrPost: function(recObjL) {
// promise will resolve an object to mixin
return new Promise(function(resolve1,reject1) {
if (!('ID' in recObjL)) reject1({}); // safety check
getCacheStorageP(recObjL.ID).then((rObj) => { // query the cache using record ID
if (Object.keys(rObj).length > 0) { // ( more than zero props in object)
resolve1(rObj); // found in cache!
} else { // not in cache, so get from server
let daPostData = { requestID: recObjL.ID };
this.$http.post('/requestAPIs/qj_sel_request.cfm',daPostData).then(
(response) => {
var recObjAry = response.body;
if (recObjAry.length > 0)
{ var retObj = recObjAry[0];
if ('ID' in retObj) // save to cache using record ID
{ setCacheStorageP(retObj.ID,retObj).then(() => { }); }
resolve1(retObj);
}
else { resolve1({}); }; // don't return null, but empty object
}
,(err) => {
reject1(err); // (there was a POST error)
});
}
});
});
}
/*
( NOTE: getRecCacheOrPost doesn't work to create a Vue computed,
so populate your Vue data object (using Object.assign or Vue.set() )
by assigning vue data within the .then resolving function )
or use Observables or something
*/