Using Vue.set ensures that copied objects within the Vuex store are properly updated

Every time I choose an object to edit, the Vuex store generates a duplicate of the object so that reverting back to the original object state is possible.

The loading mutation in my code appears as follows (when selecting an object from a tablegrid):

@Mutation
  private [MutationTypes.LOAD_OBJECT_DETAILS_SUCCESS](object: ObjectDetailViewModel) {
Vue.set(this.State.detail, 'initialObject', object);
Vue.set(this.State.detail, 'workingCopyofObject', cloneDeep(object));
}

I am utilizing the lodash CloneDeep Method for this purpose, but encountered similar issues when using plain JavaScript object copying like:

JSON.parse(JSON.stringify(router)))
.

However, whenever I attempt to modify the working copy of the object, the Vue.Set Method also modifies my initial object.

  @Mutation
  private [MutationTypes.UPDATE_WORKINGCOPY_Object](updateProp: KeyValuePair) {

      Vue.set(this.State.detail.workingCopyofObject, updateProp.key, updateProp.value);

  }

This leads to a situation where invoking the reset mutation does not restore the initial values:

 @Mutation
 private [RouterMutationTypes.DISCARD_WORKINGCOPY_CHANGES]() {
   Vue.set(this.State.detail, 'workingCopyofObject', this.State.detail.initialObject); // initialObject already reflects changes from the working copy
 }

It seems like there might be something fundamental about how the Vue.Set Method functions that I may have overlooked.

Sincerely,

Finn

Answer №1

The issue lies within your reset mutation function. Resetting the working copy to be the same as the initial object defeats the purpose of having a separate copy. This will result in the duplication behavior you reported when following this program flow:

Set > Update > Reset -- After this point, any future updates will affect both objects

To resolve this, a simple solution would be to clone the initial object during the reset action:

@Mutation
private [RouterMutationTypes.DISCARD_WORKINGCOPY_CHANGES]() {
   const clonedObject = cloneDeep(this.State.detail.initialObject);
   Vue.set(this.State.detail, 'workingCopyofObject', clonedObject);
}

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

I'm having trouble importing an image into my typescript file as it keeps showing a "module not found" error message. Can anyone

I am new to working with TypeScript and I have encountered an issue when trying to import an image from my assets folder. Despite having the image stored in a designated folder, Visual Studio Code notifies me that it cannot find the module. Here is the er ...

What are some methods to implement overflow:hidden for stacked images inside a div?

I currently have two images stacked on top of each other within a div element as shown below: <div class="container"> <img src="somepic.jpg" class="layer" /> <img src="otherpic.jpg" class="layer" /> </div> Styling is set u ...

Implementing reCAPTCHA in Spring MVC with Ajax

I've been attempting to implement Google reCAPTCHA service with Spring MVC, but I keep encountering this error in the console: http://localhost:8080/spapp/ajaxtest.htm?name=frfr&surname=frfr&phone=edede…nGi3CCrD9GprYZDn8VHc-pN--SK-u_xoRKOrn ...

Using JavaScript to pass the href attribute value

On my HTML page, there are multiple links that reload the page when clicked. I want to capture the href value of each link the user clicks and store it in a variable called xhash: Here is the HTML: <a href='#taba' id='passid'>Li ...

What do you think of the unique JSON syntax found in the React-Redux-Firebase documentation? Valid or not?

The React-Redux-Firebase documentation showcases a sample code snippet. import { compose } from 'redux' import { connect } from 'react-redux' import { firebaseConnect, populate } from 'react-redux-firebase' const populates = ...

Limit the API call to only respond to requests from the localhost

I'm currently working on a website that uses API calls within the same node project. I would like to restrict most of these API calls to only be accessible by the localhost website. Is there a way to achieve this without implementing OAuth and simply ...

Disappearance of newly added row in jquery

I am currently facing an issue with a code I have written for database updates. The problem arises after adding a new row, as it initially displays but then promptly disappears. This peculiar behavior occurs on the server end when I attempt to view the p ...

Nuxt router directing to incorrect URL upon refreshing the page

Let me show you exactly what I mean by setting up a demo Nuxt blog at https://example.com/nuxtblog/. This demonstration includes articles populated by the @nuxt/content package. The website has been generated statically using the nuxt generate command. Fol ...

Choose autocomplete feature from an external source within the context of AngularJS

I am currently working on an autocomplete textbox and I stumbled upon this script that I found through my search efforts. .controller('autoCompleteCTRL', function($scope, $rootScope){ $rootScope.searchItems = [ "ActionScript", ...

Using ReactJS to dynamically append tags and content to react-dom and then refresh

As a newcomer to reactJS, I am currently working on building a react app presentation using spectacle. The unique aspect of this project is that the content and number of slides for the presentation are dynamic and fetched from a server. These slides come ...

Obtaining the source code from a different domain website with the help of jQuery

Is there a way to extract part of the source code from a YouTube page without using server-side programming? I've tried cross-domain AJAX techniques like Yahoo YQL and JsonP. While Yahoo YQL allows me to grab part of the source code, I'm facing ...

Leveraging RXJS for real-time response to keyboard and mouse click combinations in web

I am new to RXJS and looking for a way to drag an HtmlElement when the user presses the space key and then drags the element with the mouse. The dragging should be initiated by either pressing the SPACE key followed by a left click, or vice versa. The dra ...

Chrome successfully processes Ajax request, but Firefox encounters failure

I've encountered a peculiar issue with a JavaScript function that is responsible for making an Ajax call to a PHP page for database transactions and data processing. Take a look at the function below: function processQuizResults() { console.log(" ...

What is the best way to modify a specific data point in d3.js without affecting any other elements?

I need help updating a single data point and changing only the corresponding element without affecting all elements. However, I'm struggling to find the right solution. An article suggests that using var sel = svg.selectAll(...).data(...) provides an ...

Sometimes, a record goes missing from a PHP/JSON array

When calling a PHP script to retrieve results and converting the array into a JavaScript array for a multiple choice quiz, I encountered an issue where certain records were missing unexpectedly. It seems to occur randomly as there is no specific pattern to ...

How to extract the chosen option from a bound list within an Angular application

Our goal is to avoid using 2-way binding in our component setup: <select type="text" formControlName="region" (change)="regionChanged($event)"> <option *ngFor="let region of regionsDDL" [ngValue]="region">{{region.name}}</option> ...

Upon refreshing the page, the React application alters the font style

While working on my ReactJS application, I noticed that the font changes after the page loads and then reloads. Take a look at the examples below: Before reloading (press f5): https://i.sstatic.net/tJFjU.png After reloading (press f5): https://i.sstati ...

The curious conduct of wild safari creatures

I have been using JavaScript to split my ePub chapter into pages by wrapping the code with new divs that represent separate pages. I have also created CSS styles for these new divs. Everything works perfectly when the file has a filename extension of &apos ...

Having trouble resetting a checked checkbox label in Vuejs?

uncheck: function(checkedName) { this.checkedNames = this.checkedNames.filter(name => name !== checkedName); }, uncheckall: function(event) { this.checkedNames = []; }, checkedInput(event) { if (this.checkedNames.includes(event.targ ...

What To Do When You See "Unable to retrieve the 'get' property of an undefined or null reference" Error

I've encountered an error while trying to develop an application. The error message reads "Unable to get property 'get' of undefined or null reference at ProfilePage.prototype.ngOnInit". My framework of choice is Ionic 4. This error specif ...