Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

Delegation Pattern for Sling Models doesn\'t populate all the properties from super class

Avatar

Level 1

I am trying to understand Delegation patterns better when we extend the core components Sling model.

I am following the example provided here https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models  to extend the Teaser component. Basically, I want all the properties defined in the Teaser component and add a few more. 

Whatever new properties I am adding to the custom sling model, I can access those, and they return the correct values. But the properties defined in the core component Teaser model return no value. Upon debugging, I noticed that values are being populated correctly in the private teaser property, so I thought of returning the whole Teaser object through the getter as shown in the below code snippet. (again, not sure if this is a good solution or not).

 

@Deleted Account
@Via(type = ResourceSuperType.class)
private Teaser teaser;

public Teaser getTeaser() {
    return teaser;
}

 

Now, I can access the core component's properties and read the values, but now my HTL looks ugly because to access the core component's properties, I have to write a teaser before those properties and not directly. 

 

<div data-sly-use.customTeaser="com.company.subdomain.core.models.CustomTeaser">
    <h2>${customTeaser.teaser.title}</h2>
</div>

 

I am sure I am doing something wrong here; that's why I wanted to reach out to the community to get some better ideas. Is there any other approach to extend a core component easily? Is something that needs to be done differently when implementing a delegation pattern to access core components properties without overriding each one?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@webdev24,

your code looks correct, can you show with us your implementation of the sling model using the delegation pattern?

Also make sure that your top level annotations are correct,

@Model(adaptables = SlingHttpServletRequest.class, adapters = Teaser.class, resourceType = "myproject/components/myTeaser")

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

@webdev24,

your code looks correct, can you show with us your implementation of the sling model using the delegation pattern?

Also make sure that your top level annotations are correct,

@Model(adaptables = SlingHttpServletRequest.class, adapters = Teaser.class, resourceType = "myproject/components/myTeaser")

Avatar

Community Advisor

Faced a similar issue. Leaving a reply so that it helps others who come across the same problem.

 

The reason the core component properties are null is that currently, the above delegate sling model doesn't have access to core component properties. You will have to override all the helper methods, not just the ones you are modifying. For example, say you are not changing any logic related to 'title', still you will have to override getTitle().

 

@Override
public String getTitle() {
return Teaser.getTitle()
}

Or you can use Lombok to get rid of the boilerplate code.

 

https://blogs.perficient.com/2021/06/14/writing-less-java-code-in-aem-with-sling-models-lombok/