Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 5

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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/JsonO...

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.

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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/JsonO...

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.

Avatar

Level 5

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

 

Avatar

Community Advisor

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-lay...