• Dale James
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi Devs,
I'd finished the Input Data Using Forms unit from Aura Components Basics module and in some part they use a JSON.parse(JSON.stringify()) and call it a DISGUSTING TEMPORARY HACK.
My problem is that I don't understand why are they using that.
As far as I know, JSON.parse(JSON.stringify()) clones an object (use with care since can give strange results sometimes), but I don't get why clone when you already have the object.
Even worse, I removed that line of code and everything seems to work perfectly.
 This is all the explanation in the unit: 
"The “disgusting hack” works around a similar issue with references. To see the issue, change the line to remove the two JSON calls, and test the app. You’ll see what the problem is quickly enough. We’ll remove it in the next unit, and so won’t explain further."
The misterious hack is in the helper.js file.
({
    createExpense: function(component, expense) {
        let theExpenses = component.get("v.expenses");
        // Copy the expense to a new object
        // THIS IS A DISGUSTING, TEMPORARY HACK
        let newExpense = JSON.parse(JSON.stringify(expense));
        theExpenses.push(newExpense);
        component.set("v.expenses", theExpenses);
    }
})
Please, help me understand this hack.

Regards,

Benja