I have a lightning component that has 2 attributes and in input.
<aura:attribute name="wrapperObj" type="Object" />
<aura:attribute name="originalWrapperObj" type="Object" />
<input id="myField" onchange="{!c:doValidateChange}" type="text" />
Here is the Javascript controller
doValidateChange: function(cmp, event, helper) {
helper.validateChange(cmp, event, helper);
},
Here is the Helper
validateChange: function(cmp, event, helper) {
var val = event.currentTarget.value;
cmp.set("v.wrapperObj.field1",val);
},
Here is what populates the attributes when the page loads
runWhenPageLoadsHlpr : function(cmp, event, helper) {
var pageLoadAction = cmp.get("c.runWhenPageLoadsApx");
pageLoadAction.setParams({
"isCommunityBuilder" : isCommunityBuilder,
"country" : country
});
pageLoadAction.setCallback(this, function(response){
cmp.set("v.wrapperObj",response.getReturnValue());
cmp.set("v.originalWrapperObj",response.getReturnValue());
});
$A.enqueueAction(pageLoadAction);
},
The problem is that when I change the field wrapperObj.field1 is getting populated with the value just like I would expect, but originalWrapperObj.field1 is also getting populated with the new value. I need it to maintain the original value. I am not updating originalWrapperObj.field1 so why would it update with the new value?
{!c:doValidateChange}
with{!c.doValidateChange}
cmp.set("v.wrapperObj", response.getReturnValue()); cmp.set("v.originalWrapperObj", Object.assign({}, response.getReturnValue()));
try it in this way.