How to pass parameters to Sling Modal from Sightly Component | Community
Skip to main content
Level 3
Jun 8, 2017 4:42 PM UTC
Solved

How to pass parameters to Sling Modal from Sightly Component

Hi ,

I am trying to pass some custom parameters from my Sightly Component to Sling Model. But I am not able to read those passed parameters inside my Sling Model.

Below is my sightly code :

<sly data-sly-use.mySlingModal="${'com.adobe.website.models.ImageModel' @ colour='red', path=resource.path}"></sly>

How to read color and path in side my sling model java class.

Can you please help me in solving this.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
EakambaramAccepted solution
Level 3
Jun 21, 2017 9:16 PM UTC

Thank you all for responding. We  have solved it as below.

@Model({adaptables=Resource.class, adaptables=SlingHttpServletRequest.class)

So that at the same time I will get my resource/Node/Content properties and the values passed to my sling modal from my sightly component.

16 replies

rampai
Level 1
Jul 25, 2019 12:01 PM UTC

Hi,

Correction in this for the curly braces: @Model(adaptables = {Resource.class, SlingHttpServletRequest.class})

Translate
smacdonald2008
Level 10
Mar 9, 2018 1:59 PM UTC

Here is HTL and Sling models -- Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Models

Using this approach - as you see - very little Java code. 

Translate
susheel
Level 5
Mar 8, 2018 2:14 PM UTC

We can do with sling models as below:

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.models.annotations.Optional;

import org.apache.sling.models.annotations.injectorspecific.RequestAttribute;

@Model(adaptables = SlingHttpServletRequest.class)

public class TestModel {

@RequestAttribute

@Optional

private String test;

@PostConstruct

    public void init() {

        //we can read the values directly here.

    }

}

<sly data-sly-use.mySlingModal="${'TestModel' @ test='red'}"></sly>

Translate
smacdonald2008
Level 10
Nov 7, 2017 3:11 PM UTC

WCMUsePojo is a valid way if you prefer to use over Sling Models,

Translate
Level 5
Nov 7, 2017 3:08 PM UTC

Thanks.

Adobe recommends to use sling model instead of WCMUsePoJo. I was curious how to adaptable two classes in above examples.

Translate
Level 3
Nov 5, 2017 5:30 PM UTC

@Model({adaptables=Resource.class, adaptables=SlingHttpServletRequest.class}).

I was trying to pass some parameters/values from my sighlty html to my sling model, once we receive those parameters in our java we have some custom process requirement for those parameters and have to return some value found in repository to sighlty.

You can try the adaptables way. Another way is we can change our approach from sling model to wcmuse pojo way for this component alone.

Sighlty code snippet for passing some parameters (Those parameters are not authored by the author) from sightly component to Java/WCMUsePojo class

<sly data-sly-use.customPropReader="${'com.yourcompany.division.project.CustomPropReader' @ properties='prop1,prop2'}"></sly>

WCM use pojo Java code to read those parameters.

public class CustomPropReader extends WCMUsePojo {

/**

* custom biz logic applied value map

*/

private HashMap customProcessedProps;

@Override

public void activate() {

String property = get("properties", String.class) != null ? get("properties", String.class) : "";

String[] propertyList = property.split(",");

//We will get all values in propertyList string array. We can loop through those each value and apply custom logic

//One we got the results we can send them back to Sightly

customProcessedProps = new HashMap<String, String> ();

for (String property : propertyList) {

//apply custom logic to each property string and put that in map.

customProcessedProps.put(property, applyCustomBizLogic(property));

}

}

/**

* get ValueMap of inheritedPageProperties

*

* @return HashMap

*/

public HashMap getCustomProcessedProp() {

return customProcessedProps;

}

}

Translate
Feike_Visser1
Employee
Nov 5, 2017 1:26 PM UTC

You adapt always from *one* class. In case you adapt from request, you have the resource too via the getResource()

Translate
Level 5
Nov 5, 2017 6:33 AM UTC

Thank you.

However, these examples are too simple. Do you have some more examples, which adaptable resource, and inject the request? or can adaptable request and resource both?

Translate
smacdonald2008
Level 10
Nov 5, 2017 6:23 AM UTC

Newer article that uses Sling Models and @ inject Creating a custom Touch UI Grid Component for Adobe Experience Manager

Translate
Level 5
Nov 5, 2017 6:06 AM UTC

Hi Eakambaram:

I have the same issue, not sure if you can help me.

for @Model({adaptables=Resource.class, adaptables=SlingHttpServletRequest.class), where to close '}'. Can you share a little bit code snippet.

Appreicated.

Thanks

Translate