Tomas Lucena
Tomas Lucena

Reputation: 1526

Vue, wait data is assign to run a function

I have a method in my Vue instance that do the following:

submitForm(confirmation) {
            //set price confirmation
            this.price_confirmation = confirmation

            //proceed
            var form = this.getForm()
}

Price confirmation is the v-model of an input.

Then the method getForm serialize (with jquery) the form. The thing is that my form is being serialized before this.price_confirmation = confirmation is run.

How can I run this.getForm() after Vue assign the data?

Upvotes: 0

Views: 670

Answers (2)

ravi kumar
ravi kumar

Reputation: 1620

You probably need to use the nextTick method to wait until the next update cycle:

submitForm(confirmation) {
    //set price confirmation
    this.price_confirmation = confirmation

    //proceed
    this.$nextTick(() => {
        var form = this.getForm();
    });
}

Upvotes: 1

hatef
hatef

Reputation: 6199

From what you've shared, it's not clear how the confirmation is set in your component and at what point the submitForm is called. submitForm method seems to be correct, and should work correctly (i.e. this.getForm() is called after this.price_confirmation is set). What you could do is, look where the confirmation variable is set and add an async function to it. e.g:

async confirmation() {
    await getConfirmations();
}

If you need more help please share the relevant code from your component.

Upvotes: 0

Related Questions