I have been trying to build a generic method which can dynamically set attributes via component.set()
for example something like:
for(var i=0;i<myObjectArray;++){
component.set(myObjectArray[i].attName,myObjectArray[i].attValue);
}
But I get undefined errors. I've tried using "v.att" and just v.att but neither work.
Is this possible?
Edit
Here's the combinations I have tried.
Single quotes:
var myObjArray = [];
myObjArray.push({attName:'v.att1', attValue:object.value1});
myObjArray.push({attName:'v.att2', attValue:object.value2});
for(var i = 0; i < myObjArray.length; i++){
component.set(myObjArray[i].attName,response);
}
Single in double:
var myObjArray = [];
myObjArray.push({attName:"'v.att1'", attValue:object.value1});
myObjArray.push({attName:"'v.att2'", attValue:object.value2});
Double in single:
var myObjArray = [];
myObjArray.push({attName:'"v.att1"', attValue:object.value1});
myObjArray.push({attName:'"v.att2"', attValue:object.value2});
All end up with the error:
Uncaught Error in $A.getCallback() [Cannot read property 'varName' of undefined]
component.set
expects the attribute name enclosed in quotes. So that's where you may like to have themyObjectArray[i].attName
rendered enclosed accordingly.