Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Passing Attributes to Sling Model via React?

Avatar

Level 3

We have a React component that returns data from the DAM based on the category Tag assigned to it. Is there a way to pass an attribute from React to the sling model and update the model.json with category data to display in React? Or will we have to keep using servlets to achieve this? 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

SPA
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @gregy68980908, did you consider to pass the value using GET parameter. Sling model has access to request, so you should be able to retrieve GET param value and update data exposed by model respectively, e.g assuming this is url you are using now

/content/we-retail/language-masters/en.model.json

it can be changed to something like that (this is request you will sent from React)

/content/we-retail/language-masters/en.model.json?attribute=value

In your sling model you can access value like this:

@Self
private SlingHttpServletRequest request;

private String attributeValue;

@PostConstruct
protected void init() {
    this.attributeValue = request.getParameter("attribute");
} 

 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @gregy68980908, did you consider to pass the value using GET parameter. Sling model has access to request, so you should be able to retrieve GET param value and update data exposed by model respectively, e.g assuming this is url you are using now

/content/we-retail/language-masters/en.model.json

it can be changed to something like that (this is request you will sent from React)

/content/we-retail/language-masters/en.model.json?attribute=value

In your sling model you can access value like this:

@Self
private SlingHttpServletRequest request;

private String attributeValue;

@PostConstruct
protected void init() {
    this.attributeValue = request.getParameter("attribute");
}