Forum Discussion

Yulia-Anaplan's avatar
4 years ago

Any cons of NOT using LITHIUM.jQuery when calling an endpoint from a component?

Hi folks, 

I was wondering if using LITHIUM.jQuery when calling endpoints is the only preferred way of doing it:

 

<@liaAddScript>
	;(function ($) {
		$(document).ready(function () {
			function callLithiumEndpoint() {
				$.ajax({
					url: '${endpointUrl}',
				})
			}
			callLithiumEndpoint();
		});
	})(LITHIUM.jQuery);
</@liaAddScript>

 

 

Can it be done with vanilla JavaScript inside a <script> tag as well, for example using fetch?
I noticed that endpoints can be triggered from a browser or a postman via their path 

https://<community-name>/pjakv59666/plugins/custom/<some-path-to-endpoint>/<component-name>



Can it be treated as a simple url, passing there parameters and parsing it?

  • There are no specific issues with using vanilla javascript to call endpoints.

    Using LITHIUM.jQuery is recommended if you intended to use jQuery.

    Also, using liaAddScript is recommended if you need the code to be executed near the bottom of the page after OOB javascript has executed (It may also do some minification/obfuscation). 

    If none of those situations apply or are of concern, feel free to use plain JS in <script> tags. I would just encourage you to be mindful of code organization so the JS makes sense where it is placed, isn't repeated by multiple component includes, etc....

  • MattV's avatar
    MattV
    Khoros Staff

    There are no specific issues with using vanilla javascript to call endpoints.

    Using LITHIUM.jQuery is recommended if you intended to use jQuery.

    Also, using liaAddScript is recommended if you need the code to be executed near the bottom of the page after OOB javascript has executed (It may also do some minification/obfuscation). 

    If none of those situations apply or are of concern, feel free to use plain JS in <script> tags. I would just encourage you to be mindful of code organization so the JS makes sense where it is placed, isn't repeated by multiple component includes, etc....