0

Based on this article we know that there are 2 types of binding:

{!v.parentAttr} and {#v.parentAttr}

As we know, one time binding ({#...}) does not set up event listeners for change of attribute so this is more efficient. In my components I use Object field labels that I get from Apex and bind using two-way binding.

<aura:attribute name="labels" type="String[]"/>

Usage as table headers:

<aura:iteration items="{!v.labels}" var="label">
    <lightning:layoutItem class="slds-truncate">
        <div title="{!label}">{!label}</div>
    </lightning:layoutItem>
</aura:iteration>

And I retrieve labels with controller:

action.setCallback(this, function(response) {
    if (response.getState() === 'SUCCESS') {
        component.set("v.labels", response.getReturnValue());
    }
});

Apex:

@AuraEnabled
public static List<String> getLabels() {
    return new List<String>{
        MyObject__c.MyField1__c.getDescribe().getLabel(),
        MyObject__c.MyField2__c.getDescribe().getLabel(),
        // ...
    };
}

I want to use one-time binding for this. One of possible options I see is to create a wrapper component, where I would retrieve labels and in callback dynamically create child component with one-time binding.

Is this the only way to achieve this?

1

You can't use a callback handler to update an unbound variable, because the other elements won't realize that anything's happened. The same article you quoted states:

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.

Unbound data expressions are primarily useful for binding to a default value (aura:attribute default=...) or using configuration values that are preconfigured (such as using a design token).

Since you need the update to be detected, you'll need a normal data binding.

| improve this answer | |

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.