0

Is it possible to have two aura:id for one lightning component?

I am feeling that I have an x-y problem, but I can not think any other solution her. So, let me explain the issue.

In a nutshell I have the following lightning app architecture:

Parent component(pc) has two child components (c1, c2). pc can have different components in place of c1 and c2. Sometimes c1 may dispatch a component event which pc should handle. pc should handle the event as follows: check that c2 has a specific component function if it does have it pc should call it.

In order to check for the function presence in c2 I need to access the component at first. I would like to use aura:id for that. But the thing is that components which may be in place of c2 may have different aura:id. So, I was hoping to add a second aura:id to c2 to extract whatever component there might be. In other words I would like to us aura:id here as an interface in OOP.

If you have different propositions on how to solve that I would be glad to hear them. In case I did not provide some information ask me and I will provide it.

  • Every component has a globalId; "getGlobalId () Gets the globalId. This is the generated globally unique id of the component. It can be used to locate the instance later, but will change across page loads.". Does that help? – Keith C Sep 6 '18 at 11:46
  • 1
    You're definitely in X-Y problem territory. You cannot have more than one aura:id on a component. You might want to include some sample code so we can better assist you with your problem. – sfdcfox Sep 6 '18 at 11:47
  • @sfdcfox, I updated the question a bit. What I want is basically to have an interface for the component selection. – iloveseven Sep 6 '18 at 11:51
  • @KeithC, I updated the question a bit. What I want is basically to have an interface for the component selection. – iloveseven Sep 6 '18 at 11:51
4

Presuming that you're dynamically creating components (e.g. c1 and c2), you have total control over the aura:id. A component cannot define its own aura:id, only the aura:id for children within itself. From here, it follows that you're trying to figure out what type of component you're dealing with. For that, we use Component#getType. This tells you exactly what type of component you're dealing with.

For example:

var c2 = component.find("child2");
if(c2.getType() === "c:someComponent") { ...

Alternatively, if you're exposing actual aura:method references, you can also check for the presence of the function before calling it:

var c2 = component.find("child2");
if(c2.someMethod) { ...

Finally, if you are using interfaces, you can also check if a component is a type of interface:

if(c2.isInstanceOf("c:interfaceName")) { ...
  • You can also do something like this: component.find("buttons").find({ instancesOf : "lightning:button" }) where "buttons" is an aura:id given to a div wrapping where dynamic children will live. – tsalb Sep 6 '18 at 20:35

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.