-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.js
More file actions
61 lines (55 loc) · 1.5 KB
/
basic.js
File metadata and controls
61 lines (55 loc) · 1.5 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
import Controller from '../lib/ez-ctrl';
function delay(milli) {
return new Promise( (resolve)=> setTimeout(resolve, milli) );
}
export var middleware = {};
export default class BasicController extends Controller {
async beforeResponse() {await this.callMiddleWare('beforeResponse'); }
async initialize() {await this.callMiddleWare('initialize'); }
async afterGetData() {await this.callMiddleWare('afterGetData'); }
async afterSuccess() {await this.callMiddleWare('afterSuccess'); }
async onServerError(error) {
throw error;
}
async callMiddleWare(name) {
await delay(5);
if(middleware[name + 'MainDate']) throw new Error('Already ran ' + name);
middleware[name + 'MainDate'] = Date.now();
}
};
BasicController.modelName = 'Basic';
BasicController.defineRoutes({
getRawValue: {
logic() {
return 5;
}
},
getOtherAttribute: {
otherAttribute: 'otherAttribute',
logic() {
return this.routeDetails.otherAttribute;
}
},
acceptArguments: {
logic(a) {
return a + 5;
}
},
// if logic is the only attribute, you should be able to use it.
resolvePromise(a) {
return delay(100).then( ()=> a + ' dog');
},
personalLifecycle: {
beforeResponse: setNow('beforeResponse'),
initialize: setNow('initialize'),
afterSuccess: setNow('afterSuccess'),
afterGetData: setNow('afterGetData'),
logic() {}
}
});
function setNow(attr) {
return async function() {
await delay(5);
middleware[attr + 'Date'] = Date.now();
};
}