Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

test condition for model class

Avatar

Level 3

Hi,

I have a query, can we write a condition to check the methods in the model class before we call the model class using data-sly-use and call the methods.

 

here's a scenario:

Model Class,

 

@Model(adaptables = Resource.class)
public class HelloWorldModel {

    @ValueMapValue(name=PROPERTY_RESOURCE_TYPE, injectionStrategy=InjectionStrategy.OPTIONAL)
    @default(values="No resourceType")
    protected String resourceType;
    @inject
    private String message;
    public String getHelloMessage() {
        return message;
    }
}

 

HTML file

 

<div data-sly-use.model="com.bootcamp.core.models.HelloWorldModel">
 
data-message=${model.helloMessage} </div>
question: how do I use data-sly-test to check the class and it's objects
 
Thanks,
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

<section data-sly-use.model="com.bootcamp.core.models.HelloWorldModel" data-

message="${model.helloMessage}">

</section>

This is just fine, looks like you are using HTML attributes, which is consumed by the JS frontend code. you can detect whether data-message is null, blank or not, then perform the task as needed.

View solution in original post

8 Replies

Avatar

Community Advisor

@Vinod-N-E it's recommended to write your logic on the sling model and assign a return value to a member variable and use that in HTL.

Avatar

Level 3

Thank you Manu, but could I some how do it in the front end as well.

Avatar

Community Advisor

@Vinod-N-E I believe, data-sly-use initializes your sling model, ie. it basically declares component-beans with this statement for instance, to check conditions before that may not be possible. 

However, I am not sure what your use case is.

Avatar

Community Advisor

Easy,
<sly data-sly-use.model="com.bootcamp.core.models.HelloWorldModel"></sly>
<sly data-sly-test="${model.helloMessage}>${helloMessage}</sly>

Avatar

Level 3

This is helpful Brian, could you suggest me for a different secnario.

 

here's the scenario: take the example of the same model class above,

 

<section data-sly-use.model="com.bootcamp.core.models.HelloWorldModel" data-

message="${model.helloMessage}">

</section>

How do I write a test condition as an attribute for the section tag and execute the helloMessage method.

 

Thanks,

Avatar

Correct answer by
Community Advisor

<section data-sly-use.model="com.bootcamp.core.models.HelloWorldModel" data-

message="${model.helloMessage}">

</section>

This is just fine, looks like you are using HTML attributes, which is consumed by the JS frontend code. you can detect whether data-message is null, blank or not, then perform the task as needed.