Hey i have a component in which i have created two view mobile view and desktop view, i have used two different resources for the same call. i need to minimize number of call to one. how do i use one resource and pass the values to both of them
Views
Replies
Total Likes
Hi @RaunakRa2 , Can you please share the code snippet you have created with 2 resource calls
Without seeing the actual code, it's challenging to provide a context-aware response.
However, you can achieve your goal by creating a shared template that both views can use while initializing your resource or Sling Model only once. Here's how to do it:
<!--/* Initialize your resource/model ONCE */-->
<sly data-sly-use.model="com.example.MyComponentModel"/>
<!--/* Define template for both views */-->
<template data-sly-template.mobileView="${@ model}">
<!--/* Mobile-specific markup using ${model.property} */-->
<div class="mobile">
${model.mobileContent}
</div>
</template>
<template data-sly-template.desktopView="${@ model}">
<!--/* Desktop-specific markup using ${model.property} */-->
<div class="desktop">
${model.desktopContent}
</div>
</template>
<!--/* Render appropriate view */-->
<sly data-sly-call="${desktopView @ model=model}"></sly>
<sly data-sly-call="${mobileView @ model=model}"></sly>
The data-sly-use initializes the model once at the root level
Templates accept the pre-initialized model as a parameter
Both templates share the same model instance
@RaunakRa2 Did you find the suggestions helpful? If you need more information, please let us know. If a response resolved your issue, kindly mark it as correct to help others in the future. Alternatively, if you discovered a solution on your own, we'd appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes