How to call the component Models asynchronously | Community
Skip to main content
Level 4
June 2, 2022

How to call the component Models asynchronously

  • June 2, 2022
  • 2 replies
  • 1980 views

Hello, in the component, I'm having trouble with Models calls. This page is taking an extremely long time to render.
I've made a page with five components, let's call them C1,C2,C3,C4,C5, and we've used C2 four times and C4 ten times.
The page's components are arranged in the following order: C1,C2,C2,C2,C2,C3,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,C4,
I've set a timer in the logs. I have observed page is rendering order of the components.
C1-300ms, C2-400ms, C2-400ms, C2-300ms, C2-300ms, C3-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400ms, C4-400 C4-400ms C4-400ms C4-400ms C4-400ms C4-400ms Finally, C4-400ms and C5-100ms are used.
As a result, it will take at least 10-15 seconds for the page to render.
How to call C1,C2,C3,C4,C5 components at the same time, rather than in the order they appear on the page?

 

 

 

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

2 replies

SantoshSai
Community Advisor
Community Advisor
June 2, 2022

Hi @kbitra1998 

you may take a look at Sling Model Caching. This does exactly what you want, as long as it is adaptable from Resource.

https://sling.apache.org/documentation/bundles/models.html#caching

You only have to specify cache=true in the Model-annotation.

@Model(adaptable = SlingHttpServletRequest.class, cache = true)
public class ModelClass {
   ...
}

Hope that helps!

Regards,
Santosh

Santosh Sai
RajaShankar
Community Advisor
Community Advisor
June 2, 2022

Hi @santoshsai 

It was a good pointer for caching the Adapted resource for all instance. But i have a doubt in this as mentioned by @kbitra1998  they are trying to use same component in multiple instance within same page exampleC1,C2,C2,C2 .If i cache the Sling model will it not return same attributes/values as that of first instance of C2 even for second and third case. LEt say if C2 first instance as a text value "This is text component1" in this case second and third instance also will get same value right."assert object1 == object2;"

Please clarify.

Regards,

Rajashankar.R

 

 

Level 4
June 3, 2022

@rajashankar 
No, Have a loot at below use case and result.

@Model(adaptables = Resource.class, cache = true)
public class HelloWorldModel {
private String message;
..............
}


@santoshsai , @rajashankar 

 

For all C2 components on the page, I'm getting the same value. @rajashankar I believe you are correct, however @santoshsai  How did you come up with the various values for the same component?

Could you please assist me in this issue?

 

If possible, a sample of the model's code would be very appreciated.

arunpatidar
Community Advisor
Community Advisor
June 2, 2022

Hi,

If your page are cached at dispatcher than this issue will not occurs.

I believe you are caching this page.

but if you just want to show as per type than simple javascript can do a job.

Arun Patidar