I have an attibute <aura:attribute name="projectResources" type="Contact[]" />
and iterate it by <aura:iteration>
. When user clicks a button I want to add a new element to the array. When I use projectResources.push(selectedResource);
it adds the element as a Proxy and do not apply the element inside of <aura:iteration>
. To workaround I use projectResources = JSON.parse(JSON.stringify(projectResources));
. Example:
var projectResources = component.get("v.projectResources");
// Workaround to extract array from Proxy.
projectResources = JSON.parse(JSON.stringify(projectResources));
projectResources.push(newResource);
component.set("v.projectResources", projectResources);
I don't like this JSON.parse(JSON.stringify())
approach. Is there any better way?
get
,push
,set
to add new entries to an<aura:iteration>
with the HTML updating as expected. Not sure what you mean by "proxy" here. If you are starting from no items worth addingif (!projectResources) projectResources = [];
to start with an empty array not null or undefined.<aura:attribute type="anySObjectArray">
to show it inside of<aura:iteration>
?