Strange! Two different attributes are not isolated to each other in a Lightning Component

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Strange! Two different attributes are not isolated to each other in a Lightning Component



Well, I don't know what going on.
I'm setting two different attributes in a Custom Lightning Component on init, one is to iteration on UI side and another one is for just reference and it holds the original value.


component.set("v.products", productRatesByInt);
component.set("v.productsTemp", productRatesByInt);



Now, if there is any change from the end user on UI, I have a method to handle it and do some changes to its dependent records and that is happening in productsTemp attribute and then setting its updated value, like below:


productsTemp


component.set('v.productsTemp', productsTemp);// After some change from UI



That works perfectly. But in the same method when I do below


var actualProducts = component.get("v.products");



to get actual/Original Products, which should be without any change, because it is a totally different attribute, but it is not the same. Its records are updated with the temp record's updated value, even though these are two different attributes.



Not sure why this is happening. There is no update/modifications in v.products during this whole Process. The goal I need to achieve is I want to compare what has been changed from actual record.


v.products




1 Answer
1



Hmmm, maybe for some reason the Lightning frameworks binds those together.



Can you try to make a clone of productRatesByInt by doing this:
component.set("v.productsTemp", JSON.parse(JSON.stringify(productRatesByInt)));


productRatesByInt


component.set("v.productsTemp", JSON.parse(JSON.stringify(productRatesByInt)));





Wow!!! That was one shot solution. It is isolated now. Thanks
– SFDC Mafia - VIC
1 hour ago






I'd avoid stringify/parse like that because of performance reasons, though.
– sfdcfox
1 hour ago





@sfdcfox Yes, Definitely I see the difference now. What would you think of other solution, if we avoid Stringify/parse?
– SFDC Mafia - VIC
1 hour ago






@SFDCMafia-VIC component.set("v.productsTemp", Object.assign(, productRatesByInt)); Object.assign copies values of an object on to a different object.
– sfdcfox
1 hour ago



component.set("v.productsTemp", Object.assign(, productRatesByInt));





That is indeed a better solution. Anyways, happy that it helped!
– Bram
58 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS