5

I know that I can hook a component/app to a controller like this:

<aura:component controller="IncidentsController">
</aura:component>

However, is it possible to get this component to communicate with more than one controller?

Thanks.

  • I don't think it is possible.. In VF page as well we use extension for multiple controllers.. But I am commenting here because I am not sure. – Ratan Paul Jan 18 '16 at 7:38
  • Can I use an extension with aura? – eyal83 Jan 18 '16 at 11:04
  • I don't think so...but what exactly are you trying to accomplish...what is the logic you want to put in the "extension" as opposed to the controller? – pchittum Jan 19 '16 at 1:04
  • I have a lightning component which includes javascript which is basically a React + Flux application. I can communicate with the Apex server using the lightning component. However, I can only communicate with 1 controller. If I need to communicate with a different controller, I will need to a add another component just so it will associated with a different controller. Does that make more sense? – eyal83 Jan 19 '16 at 5:36
  • Based on this post you can't do that unless I am misunderstanding. I want to do the similar thing but with remote object. But after my research I only can implement lighting component with aura. I hope I am wrong. salesforce.stackexchange.com/questions/100412/… – user3211 Jan 20 '16 at 18:32
4

Yes and No , no way so far for multiple apex controllers link to one aura component. but component allows include child components or inherited from super component. Each component has one controller.

using inheritance

<!-- BaseComponent -->
<aura:component extensible="true" controller="BaseComponentController">
</aura:component>

<!-- child component -->
<aura:component extends="BaseComponent" 
controller="childComponentController">
</aura:component>

Invoke Base component helper method from child component, then invoke BaseComponentController methods.

for example in childhelper.js

doStuff: function(component,event, helper){
  //call base component helper deleteRecord method
  helper.deleteRecord(component);

  //call apex childComponentController method doStuff
  var action = component.get("c.doStuff");

}

in basecomponentHelper.js

deleteRecord : function(component){
   var baseCmp = component.getConcreteComponent();
   //call apex BaseComponentController delRecord function
   var action = baseCmp.get('c.delRecord');

}

or adapt the same idea to invoke Apex controller from child component.

|improve this answer|||||
3

I think your best option is using component.find and helper components:

<aura:component>
   <c:OpportunitiesSelector aura:id="oppsHelper" />
   <c:ContactsSelector aura:id="conHelper" />
   <ui:button press="{!c.doSomething}" label="Do Something" />
</aura:component>

Then in controller you'd go:

doSomething : function (component) {
    var action = component.find('oppsHelper').get('c.getOppsById');
    action.setParams({
        ids: [123456789012345678, 098765432187654321]
    });
    var action2 = component.find('conHelper').get('c.getContactsByEmail');
    action2.setParams({
        email: 'foo@baz.qux'
    });
    $A.enqueueAction(action);
    $A.enqueueAction(action2);
}

All your OpportunitiesSelector has to have is:

<aura:component controller="OpportunitiesSelector" />
|improve this answer|||||
1

You cannot use two Apex controller in Lightning Component, but you can Extends the Component and use Super Component controller action in sub component.Look at this blog

https://developer.salesforce.com/blogs/developer-relations/2015/03/salesforce-lightning-components-by-example-component-extension.html

However do not call Super component controller action in Sub component, because I found one note in Developer Guide

We don't recommend using inheritance of client-side controllers as this feature may be deprecated in the future to preserve better component encapsulation. We recommend that you put common code in a helper instead.

|improve this answer|||||
0

Referencing multiple Server-Side Apex controllers Classes within a Lightning Component is not supported. I don't have a specific reference to support that statement, but consider this from the Lightning Components Developers Guide. When we want to call a Server-Side Action from a component, we make a reference to the method in the client side controller like this:

    // create a one-time use instance of the serverEcho action
    // in the server-side controller
    var action = cmp.get("c.serverEcho");

The cmp.get("c.serverEcho") call indicates that we’re calling the serverEcho >method in the server-side controller. The method name in the server-side >controller must match everything after the c. in the client-side call. In this >case, that’s serverEcho.

SO... if we want to make references to two different Server-Side Apex controller Classes, we would need a means to identify the two Apex Classes as, let's say, "c1" and "c2". Only then would we be able to make a reference methods from one or the other... that's currently not supported.

Does somebody want to create an Idea in the Idea Exchange?

|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.