Expand my Community achievements bar.

SOLVED

Using Backend Javascipt Use-API in AEM HTL

Avatar

Level 2

I need to create a component that uses the tag title (jcr:title) set in ContentFragment. The tag title cannot be obtained by the core component, so I need to use the TagManagerAPI, in which case I need to write the code in either Java Use-API or Javasciprt Use-API.

I prefer to use Javacript Use-API because I have more experience with Javascript and it requires less code to write. However, Adobe's documentation shows Java Use-API, but Javascript Use-API is only mentioned on some of the pages.

 

Java Use-API
https://experienceleague.adobe.com/docs/experience-manager-htl/content/java-use-api.html?lang=en

 

Javascript Use-API
https://experienceleague.adobe.com/docs/experience-manager-htl/content/getting-started.html?lang=en#...

 

The differences between the APIs are described below, but I wonder if using the Javascript Use-API is not recommended because it is slow and difficult to unit test. I would like to know if there are any problems in using it.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-htl-use-api-aem-commun...

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@satoshi1 

You can use JavaScript Use API for simple requirements.

  1. Yes, it is bit slow compared to Java Use API as there is an extra processing step which converts the JavaScript to Java. But as there is a caching mechanism it is ok.
  2. Yes, you can not write unit test as the script resides in ui.apps along with component. But again as this is only for one component nothing to worry.

Writing code using Java Use API, Sling Models is very simple give a try.

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

@satoshi1 

You can use JavaScript Use API for simple requirements.

  1. Yes, it is bit slow compared to Java Use API as there is an extra processing step which converts the JavaScript to Java. But as there is a caching mechanism it is ok.
  2. Yes, you can not write unit test as the script resides in ui.apps along with component. But again as this is only for one component nothing to worry.

Writing code using Java Use API, Sling Models is very simple give a try.

Avatar

Community Advisor

@satoshi1 If it is for simple manipulations with the content , then use Javascript API. But ideally all the business logic should go into your Java. In your case, I don't think there should be any issue in using the Javascript for fetching the tag title , if you are confident about it. 

Avatar

Community Advisor

Its not about you are more comfortable with Javascript or Java, its about the implementation that too with best practices, and best practice says use Java only in this scenario. 
Think about the advantages you will have if you go with JS, apart from your comfortability you are not going to get any thing out of it because AEM ultimately converts that JS to Java( this will make your implementation slow), Getting tagmanager an all are not going to be an easy task to work with in js and as you mentioned, that you can not write test cases for that, this is again going to be a headache for you.

I would say use java only because it is simple, easy and will be based on best practices of the time. Even you know basics of java you can easily write the logic for your requirement.

Hope this helps.

Umesh Thakur

Avatar

Level 2

@Anudeep_Garnepudi @Umesh_Thakur @VeenaVikraman 

Thank you all for your very helpful input!

I understood that the best practice is to write the logic in Java Use-API.
I would like to try Java Use-API.