Expand my Community achievements bar.

Exporting CIF Core component Sling models via Model Exporter :: Facing Issues..

Avatar

Level 2

Hi All,
The CIF Core components for Adobe Commerce are implemented on Sightly at the moment. What we were trying to do is repurpose as much core component code as we can to create SPA components by extending core CIF components. 
Following is one such case where we were trying to export Search Results Component Sling Model by using Sling model exporter.
Interface Impl

 

 

 

package com.demoproject.core.models.impl;

import com.adobe.cq.commerce.core.components.models.common.ProductListItem;
import com.adobe.cq.commerce.core.components.models.searchresults.SearchResults;
import com.adobe.cq.commerce.core.components.storefrontcontext.SearchResultsStorefrontContext;
import com.adobe.cq.commerce.core.components.storefrontcontext.SearchStorefrontContext;
import com.adobe.cq.commerce.core.search.models.SearchResultsSet;
import com.demoproject.core.models.SearchResultsRetail;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;

import javax.annotation.Nonnull;
import java.util.Collection;

@Model(
        adaptables = {SlingHttpServletRequest.class},
        adapters = SearchResultsRetail.class,
        resourceType = SearchResultsRetailImpl.RESOURCE_TYPE
)
@Exporter(name="jackson", selector = "data", extensions = "json")
public class SearchResultsRetailImpl implements SearchResultsRetail {

    @Self
    (type = ResourceSuperType.class)
    private SearchResults searchResults;

    static final String RESOURCE_TYPE = "demoproject/components/commerce/searchresults";

     public SearchStorefrontContext getSearchStorefrontContext() {
        return searchResults.getSearchStorefrontContext();
    }

     public SearchResultsStorefrontContext getSearchResultsStorefrontContext() {
        return searchResults.getSearchResultsStorefrontContext();
    }

    @Nonnull  public Collection<ProductListItem> getProducts() {
        return searchResults.getProducts();
    }

    @Nonnull  public SearchResultsSet getSearchResultsSet() {
        return searchResults.getSearchResultsSet();
    }

     public boolean loadClientPrice() {
        return searchResults.loadClientPrice();
    }

     public String getPaginationType() {
        return searchResults.getPaginationType();
    }

     public boolean isAddToCartEnabled() {
        return searchResults.isAddToCartEnabled();
    }

     public boolean isAddToWishListEnabled() {
        return searchResults.isAddToWishListEnabled();
    }

     public String getClassName() {
        return SearchResultsRetailImpl.class.getName();
    }
}

 

 

 

Interface Class : 

 

 

 

@ProviderType
public interface CustomSearchResults extends SearchResults {
    String getClassName();
}

 

 

 


Now In above Implementation of sling exporter we are referencing the core sling model implementation only. 
What happens is the core sling model classes and pojos of Search Results Component have custom serializers and exporters which straightway give us response from the graphql , without the structure and logic that we get from using sling models in sightly.
When we export the sling model data from exporter, those response do not come as they would in sightly when we use the model getter methods.
Example : 
In response in the object products -> product -> priceRange , I do not get isDiscounted Boolean key value in sling model exporter which I would normally get in sightly because of serializers used.

Which is the best way to go around this and make maximum use of core component sling models ??

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

2 Replies

Avatar

Community Advisor

Hi, 

 

You should use the delegation pattern, so you don't lose the original functionality of the CIF components and you can extend and add your custom logic. 


You can find more info here: 

https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models 

https://kiransg.com/2021/11/07/aem-core-component-delegation/

https://aemexplained.wordpress.com/aem-guide/extending-component-using-sling-delegation/ 

 

 

 



Esteban Bustamante

Hi @EstebanBustamante 
Thanks for the reply.
But if you check the post above, I am using the delegation pattern only.
The issue is that when I export the model, the response json doesn't have all the attributes and structure that we get when using sling model directly in sightly via data-sly-use.