how can I get a list of object from my sling model to js or html ? | Community
Skip to main content
Level 4
August 12, 2022
Solved

how can I get a list of object from my sling model to js or html ?

  • August 12, 2022
  • 1 reply
  • 2233 views

I have a list of object of type say <ABC> 

which I am accessing in my HTML file as :    

                                                                   data-sly-list.navItem="${model.myList}"
-> I am able to access the objects and their respective properties via this
-> but I want this list of object of type<ABC> in my js file 
-> currently I am accessing it from the attribute as : 
                                                               <p     data-myList="${model.myList}">
->when I am accessing it in my js file it's returning as string with the output like :
 
-> how can I get it as list of object , so that I can use it in my js code
 
 
 
Please help me find out 
Thank you

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Sachin_Arora_

You can send Json Array( as a String) of Breadcrumb List from the Sling Models to data-myList attribute in HTML. 

You can use GSON for POJO to JSON conversion or you can write logic to create JsonArray containing data of breadcrumb as a JsonObject. Then in your getter method, you can send the jsonArray.toString() so that return type is String and understood by JS.
https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/google/gson/JsonObject.html

http://www.javased.com/?api=com.google.gson.JsonObject

 

You can refer data-cmp-data-layer attribute of Core components which is created for similar purpose.

1 reply

Sachin_Arora_
Community Advisor
Sachin_Arora_Community AdvisorAccepted solution
Community Advisor
August 12, 2022

You can send Json Array( as a String) of Breadcrumb List from the Sling Models to data-myList attribute in HTML. 

You can use GSON for POJO to JSON conversion or you can write logic to create JsonArray containing data of breadcrumb as a JsonObject. Then in your getter method, you can send the jsonArray.toString() so that return type is String and understood by JS.
https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/google/gson/JsonObject.html

http://www.javased.com/?api=com.google.gson.JsonObject

 

You can refer data-cmp-data-layer attribute of Core components which is created for similar purpose.

Level 4
August 12, 2022

yes currently doing it via JSON only , is there any better approach to do it ?

 

Sachin_Arora_
Community Advisor
Community Advisor
August 12, 2022

There are other ways as well like using AJAX but this approach is good to go. Analytics integration is also following same design.
https://experienceleague.adobe.com/docs/experience-manager-core-components/using/developing/data-layer/integrations.html?lang=en