Skip to content

Instantly share code, notes, and snippets.

@jonasraoni
Created August 7, 2019 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasraoni/0a36dacf8294331bdb7213de18ba87ff to your computer and use it in GitHub Desktop.
Save jonasraoni/0a36dacf8294331bdb7213de18ba87ff to your computer and use it in GitHub Desktop.
Get/set v-model value inside an Vue directive
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
// NOTES:
// 1. The vnode is provided by the directive callbacks
// 2. If eval sounds scary to you, you might replace it by any function that provides "navigation" from a string like "array[0].property" (that's what you get from the "expression") =]
getValue (vnode) {
//standard v-model
if (vnode.data.model) {
return vnode.data.model.value;
}
//component with custom v-model...
return eval(`vnode.context.$data.${vnode.data.directives.find(directive => directive.name === 'model').expression}`);
}
setValue (vnode, value) {
//standard v-model
if (vnode.data.model) {
return vnode.data.model.callback(value);
}
//component with custom v-model
return eval(`vnode.context.$data.${vnode.data.directives.find(directive => directive.name === 'model').expression} = value`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment