0

Faced an issue where unbound expressions are not working as expected. Below is the scenario.

Parent component:

<aura:component implements="force:appHostable" controller="test1" >   
<aura:attribute name="campingItem" type="Camping_Item__c" default="{ 'sobjectType': 'Camping_Item__c'}" />    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>    <aura:attribute name="campingItemList" type="Camping_Item__c[]"/>


    <c:CampingItemList campingItems="{#v.campingItemList}" campingItem = "{#v.campingItem}"/>   

</aura:component>

Parent Controller:

({
    doInit : function(component, event, helper) {
        var action = component.get('c.getcampingItems');  

        // Configure response handler
        action.setCallback(this, function(response) {
            var state = response.getState();

            if(state === "SUCCESS") { 
                console.log('Working: ' + state); 
                component.set('v.campingItemList', response.getReturnValue()); 
                console.log('list: ' + component.get('v.campingItemList'));
            } else {
                console.log('Problem getting account, response state: ' + state);
            }
        }
        $A.enqueueAction(action);               

        component.set('v.campingItem.Name', 'check1'); 
    }
});

Child Component:

<aura:component implements="force:appHostable" controller="test1"> 
  <aura:attribute name="campingItems" type="Camping_Item__c[]"/>

<aura:attribute name="campingItem" type="Camping_Item__c" default="{ 'sobjectType': 'Camping_Item__c'}" />       

<ui:message aura:Id="msg" title="Error" class="slds-hide" severity="confirm" closable="true">        Successfully deleted ID    </ui:message>   

ABC = {!v.campingItem.Name} 

</aura:component>

Here, we are getting that value of the attribute campingItem in the child which we are setting it in the doInit of the parent i.e. check1 which is not supposed to be as per the developer guide as it is the unbound expression. But with list campingItemList its working as expected mean not getting the value.

2 Answers 2

1

Please check the below documentation link which very clearly says if you are going to use init events to load data for attributes which will get passed to other components then you have to use bound expressions. Unbound will not work for these cases

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

Don’t use a component’s init event and client-side controller to initialize an attribute that is used in an unbound expression. The attribute will not be initialized. Use a bound expression instead. For more information on a component’s init event, see Invoking Actions on Component Initialization.

Alternatively, you can wrap the component in another component. When you instantiate the wrapped component in the wrapper component, initialize the attribute value instead of initializing the attribute in the wrapped component’s client-side controller.

5
  • I do agree but my question is why the value is passed from the doInit to the child when we are using inbound expression. As it is not supposed to.
    – umang
    Commented Jun 26, 2017 at 10:40
  • Your question is not clear? Didn't fully understand the part 'why the value is passed from the doInit to the child when we are using inbound expression'
    – RedDevil
    Commented Jun 26, 2017 at 10:44
  • Here if you check, i am using two attribute campingItem and campingItem the same attribute is used in the child component as well but using unBound expression I am passing these values to the child. In doInit of parent I am setting the value of both the attribute one is object and other is list<Object>.
    – umang
    Commented Jun 26, 2017 at 11:11
  • Yep thats what the documentation says instead of 'campingItems="{#v.campingItemList}" ' change it to campingItems="{!v.campingItemList}" . That is the bound way of doing it
    – RedDevil
    Commented Jun 26, 2017 at 11:13
  • I am getting the same value of the object in the child component but I am using the unbound expression so It was not supposed mean I should not get any value of object in the child as I am setting it in the doInit. Also list<object> is working fine I am not getting the records in the list as aspected as it is an unbound expression
    – umang
    Commented Jun 26, 2017 at 11:15
-1

When we use Unbound expression, it works as a one-way binding. So if we are passing any value as an attribute to child component, then both of the components have the same value(at initialization time), but then if we update the value in any of the components, it doesn't update to another one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .