Skip to main content

Day 08: Dueling with JavaScript Use API



AEM Developer Series

Having learnt the nitty-gritty details of component development in AEM in the previous post, we will now explore further. In this post, we are going to look into the concepts of Use API.

This API lets us write server-side code which can be consumed by HTL. AEM provides support for two types of Use API - Java Use API and JavaScript Use API. In this post, we are going to learn about JavaScript Use API and in the next post, we will look into Java Use API.

JavaScript Use API

A JavaScript Engine (for e.g. Google's V8 Engine) is responsible for executing JavaScript in the browser. But when we are using Use API, what makes a JavaScript engine to execute it is Rhino. It is an open-source implementation of JavaScript written entirely in Java.

Since JavaScript is translated into Java, we have Java libraries at our disposal. To understand how we can use JavaScript Use API, let's take an example.

Example

  • Create a simple text component with a basic configuration as follows (how? see here)
Create a component
  • Create nodes and properties under the component node as per the following XML file
  • Now, create a new file text.js under the path  /apps/demoproject/components/content/text  and paste the following code in it
  • Here, note that we are using use(function()) which signifies the usage of the Use API and in the function, at line #4, we are initializing a JS object call text
  • Then, we created two members of the text object, title and description. we are setting these with the JCR properties title and description respectively. Note that to access these values from the JCR, we are using granite.resource.properties[] object.
  • In line #9, we are returning the text object.
  • Now rename text.jsp to text.html and paste the following code in it.
  • Here we are using data-sly-use.text="text.js" which includes the JS file in the HTML code. You have to use the right path of the file if the HTML file and the referred file are not in the same location. This code gets the text object and accesses the properties via dot (.) operator.
  • Now drag and drop the component on the page and configure it using the edit dialog. You will see the passed values on your page.

Conclusion

Congratulations!! 🙋 you have duelled with JavaScript Use API which can be used to write server-side logic. I hope you enjoyed this post.

You can find the complete code of this project on my GitHub. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like to have suggestions from you to make it better. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊



Comments

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see that the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.

Adobe was way ahead in this war and have gained a lot of market capture. They are leaders in the Digital Marketing platforms since the 2014-15. One of the flagship product in its Digital Marketing suite is Adobe Experience Manager (AEM).

Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.

When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the …

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle