Hi, Guys,
I am new to AEM and I am facing a problem about converting Resource to Custom Model.
//--------------------------------------------------
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @TrifaCh
Data sly resource will include the component as a whole. If you want to just read one property from another component in a different page this will not help.
You can get to that page and iterate over resources and read that property .See a similar snippet in below URL.
Hi @TrifaCh, Can you share your CustomText class implementation? It will be easier to understand the issue.
In some cases, the adaptTo() method might return null due to various reasons, which include:
For more details, you can refer to the documentation at: Adobe Experience Manager - Sling Adapters
@Model(adaptables = SlingHttpServletRequest.class, adapters = { CustomText.class,
ComponentExporter.class }, resourceType = CustomTextImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class CustomTextImpl implements CustomText {
static final String RESOURCE_TYPE = "application/components/text";
@ValueMapValue
private String text;
@ValueMapValue
private int size;
@Self
private SlingHttpServletRequest request;
@Override
public String getText() {
return text;
}
@Override
public int getSize() {
if (size > 0){
return size;
}
return 14;
}
@Override
public String getExportedType() {
return CustomTextImpl.RESOURCE_TYPE;
}
}
Hi @TrifaCh, You are currently adapting Resource to CustomText; however, the Resource.class is absent in the @Model adaptables properties. To resolve this, make sure to include Resource.class in the @Model annotation's adaptable properties. This addition will enable the adaptation of Resource to CustomText.
// SlingHttpServletRequest not required since no use of request
@Model(adaptables = Resource.class
....)
Thank you for replying. There are some component can be adapted from Resource after adding it. However, some components still not work. Maybe there are other reasons affect the progress of adapting.
Hi @TrifaCh
Sling offers an Adapter pattern to conveniently translate objects that implement the Adaptable interface. This interface provides a generic adaptTo() method that translates the object to the class type being passed as the argument. In your case it returns a null because I believe the implementation is not supported here .
Please read the below documentation to know more about supported resource adaptions
On the tasks you had queries
Components are independent of pages. If both the components are allowed on the templates that is used to create page A and page B you can directly add component B in page A if its allowed in layout container policies.
Another approach is you can include component B through code . Place the code on the template for page A or in component A html
eg.
<sly data-sly-resource="${resource @ resourceType='wcm/foundation/components/responsivegrid'}"></sly>
where "wcm/foundation/components/responsivegrid" is path to your component.
Hope this helps
Thank you for replaying. But I have a question about implementation.
<sly data-sly-resource="${resource @ resourceType='wcm/foundation/components/responsivegrid'}"></sly>
How should I access the resource of component B? For example, there is a get function in component B, how should I call it in the component A.
Thank you very much.
Hi @TrifaCh
Data sly resource will include the component as a whole. If you want to just read one property from another component in a different page this will not help.
You can get to that page and iterate over resources and read that property .See a similar snippet in below URL.
Thank you very much. I think your sharing is useful. I may need to use it in the future.
Views
Likes
Replies