I have components some of which use the same function (in client-side JavaScript controller). At this moment i resolved this problem putting this function in each component. But it isn't comfortable :( I tried to use tag aura:method with access="global" in main component and invoke it from child ones but it isn't work.

How to resolve this problem without using events?

  • 1
    If its a common method make it in separate class as static method and pass necessary values as parameters. – Reshma Mar 22 '17 at 11:55
  • @Reshma This is referring to JavaScript, not Apex Code. – sfdcfox Mar 22 '17 at 11:58

The only way to "share" at the moment is to use the extends keyword. If you create a component, and then use the extends attribute on a child component, that component's helper are available to the child. See What is Inherited? for additional things that are inherited when you extend a component.

  • From 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." – TPEHEP Mar 22 '17 at 12:16
  • @TPEHEP They're referring to the controller, not the helper. That quote also mentions that you should put shared methods in the helper. – sfdcfox Mar 22 '17 at 12:17
  • @sfdxfox It's perfect! I created new extensible component with neccessary methods in helper and markup {!v.body}. Than extended components which use that methods and magic )). Helper methods in extensible component getting in "proto" property of extended component helper. Thank you very much for help! – TPEHEP Mar 22 '17 at 12:42

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.