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
Vinay vinayVinay vinay 

The controller action marks the item attribute as packed, updates the item value provider and disables the button.

The controller action marks the item attribute as packed, updates the item value provider and disables the button is the task given in Trail head.

I have written the below controller to perform the action:

({
    packItem : function(component, event, helper) {
        event.getSource().set('v.disabled',true);
        component.set('v.item.Packed__c',true);
    }
})

This is setting the Packed__c to true but I am not passing the challenge.

but when I googled, below code was suggested:

({
    packItem : function(component, event, helper) {
        event.getSource().set('v.disabled',true);
        var a = component.get("v.item",true);
         a.Packed__c = true;
         component.set("v.item",a);
    }
})

But this code is not setting Packed__c to true. Please let me know the difference between two controller.

Trail head challenge:
https://trailhead.salesforce.com/lex_dev_lc_basics/lex_dev_lc_basics_controllers
NagendraNagendra (Salesforce Developers) 
Hi Vinay,

Please check with below thread from forums community for similar issue with possible workarounds. Hope this helps.

Best Regards,
Nagendra.P

 
Vinay vinayVinay vinay
Hi Nagendra,

My question is:

how 
 var a = component.get("v.item",true);
 a.Packed__c = true;

is different from 
component.set('v.item.Packed__c',true);

That is not explained in above link which you had provided.

Regards
Vinay Tej
Mohammad AnisMohammad Anis
Hi Vinay,

Please refer the below link for the above explanantion as:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_attr_values.htm

Hope this will clear the difference between the two.

Please mark this as best answer if your query is resolved.

Thanks,
Mohammad Anis
Mohammad AnisMohammad Anis
Hi Vinay,

You can use "set" as:

packItem : function(component, event, helper) {
event.getSource().set('v.disabled',true);
component.set("v.item.Packed__c", true);}

I have tested the same in my Dev. Org. and It is working.

Please mark this as best answer if your query is resolved.

Thanks,
Mohammad Anis
Emanuele VaccaEmanuele Vacca
The cleanest and sintactically correct solution:

----------------------------COMPONENT---------------------------
 
<aura:component >
    <aura:attribute name="item" type="Camping_Item__c" required="true" default="{Name:'Tent', Price__c:100, Quantity__c:1, Packed__c:true}"/>
    <p>{!v.item.Name}</p>
    <lightning:formattedNumber value="{!v.item.Price__c}" style="currency" currencyCode="USD"/>
    <lightning:formattedNumber value="{!v.item.Quantity__c}" maximumFractionDigits="0"/>
    <lightning:input type="toggle" label="Packed?" name="packed" checked="{!v.item.Packed__c}"/>
    <lightning:button label="Packed!" onclick="{!c.packItem}"/>
</aura:component>

----------------------------CONTROLLER---------------------------
 
({
    packItem : function(component, event, helper) {
        component.set("v.item.Packed__c", true);
        event.getSource().set('v.disabled',true);
    }
})