Vue Js: Addressing the lag in data/model updates

I have created a sample that mirrors the work I am currently doing.

var App  = new Vue({
  el:'#app',
  data:{
    demoText:'text',
    sections:2,
    sectionData:[0,0]
  },
  methods:{
    changeData:function(){
      for(var i=0;i<this.sections;i++){
        if(isNaN(this.sectionData[i]))
          this.sectionData[i]=0;
      }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>

<div id='app'>
  
  <div v-for="n in sections">
    <input type="text" v-model=sectionData[n-1]>
  </div>
  
  <input type="button" @click="changeData" value="click">
</div>

When clicking on the button in this example, I check if the data is isNaN and set it to zero if it is. However, the updated data is not immediately reflected in the DOM or textbox. Subsequently, if I update another textbox, the pending update action occurs.

How can I address this problem?

Answer №1

There are a few key issues at play here.

Initially, Vue is unable to detect the change you make in your click handler when altering it by index. This is a common limitation when modifying an array in Vue.

Additionally, there appears to be a flaw in the code where emptying a text box causes isNaN("") to evaluate to false. This phenomenon has been documented in various sources. Although I've applied a simple fix in the provided code, it's advisable to seek out a more robust solution.

var App  = new Vue({
  el:'#app',
  data:{
    demoText:'text',
    sections:2,
    sectionData:[0,0]
  },
  methods:{
    changeData:function(){
      this.sectionData = this.sectionData.map(d => isNaN(parseInt(d,10)) ? 0 : d)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>

<div id='app'>
  
  <div v-for="n in sections">
    <input type="text" v-model=sectionData[n-1]>
  </div>
  
  <input type="button" @click="changeData" value="click">
</div>

An alternative approach for your click handler could be

for (let i=0; i < this.sectionData.length; i++)
  if (isNaN(parseInt(this.sectionData[i],10)))
    this.sectionData.splice(i, 1, 0)

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

Real-time form validation in Bootstrap 4 while completing the form

After exploring the form validation section in Bootstrap 4.4 (https://getbootstrap.com/docs/4.4/components/forms/#how-it-works), I have successfully integrated the code into my project. The code example and script for triggering validation seem quite impre ...

What is the best method for waiting on a JavaScript function to provide a response utilizing the Chrome.storage API?

I am attempting to utilize the given code for managing name value pairs in a Chrome Extension. if (!this.Chrome_getValue || (this.Chrome_getValue.toString && this.Chrome_getValue.toString().indexOf("not supported") > -1)) { this.Chrome_getV ...

Retrieving a single post from a JSON file using AngularJS

I'm currently delving into AngularJS, but I seem to be stuck on what might be a simple issue. At the moment, I have some hardcoded JSON files with a few persons in them and no actual backend set up yet. In my form, I aim to display a single person ea ...

Comma styling in JavaScript

What is the reasoning behind developers choosing to format commas in code this particular way? var npm = module.exports = new EventEmitter , config = require("./lib/config") , set = require("./lib/utils/set"); As opposed to this formatting style? va ...

How can I use ajax to request data from PHP and retrieve several variables at once?

I'm exploring the use of JavaScript to call a PHP script that will then send multiple variables back to my JavaScript for manipulation purposes. Below is the JavaScript code I am using: $.ajax({ url: 'test.p ...

What is the best way to ensure that custom JavaScript and CSS files in Sphinx are always loaded with the most recent changes

Within the configuration file conf.py for Sphinx, I have specified the following: html_css_files = ['css/custom.css'] html_js_files = ['js/custom.js'] However, any alterations made to custom.js or custom.css do not immediately appear i ...

Your request was denied by the Google Maps API server due to an invalid request. Please include the 'size' parameter in your request

I need to incorporate a static Google Map into my application built on node.js. Here is the code snippet: .panel.panel-primary .panel-heading h2.panel-title On the map .panel-body img.img-responsive.img-rounded(src="http://maps.googl ...

JavaScript believes that the function is not defined, despite its clear existence

This question pertains to an issue regarding the recognition of Bookshelf.js model function as a function. The error message "Function is undefined, Bookshelf.js model function is not being recognized as a function" arises when trying to POST to the login ...

Expanding IntelliSense and helpful tooltips in VSCode for JavaScript and TypeScript by utilizing Node.js for a deeper understanding

As a beginner in programming, specifically in JS/TS, I've been experimenting with node.js and have encountered a puzzling issue with the IntelliSense or 'helptext' feature in VSCode. For instance, when attempting to use fs.open(), I receive ...

What is the best way to manage the login notification?

Is there a way to close the alert or input the UserId and password and then click on Login? Here is the code for the alert This is how the alert appears I could use some assistance in managing this alert ...

Transforming a string into a JavaScript array using regular expressions

Excuse me if this question has already been asked (this is my first time asking). The content of my string looks like this (after removing the whitespace): "#home #blog description of page here ##article1 ##article2 #contact" When parsed, it will produce ...

A Step-by-Step Guide to Retrieving the Route of a Controller Function in Express

When working with Express.js, it is possible to retrieve the names of all controllers from an app. However, these names are usually in an unfamiliar format (such as handle: [AsyncFunction: login]). I have been unable to figure out how to destructure this i ...

Routing with nested modules in Angular 2 can be achieved by using the same

Encountering a common issue within a backend application. Various resources can be accessed through the following routes: reports/view/:id campains/view/:id suts/view/:id certifications/view/:id Note that all routes end with the same part: /view/:id. ...

Elevate your tooltips with Bootstrap 5: enabling hoverable tooltips and clickable links

At times, you may want to include clickable links in tooltips. I recently encountered this issue while using Bootstrap 5 and had trouble finding a solution. After some trial and error, I finally figured it out and wanted to share my findings. The standard ...

Building a linked list in Javascript with pointers

After implementing a linked list in two different ways using Javascript, I found myself in a state of confusion about the inner workings of the code. Trying to mentally visualize the creation process and the memory addresses being referenced proved to be ...

Retrieve a specific JSON object from an array of JSON objects by applying a condition with Restangular

How can I efficiently retrieve a specific playlist with user_id == 1 from a json api (playlists.json) without having to loop through all playlists? This is what I have tried so far: var playlists = Restangular.all('playlists'); playlists.getLi ...

How to Alphabetize an Array of Strings Containing Numbers using TypeScript and Angular

I'm working with an array that looks like this: arr = ["100 abc", "ad", "5 star", "orange"]; The goal is to first sort the strings without numbers at the beginning, then sort the strings with numbers added at t ...

What are some solutions for addressing the viewport width problem that comes up when incorporating Google+ comments on a responsive website

My website looks great on both desktop and mobile devices without any viewport issues, until I added Google+ comments. These comments will automatically set the width of the page to a specified amount: <!-- Google comments --> <script src="https: ...

Put the values into an array format for each key within an object using JavaScript

I am attempting to transform an array containing email addresses into an object. How can I insert values in the value array for a single key? var list = [ "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6903060107291008010 ...

Achieving a bottom tab bar display exclusively on default screens within nested stacks in react-navigation

Suppose I have a Tab Navigator where each tab contains nested stack navigators (refer to the code snippet below). My goal is to have the tab bar visible only on the default screen of each nested stack, similar to how the current Twitter Android app operate ...