Discussion icon indicating copy to clipboard operation
Discussion copied to clipboard

How to check if a form has values for every input on every input change and then trigger a method?

Open noahmatisoff opened this issue 9 years ago • 5 comments

I have a form that will be filled out by a user. Once the user has completed the form, I want to send an HTTP request to my API to get some data.

I'd prefer not to use v-on:click and include a button in the form, because the user may change around the inputs multiple times to see new data (and trigger the method with sends the HTTP request again), and I think it's a bad user experience to repeatedly click a button to get the new data.

How can I watch each individual input for changes and if there's a value for all of the inputs then send the HTTP request? I already have it implemented, but with a button to trigger the request, and that is what I'm trying to move away from.

noahmatisoff avatar Dec 14 '15 18:12 noahmatisoff

try using $watch on each individual data.

Twiknight avatar Dec 15 '15 01:12 Twiknight

Hey @Twiknight thanks, could you show me an example in code?

noahmatisoff avatar Dec 15 '15 05:12 noahmatisoff

@matisoffn http://vuejs.org/api/#watch

Twiknight avatar Dec 15 '15 13:12 Twiknight

So it works great with the code below:

watch: {
 'my.key': {
    handler: function(val, oldVal) {

    },
    deep: true
  }
}

However... The values of val and oldVal are identical when logging them to the console. Shouldn't oldVal be one step behind? Any idea what would cause this?

noahmatisoff avatar Dec 16 '15 16:12 noahmatisoff

Hi!

This is indeed documented behaviour: Vue.JS will only provide the old value for primitive data-types. For complex types Vue would have to copy the whole object before mutation, to give you the old state - this could get very expensive for big objects. - So you have to save the original value yourself (e.g. via JSON.stringify)

falco467 avatar Sep 14 '17 13:09 falco467