-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (44 loc) · 1.21 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
const diff = require('js-deep-diff')
function calculate (conf) {
const computedValues = {}
const computed = conf.computed || {}
Object.keys(computed).forEach(key => {
// if (conf.data[key]) {
// console.error(`data[${key}] already exist!`)
// return
// }
computedValues[key] = computed[key].call(conf, conf)
})
return computedValues
}
module.exports = function Voila (conf) {
/**
* conf
* {
* computed: {
* computedValue () {
* return this.data.value + 1
* }
* }
* }
*/
return Page ({
...conf,
...calculate(conf),
$setData (data) {
const newData = {}
const differences = diff(this.data, {
...data,
...calculate(this.data)
})
differences.forEach(state => {
if (state.type === 'EDIT' || state.type === 'ADD') {
newData[state.path.reduce((prev, next) => {
return prev + `[${next}]`
}, '')] = state.rhs
}
})
this.setData(newData)
}
})
}