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.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityI 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.
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.
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" />
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
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.
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?