1

My problem is related with the 'change' event on a component attribute.

I only want to execute a code after this attribute is initialized by the parent component. For example:

    <!-- CHILD COMPONENT -->
    <aura:component>
        <aura:attribute name="inputValue" type="String" />
        <aura:handler name="change" value="{!v.inputValue}" action="{!c.processChange}"/>
    </aura:component>

    <!-- PARENT COMPONENT -->
    <aura:component>
        <aura:attribute name="obj" type="Object" />
        <c:ChildComponent inputValue="{!v.obj.value}"/>
        <button onclick="{!c.changeObjectValue}">Change Value</button>
    </aura:component>

Here, the 'ChildComponent' is initialized with 'inputValue' as undefined, so when the 'obj.value' is assigned to this attribute, the 'change' event is triggered despite this is a initial value.

I want to detect whether the change is from an initial value or the change has been made later. Is there a design pattern for this example?

3

I am assuming you are loading values for parent attribute from init handler thats why it gets set as undefined initially. '

There are few ways to handle it:

  1. Put the child components inside an aura:if and load them once the parent init has fired and the attributes on the parent especially the ones passed to child components are set.

  2. Set an attribute on init of child component and the first time value change event is fired set it to false which indicates it was fired right after init.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.