-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathvuex-orm-axios.js
71 lines (61 loc) · 2.11 KB
/
vuex-orm-axios.js
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
import map from 'lodash/map';
import Context from './common/context';
import Action from './actions/Action'
import Fetch from './actions/Fetch'
import Get from './actions/Get'
import Create from './actions/Create'
import Update from './actions/Update'
import Delete from './actions/Delete'
export default class VuexOrmAxios {
/**
* @constructor
* @param {Components} components The Vuex-ORM Components collection
* @param {Options} options The options passed to VuexORM.install
*/
constructor(components, options) {
Context.setup(components, options);
this.setupActions();
this.setupModels();
}
/**
* This method will setup following Vuex actions: $fetch, $get, $create, $update, $delete
*/
setupActions () {
const context = Context.getInstance();
context.components.Actions.$fetch = Fetch.call.bind(Fetch);
context.components.Actions.$get = Get.call.bind(Get);
context.components.Actions.$create = Create.call.bind(Create);
context.components.Actions.$update = Update.call.bind(Update);
context.components.Actions.$delete = Delete.call.bind(Delete);
}
/**
* This method will setup following model methods: Model.$fetch, Model.$get, Model.$create,
* Model.$update, Model.$delete
*/
setupModels () {
const context = Context.getInstance();
/**
* Transform Model and Modules
*/
map(context.database.entities, entity => {
entity.module = Action.transformModule(entity.module);
entity.model = Action.transformModel(entity.model);
return entity;
});
context.components.Model.$fetch = function (config = {}) {
return this.dispatch('$fetch', config);
};
context.components.Model.$get = function (config = {}) {
return this.dispatch('$get', config);
};
context.components.Model.$create = function (config = {}) {
return this.dispatch('$create', config);
};
context.components.Model.$update = function (config = {}) {
return this.dispatch('$update', config);
};
context.components.Model.$delete = function (config = {}) {
return this.dispatch('$delete', config);
};
}
}