Last active
July 12, 2019 19:37
-
-
Save horaciosystem/e26d6a8ee212095ff763340e6561c3a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue' | |
export function createWatchersForDependentFields({ | |
dependentFields, | |
form, | |
vm | |
}) { | |
return dependentFields.map(it => { | |
console.log('watcher for', `form.${it.field}`) | |
return vm.$watch( | |
`form.${it.field}`, | |
function() { | |
it.dependents.forEach(({ field, resetValue }) => { | |
if (!field.includes('.')) { | |
Vue.set(form, field, resetValue()) | |
} else { | |
let paths = field.split('.') | |
let lastPath = paths.slice(-1) | |
let initialPath = paths.slice(0, -1) | |
Vue.set(form[initialPath], lastPath, resetValue()) | |
} | |
}) | |
} | |
) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment