Skip to content

Instantly share code, notes, and snippets.

@kamalkhan
Created April 16, 2017 12:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamalkhan/5014ab58be5cb1beb97b373a59d8ea64 to your computer and use it in GitHub Desktop.
Save kamalkhan/5014ab58be5cb1beb97b373a59d8ea64 to your computer and use it in GitHub Desktop.
Vue deep model directive
const component = Vue.extend({
template: `<input v-deep-model="'one.two.three'">`,
data() {
return {
one: { two: { three: 'foo' } }
};
}
});
Vue.directive('deep-model', {
bind(el, binding, vnode) {
el.addEventListener('input', e => {
new Function('obj', 'v', `obj.${binding.value} = v`)(vnode.context.$data, e.target.value);
});
},
unbind(el) {
el.removeEventListener('input');
},
inserted(el, binding, vnode) {
el.value = new Function('obj', `return obj.${binding.value}`)(vnode.context.$data);
},
update(el, binding, vnode) {
el.value = new Function('obj', `return obj.${binding.value}`)(vnode.context.$data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment