2

I know I can use many of the items found in this documentation such as jQuery

https://docs.joomla.org/J3.x:Javascript_Frameworks

by doing something simple like this JHtml::_('jquery.framework', false);

However, say I want to use React, I know I can include it using addScript but what if I wanted to use it for multiple modules/plugins/components. Is there a way to make it globally available like jQuery is?

2

Yes. This will be short -- you need to understand the code you're working with because this technique is one of the most powerful ones Joomla affords you and there is basically no documentation of it apart from the code itself, so do it only after tracking through the code and understanding the ramifications.

HTMLHelper has a register method you can call to register a routine of your own with it.

JHtml::register('react', MyClass::loadReact)

Once this gets executed, any call to JHtml::_('react') will execute the loadReact method in the class MyClass. The basic idea is it keeps a list of keys that point to methods it needs to call, none of which are required to be members of itself. Then, when told to call that key, it looks it up and executes it. It's the same mechanism used to load bootstrap and jquery today.

  • And as I would not know if extension a, b or c is installed or any combination, I assume registering twice does not cause a problem and only registers once? Or is it better to check first prior to registering? – Eoin Jun 20 at 19:29
  • 1
    Last registration wins; every registration for a term overwrites the previous one. Which means for your case you'll probably want to create your own name for it (just in case someone registers without looking first) and yes, you can check with JHtml::isRegistered('my.framework') before registering if you want to be really clean about it. – Arlen Jun 21 at 21:17
  • 1
    I just noticed, my example uses 'react' but in the explanation I use 'my.framework'. Sigh. – Arlen Jul 16 at 13:26
  • Thanks for coming back to correct – Eoin Jul 16 at 14:50

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.