Sharing JavaScript Code Using Service Components, https://douglascayers.com/2018/12/29/sharing-javascript-code-in-lightning-aura-components-and-freezing-javascript-prototypes/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ServiceComponent.cmp --> | |
<aura:component> | |
<aura:method name="sum" action="{!c.handleSum}"> | |
<aura:attribute name="a" type="Integer" required="true"/> | |
<aura:attribute name="b" type="Integer" required="true"/> | |
</aura:method> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ServiceComponentController.js */ | |
({ | |
handleSum: function(component, event, helper) { | |
const params = event.getParam( 'arguments' ); | |
return ( params.a + params.b ); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- TryServiceComponent.cmp --> | |
<aura:component> | |
<c:ServiceComponent aura:id="serviceComponent"/> | |
<lightning:button label="Add Numbers" onclick="{!c.handleClick}"/> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TryServiceComponentController.js */ | |
({ | |
handleClick: function(component, event, helper) { | |
const service = component.find("serviceComponent"); | |
let sum = service.sum(2, 3); | |
console.log(sum); // 5 | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment