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
| const CompileUtil = { getVal(vm, expr) { let props = expr.split('.') let value = props.reduce((prev, next) => { return prev[next] }, vm.$data) return value },
getTextVal(vm, expr) { return expr.replace(/\{\{([^}]+)\}\}/g, (...args) => { return this.getVal(vm, args[1]) }) },
setVal(vm, expr, value) { expr = expr.split('.') return expr.reduce((prev, next, currentIndex) => { if (currentIndex == expr.length - 1) { return prev[next] = value; } return prev[next] }, vm.$data) },
text(node, vm, expr) { let updateFn = this.updater['textUpdater'] let value = this.getTextVal(vm, expr) expr.replace(/\{\{([^}]+)\}\}/g, (...args) => { new Watcher(vm, args[1], () => { updateFn && updateFn(node, this.getTextVal(vm, expr)) }) }) updateFn && updateFn(node, value) },
model(node, vm, expr) { let updateFn = this.updater['modelUpdater'] new Watcher(vm, expr, (newVal) => { updateFn && updateFn(node, this.getVal(vm, expr)) }) node.addEventListener('input', (e) => { let newValue = e.target.value this.setVal(vm, expr, newValue) }) updateFn && updateFn(node, this.getVal(vm, expr)) },
updater: { textUpdater(node, value) { node.textContent = value }, modelUpdater(node, value) { node.value = value } } }
|