explain component exporter, container exporter | Community
Skip to main content
sreenu539
Level 7
May 16, 2023
Solved

explain component exporter, container exporter

  • May 16, 2023
  • 1 reply
  • 991 views

I am looking at below code to reuse on one of the component to export child nodes/components as json.

@126844 @Override public Map<String, ? extends ComponentExporter> getExportedItems() { if (childModels == null) { childModels = getChildModels(request, ComponentExporter.class); } return childModels; } @NotNull @Override public String[] getExportedItemsOrder() { Map<String, ? extends ComponentExporter> models = getExportedItems(); if (models.isEmpty()) { return ArrayUtils.EMPTY_STRING_ARRAY; } return models.keySet().toArray(ArrayUtils.EMPTY_STRING_ARRAY); } /** * Returns a map (resource name => Sling Model class) of the given resource * children's Sling Models that can be adapted to {@link T}. * * @90521 slingRequest the current request * @90521 modelClass the Sling Model class to be adapted to * @2007960 a map (resource name => Sling Model class) of the given resource * children's Sling Models that can be adapted to {@link T} */ @NotNull private <T> Map<String, T> getChildModels(@NotNull SlingHttpServletRequest slingRequest, @126844 Class<T> modelClass) { Map<String, T> itemWrappers = new LinkedHashMap<>(); for (final Resource child : slingModelFilter.filterChildResources(request.getResource().getChildren())) { itemWrappers.put(child.getName(), modelFactory.getModelFromWrappedRequest(slingRequest, child, modelClass)); } return itemWrappers; }

 

 getChildModels() : how does this method adapting to specific component adapter type? I see from ModelFactory class that this method passes "ComponentExporter.class" as modelClass / adapterType. I would like to understand how ModelFactory using ComponentExporter.class as adapterType to fetch model json for child components/nodes.

 

Doesn't adapterType needs to be Button.class if button is a child component or Teaser.class if Teaser is a child component?

 

Thanks for any insights.

 

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 ManviSharma

Hi,

 

In the given code, the getChildModels() method uses the modelFactory to adapt child resources to the ComponentExporter model class. The ComponentExporter.class is used as the adapter type because it represents a common interface for exporting components. It doesn't have to match the child component classes (e.g., Button.class or Teaser.class). The Sling Model adaptation mechanism leverages annotations and configurations to map the child resources to the appropriate Sling Model implementation. This approach simplifies the process of exporting child components as JSON.

1 reply

ManviSharma
Adobe Employee
ManviSharmaAdobe EmployeeAccepted solution
Adobe Employee
May 16, 2023

Hi,

 

In the given code, the getChildModels() method uses the modelFactory to adapt child resources to the ComponentExporter model class. The ComponentExporter.class is used as the adapter type because it represents a common interface for exporting components. It doesn't have to match the child component classes (e.g., Button.class or Teaser.class). The Sling Model adaptation mechanism leverages annotations and configurations to map the child resources to the appropriate Sling Model implementation. This approach simplifies the process of exporting child components as JSON.