0

I want to share code that calls server for data ... like a service called from multiple components that are not in same containment hierarchy. I want to do this on a lightning component in salesforce community. I read we can use aura:method to call the methods of one component from a different componentv (not parent/child relationship).

Does the aura:method work between completely unrelated components ? If yes, please provide sample code. Or if there is any alternate approach, please let me know.

Thanks for your time and help

2

You can create an abstract component and extend it in all the component. In the helper of that component put the re-usable code and if you extend the abstract component you inherit the helper methods which you can re-use.

One of the big use cases for that we use abstract component for is making calls to server from lightning component. Since the structure and most of the code is the same we created a abstract component and placed the call server code in its helper and called it from all components

Abstract Component:

<aura:component extensible="true"  >

Helper of abstract component:

callServer : function(cmp, method, callback, params, cacheable) {
 var action = cmp.get(method);
    if (params) {
        action.setParams(params);
    }
    if (cacheable) {
        action.setStorable();
    }
    action.setCallback(this,function(response) {
    });
    $A.enqueueAction(action);
}

In all the future components that i create where i want these methods i will just extend them

<aura:component controller="ABC Controller" access="global" extends="c:abstractcomp">

In the helper of this sub component i call the super component helper method by doing

this.callServer(component, "c.abcmethod", function(response) {
            }, {
                Taxid: '124'
            }, false);

I can create a bunch of re-usable methods put it in a utility component and extend it across other components wherever i need to use them.

  • Hi,Thanks for the code. I was able to implement it. But I also found out we can achieve code resuability using <aura:method in the following link (last option) developer.salesforce.com/blogs/developer-relations/2016/12/… . I personally felt this is better way because I can call multiple service components inside one single component, whereas in your above approach since I can extend only one controller, I cannot access more than one Apex controller. – Raja Vivekanandhan Jul 6 '17 at 0:07
  • 1
    Sure, if it works better for your scenario then you can use it the only reason i posted this solution was because you had asked for sharing code between unrelated components. If you use aura:methods then you can only call methods which are in the child containment hierarchy of that component so they have to be related in some way. – RedDevil Jul 6 '17 at 1:21
  • yes, that is right on aura:method that I need to include the component tag on wherever component I wanted to use. Since the parent component is an invisible component , i do not mind it to include in the components where i need to use the service methods. – Raja Vivekanandhan Jul 6 '17 at 2:12
0

We can achieve code resuability in multiple ways. I was able to achieve using aura:method in the following link (last option) developer.salesforce.com/blogs/developer-relations/2016/12/… . I personally felt this is better way because I can call multiple service components inside one single component, whereas in your above approach since I can extend only one controller, I cannot access more than one Apex controller. So the aura:method suits my requirement better

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.