question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature request] Add a reference to the v-model in the directive hook signature

See original GitHub issue

Currently with Vue 2, when you use a directive on an element, you only have 2 options that I know of that allows you to update the v-model foo from within the directive bind functions (and other hooks).

The first option is to modify your Html template and pass the v-model name as a parameter, like :

<input type="text" v-my-directive="'foo'" v-model.number="foo">
//Then use that inside the directive hooks with :
vnode.context[bindings.value] = 42;

The problem with this approach is that first it’s not DRY, the v-model name is right there, why repeat it? It’s also error-prone, too verbose and more importantly, poses difficulties when used in a v-for loop.

The second option, far better in my opinion, is to keep the Html simple and semantic :

<input type="text" v-my-directive v-model.number="foo">

All you have to do then is accessing the v-model name via the vnode.data.directives variable which lists all the directives attached to the element, v-model being one of them. From there you have to iterate on that array until you find the model named directive. Lastly, you can access the v-model name in its expression attribute.

I created some helpers function to do just that ;

export function findVModelName(vnode) {
    return vnode.data.directives.find(function(o) { //Search the v-model name attached to the element
        return o.name === 'model';
    }).expression;
}

export function setVModelValue(value, vnode) {
    vnode.context[findVModelName(vnode)] = value;
}

This works well also in v-for loops, or when you do not know the v-model name beforehand.

My feature request would simply by to modify all the directive hook function signatures to add a reference to the v-model object. The new signature would be like :

bind(el, bindings, vnode, oldVnode, vModel) {}

//you would then be able to modify the v-model attached to the element the directive is declared on just by using :
vModel = 42; //Doing that would automatically update the v-model accordingly

I think that would help a lot, what do you think?

You can see an example where I needed to access the v-model in the following forum thread

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
LinusBorgcommented, Nov 1, 2016

This could be achieved in 1.0 directives with this.set(), but the decision to remove it was intentional.

In 2.0, directives are meant to be used for low-level DOM access only. Things like meddling with v-model functionality should explicitly not be their job anymore.

In most cases where you want to do that with a directive, You should rather use a component instead.

If you have a specific situation that you need help with, we’re happy to help you out on forum.vuejs.org

0reactions
LinusBorgcommented, Nov 3, 2016

Also about the v-model reference in the directive hook signatures, I imagine you could just set vModel to null by default when there are no v-model attached to an element on which the directive is declared though.

I was thinking about redusability, and compatibility.

You would often be in a situation where you start with using v-model, and later (have to) refactor to use an @event instead -. At which point all those directives relying on the v-model expression would stop working

Read more comments on GitHub >

github_iconTop Results From Across the Web

What you should know about Vue v-model - LearnVue
Vue v-model is a directive that creates a two-way data binding between a value in our template and a value in our data...
Read more >
API - Vue.js
Assign a handler for uncaught errors during component render function and watchers. The handler gets called with the error and the Vue instance....
Read more >
How To Use Built-In and Custom Directives in Vue.js
In this tutorial, you will try out some of the most common built-in Vue directives, such as v-if , v-show , v-on ,...
Read more >
@vue/reactivity | Yarn - Package Manager
compiler/v-model: catch incorrect v-model usage on prop bindings (001184e), closes #5584; custom-elements: also dispatch hyphenated version of emitted ...
Read more >
Update Vue and related dependencies (major) - autoclosed
An error occurred while retrieving approval data for this merge request. Update Vue and related dependencies (major) - autoclosed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found