-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
116 lines (108 loc) · 3.5 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Vue from "vue"
import get from "lodash.get"
import './utils/polyfill'
const config = {}
const { set, delete: remove } = Vue
const MUTATION = 'datastore/REFRESH_DATASTORE'
const MUTATION_DELETE = 'datastore/DELETE'
let DStore
export default function(_DStore, {
namespace = 'DS',
silent = true,
} = {}) {
DStore = _DStore
if (!DStore) {
console.warn('You must initialize vuex-jsdata-plugin with a DS store object from js-data')
return
}
return function(store) {
const ressources = Object.values(DStore.definitions)
let getters = {}
let moduleState = {}
set(store.state, namespace, {}) // init state
getters[namespace] = (state) => state[namespace] // set global getter
ressources.forEach(({ class: ressourceName }) => {
const key = `${namespace}${ressourceName}`
getters[key] = (state) => state[ressourceName]
set(moduleState, ressourceName, {})
})
const module = {
state: moduleState, // init ressource state
getters,
mutations: {
[MUTATION](state, { type, data }) {
const { id } = data
const namespace = state[type]
set(namespace, id, Object.assign(JSON.parse(JSON.stringify(data)))) // assign to trigger reactivity
},
[MUTATION_DELETE](state, { type, data }) {
const { id } = data
const namespace = state[type]
remove(namespace, id) // assign to trigger reactivity
},
},
}
store.registerModule('DS', module)
function commitRefresh(res, data) {
const commit = instance => {
// set(instance, '__refresh', !instance.__refresh)
store.commit(MUTATION, {
type: res.class,
data: instance,
}, { silent })
}
if (Array.isArray(data)) data.forEach(commit)
else commit(data)
}
function commitDelete(res, data) {
const commit = instance => {
store.commit(MUTATION_DELETE, {
type: res.class,
data: instance,
}, { silent })
}
if (Array.isArray(data)) data.forEach(commit)
else commit(data)
}
ressources.forEach((ressource) => {
ressource.on('Refresh', (res, id) => {
const data = res.get(id)
commitRefresh(res, data)
})
// ressource.on('DS.change', (res, data) => commitRefresh(res, data))
ressource.on('DS.afterDestroy', (res, data) => {
res.off('DS.change')
commitDelete(res, data)
setTimeout(() => { // FIXME
res.on('DS.change', function (res, data) {
commitRefresh(res, data);
});
}, 100)
})
const refreshCb = (res, data) => commitRefresh(res, data)
ressource.on('DS.afterInject', function handler(res, data) {
refreshCb(res, data)
// ressource.off('DS.afterInject', handler)
// ressource.on('DS.change', refreshCb)
})
})
}
}
export function mapRessources(ressources = []) {
function generateGetter(name, key) {
return function getter() {
const id = get(this, key)
if (id === null || id === undefined || !this.$store.state.DS[name][id]) {
console.warn('no ressource with id:' + id)
return undefined
} // !IMPORTANT trigger reactivity
return DStore.get(name, id);
}
}
const ressourceGetters = ressources.reduce((sum, ressource) => {
const getterName = Object.keys(ressource)[0]
ressource[getterName] = generateGetter(...ressource[getterName])
return Object.assign(sum, ressource)
}, {})
return ressourceGetters
}