function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nathan HincheyNathan Hinchey 

Handle Actions with Controllers: why not "v.item.Packed__c"?

What is the allowed syntax for v.attribute notation in controllers?

I found the same solution as @Jeff Hansen, but I'm curious about WHY. Why do we have to do
 
var a = component.get("v.item");
a.Packed__c = true;
component.set("v.item",a)

Instead of just doing
 
component.set("v.item.Packed__c",true)


to get the correct answer here?

I can't find documentation about this.

Thank you.

 

PS: is there a way to include code blocks in these questions and answers?

RahulForceRahulForce
Hi Nathan,

Well you can do the task by both the ways, but the best way is -
var a = component.get("v.item");
a.Packed__c = true;
component.set("v.item",a);
as here you have the advantage to update multiple fields easily within javascript and you have to update on the page only once using component.set() method.
Whereas here - 
component.set("v.item.Packed__c",true);
You have to write the same statement for each field of object and the JS is communicating with page again and again.
So, this is a more better approach as well as an easy one.

Thanks,
Rahul Malhotra
 
Nathan HincheyNathan Hinchey
Okay, so the second one is legal syntax, but not best practice?
Hara SahooHara Sahoo
Hi Nathan,

I could do the below and it was alright.


<p>
        <lightning:input type="checkbox"                            
                         checked="{!v.item.Packed__c}" label="Packed"/>
     </p> 
    
     <div>
         
        <lightning:button label="Packed?" name='myButton' onclick="{!c.packItem}"/>

    </div> 

Controller:
({
    packItem: function(component, event, helper) {
        var btnClicked=event.getSource();
        component.set("v.item.Packed__c", true);     
    }
})