Check for nested objects while watching - NuxtJS (Vue) in its most recent release

My form is connected to a data object called this.myData:

 data: function() {

  return {
    isDataChanged: false,
    myData: {},
    myChangedData: {
        default: '',
        default1: {},
        default2: []
    },
  }
},

The values in myData are populated from the server response and they fill out the form. On the other hand, myChangedData stores new values that are updated with

v-on:input="onChangeMyData($event, 'default')"
:

   onChangeMyData(e, name, required = false){
          const val = e.target.value.trim();

          this.myChangedData[name] = val;
          console.log(this.myChangedData)

          this.checkIsmyDataChanged();
      },

I can use the same method by providing a key as the second parameter. The checkIsmyDataChanged method compares the properties of myChangedData with changedData to determine if any field has changed in the form, setting this.isDataChanged = true if there's a difference.

The challenge arises from the complex structure of mydata/mydatachanged. It contains objects under default1 and default2 is an array of objects. This makes using onChangeMyData impractical, requiring different methods with specific checks (validations) where this.checkIsmyDataChanged() needs to be invoked.

To address this, I implemented a watcher for myChangedData:

 watch:{
      myChangedData: {
          handler: function (newVal) {
              console.log('change')

          },
          deep: true
      },
  },

, however, it doesn't trigger on data changes

Answer №1

Have you attempted using Vue.set? Check out the reference here

Replace this.myChangedData[name] = val; with

this.$set(this.myChangedData, 'name', val)

By doing this, any changes made to the object will be detected and trigger the watcher.

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

Utilize Angular's ng-repeat directive to iterate through this JSON data structure for implementing user-to

Struggling with the user-to-user messaging interface on an app and having difficulty using ng-repeat on the items. Here is the provided data: { "ID": 4118, "CreatedDate": "2015-08-20T03:12:50.397", "recipient": [ { ...

NextJS-PWA detected that the start_url was responsive, but it was not through a service worker

Recently, I incorporated next-pwa (npm link) into my workflow to streamline the service-worker setup process for building a PWA in NextJS. While most things have been smooth sailing, there's one persistent error (shown below) that continues to pop up ...

What are the steps to correctly shut down an ExpressJS server?

I am facing a challenge with my ExpressJs (version 4.X) server - I need to ensure it is stopped correctly. Given that some requests on the server may take a long time to complete (1-2 seconds), I want to reject new connections and wait for all ongoing req ...

Transitioning from a multipage application to Piral: A comprehensive guide

Our organization operates several ASP.NET Core applications that are traditional multipage applications. As we develop a new portal using Piral, we want to incorporate elements from our existing applications while also introducing new modules. How can we ...

Troubleshooting error 1045: User 'root'@'localhost' denied access

When trying to connect a MySQL database to my NODEJS application, I keep encountering an error every time I start the server. The error message reads "Access denied for user 'root'@'localhost' using password YES, error number 1045." De ...

`To activate/deactivate tabs by choosing options from drop-down menus`

Hey there, I'm currently dealing with a combo box that has 5 drop down items in Tab1. There are also other tabs present such as tab2, tab3, tab4, and tab5. Tab1 is enabled while the other tabs are disabled. Each of the disabled tabs contains different ...

The Power of Symfony Combined with jQuery's Data Handling Function

My app features an ajax navigation system. Each ajax link is given the class ajax, and the page it should load is stored in an aurl attribute, as shown below: <a class="ajax" aurl="/user/posts/3/edit">Edit Post</a> The element's aurl is ...

Problem encountered when trying to apply background opacity to custom colors using Tailwind CSS and shadcn/ui

While using Tailwind CSS along with the shadcn/ui library for styling buttons, I have encountered an unexpected behavior. The issue arises when trying to add background opacity to a custom color defined in globals.css using HSL values such as: --primary: 3 ...

Is there a method to incorporate lists of varying types in a single v-for loop without causing any issues with type validations?

My current component is designed to display two different datasets, each with their own unique nature of data: state.articleTypeList: string[] state.renderPriceClassNameList: {[key: string]: string[]} To render both datasets within a single v-for componen ...

no data retrieved from YouTube API query

I'm puzzled as to why the items array is coming up empty. Can someone point out what's causing this issue? Thank you. jQuery(document).ready(function ($) { $('#infl-yt-label').on('click', function() { //$('#infl-inp ...

Adjust Javascript to modify the URL by assigning a new URL without utilizing the origin as the parent

Currently, I am working on a script that is responsible for sending string values to the server in order to change the ipv4 information on a specific device. However, I am facing some difficulties and would appreciate some assistance with automatically upd ...

Issue with Javascript Promise causing failure to populate list with objects

app.get('/zones/:id/experiences', function(req,res) { var zone_key = req.params.id; var recent = []; var ref = firebase.database().ref('participants/'+zone_key+'/experiences'); ref.on("value", function(snapshot) { ...

Issue arises with asynchronous function outside of mounted lifecycle hook in VueJS

Identifying the Issue I encountered an issue while trying to create an external async function and assign its return value directly to a state variable. In addition, I passed firebase reference and store to this function to avoid importing them again in t ...

What indicators should we look for to decide if JavaScriptExecutor is needed in a Selenium C# project?

Encountering an exception with TestInChrome1 - "OpenQA.Selenium.ElementNotInteractableException: element not interactable". On the other hand, using JavaScriptExecutor in TestInChrome2 resolves the issue. Some key questions arise: What is causing the ...

Measuring Load Time Percentage in AngularJS Using $http Requests

I am currently utilizing Angular with my Rails application. I have a sample app and I am interested in displaying the response time required to load a response in Angular. For example, When loading a response of an array containing 100,000 elements, I w ...

Traversing through objects in react.js

Hello there, I'm currently learning React and JavaScript but facing some challenges with a particular task. Let's dive into it! So, imagine I have an array like this: clients = ["Alex", "Jimmy"] and then I proceed to create another array using th ...

Tips for passing parameters to an ajax request in Django URL?

In my current setup, I'm passing a URL to the ajax like this: $('.togglebtn').on("click",function(){ console.log("Hello") $.ajax({ type: 'GET', url: "http://loc ...

Designing tab navigation in React Native

Is there a specific way to customize only the ORANGE tab's style? I am curious to learn how to address this particular scenario. I have attempted various methods to modify the style of the ORANGE tab. My application utilizes navigation 5. <Tab.Navi ...

Employing jQuery with dynamic content within this context

Can anyone help me with jQuery and dynamic content? I'm trying to prepend items to a list and show the contents of a specific item when clicked, but all items' content is being displayed instead. I attempted using (this) without success. $(docum ...

Update an array by breaking it into smaller chunks, while maintaining the original index positions

I am faced with a situation where I must update an existing array by utilizing its chunks to perform a specific operation. While I have a functional solution in place, I am concerned about the performance implications and I am eager to determine if my curr ...