I feel like I am duplicating a lot of javascript code while creating helpers for different but related components. Is there a way to have like a shared helper, or maybe a global helper. What other techniques are there to share javascript code between components?
-
2+1 from me! Somewhat similar to Best Practice - Lightning JS (Code reuse, testing frameworks and IDE support) if the answers there might help you.– Adrian Larson ♦Commented Sep 20, 2016 at 17:59
-
Hey Chris, I've added an example of what I was describing in my example - it works well and is abstract enough to be called anywhere. Plus, no static resources! @DougChasman is this technique ok?– Caspar HarmerCommented Sep 21, 2016 at 9:27
2 Answers
EDIT 3 - Even better you can now return a value from your called method! This means that the technique below is useful for ASYNC calls, but in many instances, you can skip the callback and just get the result directly into a var
EDIT 2 - Salesforce supports this - see bottom.
EDIT Added Example
I can think of one way you could achieve this.
You would create a component that you would include in your markup. This component would expose a <aura:method>
that would perform your work.
Your could pass in parameters to the method like you would in any method
.
For return values, you could define a callback as an attribute that would call a function defined on the parent, or you could use a component event to broadcast the result back to the parent
I have an example of how to implement the callback in an answer here
Here is how I would implement this. First define the utility component:
<aura:component >
<aura:method name="sayHello" action="{!c.greet}" >
<aura:attribute name="personName" type="String" default="Doug" />
<aura:attribute name="callBack" type="function" default="" />
</aura:method>
</aura:component>
With the method greet
being:
({
greet : function(component, event, helper) {
var params = event.getParam('arguments');
if (params) {
var personName = params.personName;
var callBack = params.callBack;
callBack('hello ' + personName);
}
}
})
Then to call the helper (and get a response) from the parent, you would do this:
<aura:application >
<aura:handler name="init" value="{!this}" action="{!c.init}" />
<c:UtilityMethods aura:id="utilityMethods"/>
</aura:application>
And call the method on the utility object here, passing in the callback:
({
init : function(component, event, helper) {
var utility = component.find("utilityMethods");
utility.sayHello("Caspar", function(result){
alert(result);
});
}
})
This works well - and it's supported - see image here:
Note the attribute type - function. I've changed my example above to reflect this. Reference on the Salesforce Developer Relations Blog.
-
This works great. Thank you so much. I feel like using the static resource will work too, but I feel that this is an easier to use method, since you can work with the code in the same editors, and you don't have go through all the steps to re-upload a static resource each time you make a change. Commented Sep 21, 2016 at 14:53
-
1I've created a more robust version of this which is similar to a Service Component. It's got a long ways to go, but it sets some of the methodology taking this to the next level: github.com/tsalb/sfdc-lightning-service-components– tsalbCommented Nov 1, 2017 at 6:27
So if you want to have code which is very common and could be used by various helper then one approach could be move such code into one js and upload it as static resource. That way that js could be referenced in many components (though a single js would be loaded) but you will achieve the code res-usability.
This could act as a global helper.
-
This is an interesting idea, but needs more detail to really be an answer. How and where would you incorporate the
Static Resource
?– Adrian Larson ♦Commented Sep 20, 2016 at 17:51 -
1I think this is a good idea. I forgot about static resources. I wonder how well this would work with helper functions which call apex controller code. The downside is that the code will be more difficult to edit I think static resources have to be uploaded as a zip file. Commented Sep 20, 2016 at 18:38
-
For the example part Praveen has given quite elaborative answer in : salesforce.stackexchange.com/questions/136760/… Yes @Chris, Agreed on the difficulty in editing part. So I think this should be used only when things are settled and you have some utility javascript methods which you don't want to repeat. Commented Sep 25, 2016 at 16:33