Created
April 16, 2017 12:56
-
-
Save kamalkhan/5014ab58be5cb1beb97b373a59d8ea64 to your computer and use it in GitHub Desktop.
Vue deep model directive
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
const component = Vue.extend({ | |
template: `<input v-deep-model="'one.two.three'">`, | |
data() { | |
return { | |
one: { two: { three: 'foo' } } | |
}; | |
} | |
}); |
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
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