vue.js function executes before the completion of the prior function

Having an issue with submitting a form using the following function:

methods: {
  submit_form: function () {
    this.drawing = dat.toImage();
    this.$refs.my_form.submit()
  }
}

The drawing variable is supposed to be assigned to an input within the my_form like so:

<input type="text" name="doodle" :value="drawing">

Unfortunately, it seems that the form is being submitted before this.drawing is updated with the value of dat.toImage(). Splitting the function into two separate methods seems to solve the issue:

methods: {
  submit_form: function () {
    this.$refs.my_form.submit()
  },
  set_drawing: function () {
    this.drawing = dat.toImage()
  }
}

Executing set_drawing() and then submit_form() in sequence works correctly. It appears that combining them in a single function causes the form to be submitted prior to the update of this.drawing.

Any suggestions on why this behavior occurs and how to resolve it?

UPDATE:

The workaround provided does not seem to work either, as the form is still submitted with a null value. Setting up a wait condition also did not yield the desired result:

data: {
  drawing: null
}
methods: {
  submit_form: function () {
    this.drawing = dat.toImage();
    while (this.drawing == null) {
      false
    }
    this.$refs.my_form.submit()
  }
}

It appears that regardless of the initial value of drawing, that same value gets sent with the form. Even though I can see the value changing on the website upon submission, the loop never ends.

Note that the .toImage method is from the atrament.js library.

To handle the submit event, the following approach is used:

<form method="POST" class="model-form" v-on:submit.prevent="submit_form()" ref="my_form">

Answer №1

Upon closer inspection, it appears that dat.toImage() operates asynchronously in JavaScript. As a result, the script may move on to the next method before this one has completed. If you are the creator of this method, consider returning a promise within the function and upon resolution of that promise, proceed to call your subsequent method.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are some tips for setting up event listeners directly within an HTML file?

For instance, if we want our listeners to only trigger in the event capturing phase, we can achieve that by doing something like this: element.addEventListener(event, function, true); Alternatively, element.addEventListener(event, function, {passive: tr ...

Grunt tip: Converting jshint results to HTML format

I've set up jshint to run with grunt successfully, but now I'm looking to have the output in HTML format. Below is my grunt configuration: module.exports = function(grunt) { // Project configuration. grunt.initConfig({ jshint: { ...

Learn the process of storing data using Realm database in JavaScript

Recently, I started working with react-native and decided to incorporate realm database into my application. My goal is to pre-populate the data using a JSON file. Initially, I attempted to achieve this by utilizing the componentDidMount() function along w ...

The rendering with Three.js has finished successfully

I'm curious, is there a way to determine when the object has finished rendering? While I've seen a progress bar in one example, I'm seeking a straightforward and uncomplicated illustration. So far, I've searched through the loader I&apo ...

Problem with Jquery slider user interface

Encountering an issue with the min and max values of the jQuery Slider UI. Set the minimum value to 5000, maximum to 1000000, with step increments of 25000. However, when viewing it on the front-end, the displayed values are not as expected: https://i.sst ...

In Javascript, we can increment the current date by utilizing the `getDate()`

I am trying to create an if statement in JavaScript; if (nextProcessingDate > today ) { //do something } nextProcessingDate has a timestamp assigned, like 09/07/2014 12:10:17 To assign today's timestamp to the today variable, I am using this c ...

Displaying information from a database in a text box

I'm trying to display data from a database in a textbox using this code. However, when I click the show button, I encounter an error: (Notice: Undefined index: first_name ) How can I successfully display the data in the textbox? **//BootStrap Co ...

Issues have arisen with certain sections of the website following adjustments to the .htaccess file

I recently updated my website to include Pretty URL's by following the advice in this htaccess question. Options +SymLinksIfOwnerMatch RewriteEngine On RewriteBase / # Make sure all URLs have www. RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteR ...

Hearken to the modifications in the header of ag-grid

I referenced this example to create a custom header for my https://stackblitz.com/edit/clabnet-ag-grid-rich. Here is how I made modifications to it: I added a button that opens a modal popup to edit the header of a column. Everything works correctly, but ...

Creating Javascript objects using provided JSON data

Good day! I am looking for assistance in understanding how to create objects from JSON data that has been provided: { "type":"FeatureCollection", "features":[ { "type":"Feature", ...

Finding the precise source domain address using javascript

I'm having trouble retrieving the original domain address. I've attempted using document.location and $location, but haven't found a solution. Instead of the actual domain address, it returns the IP address. After trying window.location.anc ...

Tips for coordinating actions across it blocks

I am currently utilizing protractor for running e2e tests on my angular application. My goal is to synchronize actions between describe or it blocks like this: describe('My spec', function () { doMyAction(); describe('My sub spec 1&ap ...

Connecting Laravel and Vue.js for seamless sharing functionality

I'm having trouble sharing the description when I try to share my current page in Laravel or a post in Vue.js. I've included the necessary og tags in the head section for the title and image, but the description is not getting shared. Is there so ...

The endless $digest loop issue arises when attempting to filter an array of objects

Encountered a persistent $digest loop issue while passing parameters to a custom filter defined on the $scope. Here's the snippet in HTML: <ul id="albumListUL" class="fa-ul"> <li ng-repeat="album in GoogleList | myFilter:Field:Reverse t ...

An unusual situation occurs in Angular 2 when using Zone.js and CommonJS modules: the code within an IF statement is executed even when the condition

After numerous checks, I have come across a peculiar anomaly. Within my Angular 2 service, I am loading a @type definition (typescript 2) that in turn loads a commmon.js module (visionmedia/debug). Inside this common.js module, there is a simple if stateme ...

The frequency of database updates exceeds expectations - involving vue.js this.$router.push and express operations

Having some trouble updating a MongoDB with this code. It seems to be updating three times instead of just once due to having three dates in the posts.date field. Utilizing Vue, Mongo, and Express for this project, I have the following data structure: { ...

Tips for modifying javascript code to have popups appear as bPopup instead of in a separate popup window

Is there a way to modify the following code so that popup windows open as dpopups instead of just in a new popup window ()? $(document).ready(function() { var table = $('#taulu').DataTable( { "ajax": "taulu.php", "columnDefs" ...

Reset the tabulator with updated data and a revised column setup on the existing DOM element

Currently, I am utilizing the tabulator jQuery plugin for a project. My goal is to re-initialize the tabulator on the same div element but with a different column configuration and dynamic data. I have provided a sample div and script below that shows wh ...

Connect to a point on the leaflet map using an external <a> tag reference

I have a leaflet map and I am trying to create a link that, when clicked, will activate a specific marker on the map. Essentially, I want the linked marker to simulate being clicked when the link is clicked. Here is an example of the link: <a href="#" ...

Creating a Typescript constructor and factory function with the same name: a complete guide

I have a Typescript definition file to create for a pre-existing Javascript library. Within this library, there are functions that can act as constructors or factories. How should I structure the typings to accommodate both? For reference, here are specif ...