how can I get a list of object from my sling model to js or html ? | Adobe Higher Education
Skip to main content
Level 4
August 12, 2022
해결됨

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

  • August 12, 2022
  • 1 답변
  • 2233 조회

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

 

이 주제는 답변이 닫혔습니다.
최고의 답변: 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 답변

Sachin_Arora_
Community Advisor
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.

rahul234dabas작성자
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