What is the best way to trigger an axios HTTP PUT request in Vue JS only when there are changes to the object?

How can I optimize my axios http put requests to only be sent when an object changes?

I've noticed that unnecessary requests are being made, putting strain on the server. I want to limit this by only sending a request if the object has been updated or modified since it was initially loaded on the page.

One of the main reasons for this is that automatic saving on input via @blur is causing frequent http activity.

<input @blur="editInput()"></input>

The editInput() method triggers an axios put http request every time. Is there a way to check for changes before making the request?

Should I clone the object in the created() hook and then use a comparison method? This was my initial attempt but it didn't work as expected.

Answer №1

One approach is to utilize a watcher, as shown in the following example:

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!',
    foo: {
      bar: {
        baz: 'foo bar baz'
      }
    }
  },
  watch: {
    message(newValue, oldValue) {
      if (newValue != oldValue) alert('change 1')
    },
    'foo.bar.baz'(newValue, oldValue) {
      if (newValue != oldValue) alert('change 2')
    }
  }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ message }}</p>

  <input v-model="message">
  <input v-model="foo.bar.baz">

</div>

Answer №2

To keep a close eye on your property, consider the following approach:

monitor: {
  occupant: {
    observer: (latestValue, previousValue) {

    },
    comprehensive: true
  }
}

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

Conceal the URL and any parameters upon successful completion of AJAX request

In my ajax js code, the solonreport.jsp file returns a URL link that connects to a local report server and generates an Excel report. However, when using this link with the window.open(json.url) function, the link and parameters are visible to the user. ...

Running multiple npm scripts on both Unix and Windows systems can be achieved by using the "npm

Is there a way for me to execute multiple npm scripts synchronously, one after another? Below are the npm scripts I have in my project, which includes both bower and npm packages: { "scripts": { "installnpm": "npm i", "installbower": "bower i", ...

Vue.js will trigger the updated() method only when a particular array undergoes changes

Working on a chat feature and looking for a way to automatically scroll to the end of the conversation when new messages are added. The current solution involving the updated() function works well, but there's a complication with a vue-timepicker com ...

Dragging a stack of cards in a game of Solitaire using jQuery UI

I'm currently in the process of creating a Solitaire card game using Javascript. To enable dragging and dropping functionality for the cards, I am utilizing jQueryUI. In the example provided at http://jsfiddle.net/HY8g7/1/, you can see how the cards c ...

What is the best way to format a lengthy SQL query as a JavaScript variable?

I have a lengthy SQL query that needs to be converted into a JavaScript variable. The contact information is spread across separate rows using the + character. However, the SQLite plugin is displaying a parsing error: What is the correct way to format t ...

Is there a way to transform a large gltf file into jsx format?

I recently created a 3D scene in Blender and wanted to incorporate it into my React Three Fiber project. However, after exporting the scene to glTF format, I discovered that the file contained around 300k lines. The strange thing is that the file works per ...

Bespoke String Implementation

Can someone help me find a dual approach? I am interested in customizing strings based on type. I want to be able to determine the type of a string different from a primitive string during runtime. Take a look at this code: class TZDatabaseName extends ...

Retrieve data from FileReader's onload event asynchronously in Javascript

Although this question has been asked numerous times before, I have reviewed the answers provided and still cannot get my code to work. I am working with Angular 2. I have input tags for file insertion. The file successfully reaches the component, but when ...

Customize your jQuery 3.x version by adding Ajax functionality tailored to your

I've been working on creating a custom jQuery 3.x version, but I'm facing the issue of maintaining the functionality of $.ajax even when ajax is not explicitly removed. Below are my build and grunt settings: grunt custom:-css/showHide,-deprecate ...

Error: Unable to access classList property of null object

https://i.sstatic.net/ZZZDs.pngI am currently developing a nextjs application and encountered an issue while attempting to create a dropdown feature. Below is my complete code: import React from 'react' import Link from 'next/link' imp ...

Populate the attributes of an alpaca with a variable

Creating a form dynamically using Alpaca Forms is a requirement and the given code snippet serves as a starting point: $("#form").alpaca({ "schema": { "type":"object", "pr ...

Next step is transferring form data from React to Node server

I am currently working on an EmailList component in react that interacts with a server-side Node.js script called EmailServer.js. This script uses Nodemailer to send emails, and I have successfully set it up to send emails with predefined subject lines and ...

Trouble obtaining dates and values in JSON while analyzing Commodity Data Charts

{ "data": { "success": true, "timeseries": true, "start_date": "2022-02-01", "end_date": "2022-03-02", "base": "RUB", "rates": { ...

React navigator appears stuck and unable to navigate between screens

I encounter an issue where my app closes when I press the switch screen button, but there is no error output displayed. I have checked for any version discrepancies and found no problem. The function I implemented for the button is functioning as expected ...

Function in AngularJS to increment counts based on matching names

Check out my angular application on this Plunker link The text area in my app contains data in the format of (key, count). My goal is to sum up these values using the calc() function. When the user clicks the button, I want the total summation to be disp ...

Creating a line of functions pool in Javascript with a delay feature

Recently, I created a code snippet that simulates a function line. It involves calling functions such as fn1, delay, fn2, delay, and so on. The idea is to call a function, remove it from the line, have a short sleep, and repeat. However, I've encount ...

Tips for successfully mocking axios.get in Jest and passing AxiosPromise type value

I attempted to simulate the axios.get() function using the code below, however TypeScript is returning an error stating "argument of type '{ data: expectedResult }' is not assignable to parameter of type 'AxiosPromise<{}>'". Can ...

Utilize Angular2 with ES6 modules while running an Express server

Having some issues using ES6 Modules with Angular2 in an app served by Node.js and Express.js. When attempting to load the Angular2/ES6 app in browser, encountered this error message in the FireFox console: The stylesheet http://localhost:8080/boot.css w ...

Angular directive for concealing the 'ancestor' of an element

I have created a code snippet that effectively hides the 'grandparent' element of any '404' images on my webpage. Here is the code: function hideGrandparent(image){ image.parentNode.parentNode.style.display = 'none'; } < ...

Enhancing a JQuery Element to Suit Your Needs

Is it recommended to enhance a JQuery element with custom methods? For example: var b = $("#uniqueID"); b.someMethod = function(){}; Additional Information Let me explain further, I am developing a JavaScript application that binds JSON data to local J ...