Skip to content

Instantly share code, notes, and snippets.

@horaciosystem
Last active July 12, 2019 19:37
Show Gist options
  • Save horaciosystem/e26d6a8ee212095ff763340e6561c3a4 to your computer and use it in GitHub Desktop.
Save horaciosystem/e26d6a8ee212095ff763340e6561c3a4 to your computer and use it in GitHub Desktop.
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