test condition for model class | Community
Skip to main content
Level 2
February 14, 2023
Solved

test condition for model class

  • February 14, 2023
  • 2 replies
  • 1504 views

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)
    @1497330(values="No resourceType")
    protected String resourceType;
    @586265
    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,
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 BrianKasingli

<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.

2 replies

Manu_Mathew_
Community Advisor
Community Advisor
February 14, 2023

@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.

Vinod-N-EAuthor
Level 2
February 14, 2023

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

Manu_Mathew_
Community Advisor
Community Advisor
February 14, 2023

@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.

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 15, 2023

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

Vinod-N-EAuthor
Level 2
February 15, 2023

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,

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
February 15, 2023

<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.