Modified the object path and updated the Dirty stages for nested objects within Vue state

Imagine having an object that contains arrays of objects in some properties. Whenever changes are made to any field, whether it's within an object in an array or the main object itself, you want to mark that object as "dirty" using a code snippet like this:

object.defineProperty(path, '_dirty', true)
.

{
    "id": "48e143e5-eda0-490a-a87a-dbef128f539e",
    "parentId": null,
    "groups": [
        {
            "id": "142c7263-26ab-4169-88bb-5b2b880f3b97",
            "overviewId": "48e143e5-eda0-490a-a87a-dbef128f539e",
            "parts": [
                {
                    "id": "730adbca-20c0-427a-8228-7000d0553466",
                    "overviewGroupId": "142c7263-26ab-4169-88bb-5b2b880f3b97",
                },
                {
                    "id": "9c749aa0-b1c0-49cd-81c1-272d9bfe5df7",
                    "overviewGroupId": "142c7263-26ab-4169-88bb-5b2b880f3b97",
                }
            ],
        }
    ],
    "templateInfo": {
        "id": "34d2be1c-9b4e-4eb2-b452-82c7929057c8",
        "name": "Standart OV",
        "parentId": null,
        "typeId": 0,
        "storeId": "ee7bdc31-8c13-4111-9225-cb780b46e1a1"
    },
    "pageTypeInfo": {
        "id": "016ef17a-4cd7-4096-b387-6b16c39df41e",
        "typeId": 3,
        "code": "code A",
    }
}

The object is stored in data like this:

data() {
    return {
        source: {},
    }
},

and includes a watcher:

source: {
    handler(newValue, oldValue){
        console.log(...arguments)
    },
    deep: true
}

Is there a way to capture the path of the changed field (e.g., "groups[0].parts[1].id") and set the targeted object at that path as _dirty when a change occurs?

Answer №1

To modify the value, you can utilize a method that employs the $set function in the following manner:

updateProperty(object, propertyKey, updatedValue) {
  this.$set(object, propertyKey, updatedValue);
  this.$set(object[propertyKey], '__modified', 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

An effective method for linking a value from one ng-model to another is by identifying and matching a specific string

I have been tasked with binding a value to one model based on the content of another model when it contains a string that starts with "https". For instance, there are two text fields each associated with a different model. <input type="text" ng-model= ...

Can someone help me uncover the previous URL for login using just JavaScript? I've tried using document.referrer but it's not giving me the

Currently, I am utilizing express with pug templates and pure JavaScript. In order to enhance the user experience of my log in system, I would like to save the URL that someone came to the login page with, so that I can redirect them back to it once they h ...

Navigating through a large array list that contains both arrays and objects in Typescript:

I have an array containing arrays of objects, each with at least 10 properties. My goal is to extract and store only the ids of these objects in the same order. Here is the code I have written for this task: Here is the structure of my data: organisationC ...

Error: React is throwing a SyntaxError because a ")" is missing in the argument list

While working on a React-typescript project using Vite, I encountered an issue where my page was displaying blank and showing the error : Uncaught SyntaxError: missing ) after argument list (at main.tsx:6:51) This error was found in the main.tsx file : im ...

Nuxt3 introduces Intlify i18n with the requirement "It is necessary to be invoked at the beginning of a `setup` function"

Encountering an issue when attempting to alter the language within a click function. "Uncaught SyntaxError: Must be called at the top of a setup function (at message-compiler.mjs:54:19)". However, changing the language outside of the function ...

Tips for successfully passing store state as a prop in React-Redux with TypeScript

Having trouble passing information from the initial state of the store to a component where it's supposed to be rendered. Despite a console.log in the component showing that it's undefined, there doesn't seem to be any issue with the initial ...

The Javascript Node class encountered an error: X has not been defined

I have a class that looks like this: const MongoClient = require("mongodb").MongoClient; const ConnectionDetails = require("./ConnectionDetails").ConnectionDetails; const Recipe = require("./recipe").Recipe; var ObjectId = req ...

What is the best way to prevent code execution until a value is returned by $.get?

My attempt to read data from a file and store it in an array using jQuery's get function has hit a snag. Since the get function is asynchronous, the code that comes after the $.get call runs before the data is defined. How can I make sure that the cod ...

A unique string containing the object element "this.variable" found in a separate file

Snippet of HTML code: <input class="jscolor" id="color-picker"> <div id="rect" class="rect"></div> <script src="jscolor.js"></script> <script src="skrypt.js"></script> Javascript snippet: function up ...

"Maximizing Data Interaction: The Integration of PHP and AngularJS

I have been developing an AngularJS App alongside a PHP RESTful API for the Backend. I am currently brainstorming about the most effective approach to harness the power of 2-Way Data Binding in AngularJS with my specific scenario. As an illustration, I ha ...

Instructions on how to insert information into an array by clicking a button

As a newbie JavaScript programmer, I am struggling to figure out how to make my code work as a function. I want to update the ajax_data array every time I click the add row button. Fresh JS coder looking for some guidance. var ajax_data = [{ Type: "A ...

Using JavaScript's Regex to match sentences, while ensuring any full stops within quotes are ignored

Here is the regular expression: (?:(?:Jr|Master|Mr|Ms|Mrs|Dr|Capt|Col|Sgt|Sr|Prof|Rep|Mt|Mount|St|Etc|Eg)\.\s+|["'“(\[]?)(?:\b(?:(?!(?:\S{1,})[.?!]+["']?\s+["']?[A-Z]).)*)(?:(?:(?:Jr|Master|Mr|M ...

Having trouble with ejs.filters?

I'm having trouble grasping ejs filters and getting them to work correctly: Server.js var ejs = require('ejs'); ejs.filters.example = function() { //placeholder for example }; Routes.js app.get('/home', function(req, res) { ...

AngularJS - ng-repeat: Warning: Repeated items found in the repeater and are not allowed. Repeater:

I'm currently using ng-repeat to showcase a collection of items fetched from the Twitter API. However, I am encountering an issue where Angular attempts to display the empty list while the request is still being processed, resulting in the following e ...

Redirect middleware for Next.js

I am currently working on implementing a log-in/log-out feature in my project using nextjs, redux-saga, and mui libraries. // middleware.ts import { NextRequest, NextResponse } from 'next/server'; import { RequestCookies } from 'next/dist/c ...

Running Selenium tests are not able to initiate Javascript ajax requests

I'm currently troubleshooting a scenario using Selenium that involves the following steps: - When a user inputs text into a textarea, an ajax request is sent, which then adds the text to the database (implemented using django and processed in view.py) ...

Is it better to append content in JQuery rather than replacing it with .innerHTML?

Here is a function designed to retrieve older wallposts from a user, 16 at a time, and add each chunk of 16 to the end of the current list in the div called "sw1". The function works well, except when there is a wallpost containing a video or embedded obj ...

When attempting to post data using jQuery, an object is encountered that does not have a parameterless constructor

Here is the code snippet I am using with jQuery to post data and register a band: var formData = new FormData(); var file = document.getElementById("CoverPicture").files[0]; formData.append("file", file); var Name = $('#Name').val(); var Genre = ...

Utilize JavaScript to communicate with the backend server

I'm embarking on my first Cordova application, utilizing HTML, CSS, and JavaScript. My current objective is to trigger a local server call upon button click, with the intention of logging something to confirm functionality. However, I'm encounter ...

Is there a way to implement a targeted hover effect in Vue Js?

I'd like to create a hover button that only affects the nested elements inside it. Right now, when I hover over one button, all the nested elements inside its sibling buttons get styled. Any ideas on how to fix this? <div id="app"> <butto ...