12

In Visualforce apex:variable can be used to avoid the repetition of a Visualforce expression.

I have a collection returned from an @AuraEnabled method for which I want to do the same sort of thing that apex:variable does:

<aura:iteration var="item" items="{! v.items }">
    <!-- aura:variable does not exist: is there a way to do this in Lightning? -->
    <aura:variable var="sob" value="{! item.reference.sobReference }"/>
    <tr>
        <td>{! sob.Name }</td>
        <td>{! sob.Birthdate }</td>
        ...

Is there a way to do this in Lightning Components?

1
  • 3
    The ever awkward sob reference.
    – Adrian Larson
    Aug 6, 2018 at 15:03

2 Answers 2

1

Yes,

You can use an attribute in the lightning component, and then set the value in your controller.

See

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

and

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

Edit: It seems like you're just trying to alias an attribute you already have access to.

<aura:iteration var="item" items="{! v.items }">
<tr>
    <td>{! item.reference.sobReference.Name }</td>
    <td>{! item.reference.sobReference.Birthdate }</td>

    ...
2
  • Ross, I've tried various combinations of aura:attribute and aura:set (to assign the attribute inside the loop - don't see how to use the controller here) but none that I tried would save. Can you post a working example in your answer?
    – Keith C
    Mar 29, 2017 at 13:52
  • Yes I am trying to avoid the repetition of the common expression .reference.sobReference in the same way you would use a local variable in other programming languages.
    – Keith C
    Mar 30, 2017 at 7:00
0

The only pattern I've seen that works is to wrap your iteration in a custom rowComponent where attributes inside the row represent each of your nested expressions (for complicated objects).

So then you pass your sob in as a top level attribute and then break down each nested reference using something like <aura:attribute name="sobName" default="{! v.sob.name }"/>

You must log in to answer this question.

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