Reading values between AEM Models? | Community
Skip to main content
Level 6
March 1, 2023
Solved

Reading values between AEM Models?

  • March 1, 2023
  • 3 replies
  • 1194 views

I hava page in AEM that has a fixed component B and can have one or more components A. The value (multifield paths) of components A (@ValueMapValue) are then used in the logic of component B. 

Components A and Component B are not nested components. They are separate components with their own dialog and values. Component B has to iterate through all the components A that are configured on the page and use the values from components A to build its business logic. What would be a suggested way to solve this?

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 Kiran_Vedantam

Hi @anasustic 

 

You can get the

  1. currentPage in model
  2. iterate over the node using node API
  3. Check for componentA's resourceType and if exists read all the components under it (You can easily adapt each component authored under A to their models) and fetch the value for your business logic.

Hope it helps!

Thanks,
Kiran Vedantam.

3 replies

krishna_sai
Community Advisor
Community Advisor
March 1, 2023

@anasustic you can make use of querybuilder in sling model of component B to get no of components of type A present on the currentPage. 

path=/content/<project> 1_property=sling:resourceType 1_property.value=<ComponentA's resouceType> p.limit=-1


Using that result set you can do whatever you want based on your business logic.

Hope this helps,
Krishna

anasusticAuthor
Level 6
March 2, 2023

Hi @krishna_sai 

thanks for your answer. How can I do that programmatically in a model?

aanchal-sikka
Community Advisor
Community Advisor
March 1, 2023

Hello @anasustic

 

Option-1: Your usecase is very similar to a Accordian or Tabs component, where you could limit what components can be added.

Then the resourceType of component A itself can render it.

 

Option-2: Use nested Sling Models. Example: https://levelup.gitconnected.com/aem-how-to-use-nested-multifields-in-your-components-using-a-sling-model-vs-pure-htl-92ff8d036ba1

 

 

 

 

Aanchal Sikka
anasusticAuthor
Level 6
March 1, 2023

Thank you @aanchal-sikka 

 

My use case is not exaclty the Accordion or Tab usecase while my Component A und Component B are separate components. They are not nested. They are just allowed on the same page and Component B has to iterate through all the components A that are configured on a page to use these value in its  business logic.

Kiran_Vedantam
Community Advisor
Kiran_VedantamCommunity AdvisorAccepted solution
Community Advisor
March 1, 2023

Hi @anasustic 

 

You can get the

  1. currentPage in model
  2. iterate over the node using node API
  3. Check for componentA's resourceType and if exists read all the components under it (You can easily adapt each component authored under A to their models) and fetch the value for your business logic.

Hope it helps!

Thanks,
Kiran Vedantam.