test condition for model class | Community
Skip to main content
Level 2
Feb 14, 2023 8:54 AM UTC
Solved

test condition for model class

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.
BrianKasingli
BrianKasingliAccepted solution
New Participant
Feb 15, 2023 7:37 AM UTC

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

8 replies

BrianKasingli
New Participant
Feb 15, 2023 7:09 AM UTC

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

Translate
Vinod-N-EAuthor
Level 2
Feb 15, 2023 7:27 AM UTC

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,

Translate
BrianKasingli
BrianKasingliAccepted solution
New Participant
Feb 15, 2023 7:37 AM UTC

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

Translate
Manu_Mathew_
New Participant
Feb 14, 2023 9:22 AM UTC

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

Translate
Vinod-N-EAuthor
Level 2
Feb 14, 2023 10:29 AM UTC

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

Translate
Manu_Mathew_
New Participant
Feb 14, 2023 11:34 AM UTC

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

Translate