Created
August 7, 2019 07:21
-
-
Save jonasraoni/0a36dacf8294331bdb7213de18ba87ff to your computer and use it in GitHub Desktop.
Get/set v-model value inside an Vue directive
This file contains 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
//+ 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