Access design_dialog(currentStyle) properties from within a Sling Model | Community
Skip to main content
Level 2
January 4, 2018
Solved

Access design_dialog(currentStyle) properties from within a Sling Model

  • January 4, 2018
  • 7 replies
  • 5431 views

Hi,

I am trying to access currentStyle properties from within a SlingModel. The SlingModel is being called from a  HTL component using data-sly-use that has a functional design_dialog and accessible currentStyle.

This was the only suggestion I could find using google:

@Inject @Source("script-bindings")

  private Style currentStyle;

and then

@PostConstruct

public void init() {

     String startLevel = currentStyle.get("startLevel",String.class); 

}

This isn't working. Can anyone tell me what I am doing wrong??

Thank you!

Ian

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 edubey

Irrespective of adapting to use Resource or Slinghttpservletrequest.

With latest sling jar, You can use below code to get access

@ScriptVariable

private Style currentStyle;

You can refer : https://github.com/Adobe-Marketing-Cloud/aem-core-wcm-components/blob/d4a9c2c7785f5d50e91dd2ebe145ace4a0bb7491/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/ListImpl.java

7 replies

iannovicAuthor
Level 2
January 5, 2018

Update:

I decided to extend WCMUse instead, this class has access to getCurrentStyle(). I am still interested if anyone has a way to make this work with a sling model though!

VeenaVikraman
Community Advisor
Community Advisor
January 5, 2018

I would suggest you to use the above one . It works for me

Are you using @Model(adaptables = SlingHttpServletRequest.class)

@Model(adaptables = SlingHttpServletRequest.class) public class SampleModel{       private final Logger LOG = LoggerFactory.getLogger(getClass());       @Inject      SlingHttpServletRequest request;       @Inject      private Page currentPage;       @Inject      @Source("script-bindings")      private Style currentStyle;  
iannovicAuthor
Level 2
January 5, 2018

No I wasn't using @Model(adaptables = SlingHttpServletRequest.class), I had

@Model(adaptables = Resource.class) instead. that was probably my issue then?

VeenaVikraman
Community Advisor
Community Advisor
January 5, 2018

Can you try with SlingHttpServletRequest.class and let me know if this works.

edubey
edubeyAccepted solution
Level 10
January 6, 2018

Irrespective of adapting to use Resource or Slinghttpservletrequest.

With latest sling jar, You can use below code to get access

@ScriptVariable

private Style currentStyle;

You can refer : https://github.com/Adobe-Marketing-Cloud/aem-core-wcm-components/blob/d4a9c2c7785f5d50e91dd2ebe145ace4a0bb7491/bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/models/v1/ListImpl.java

smacdonald2008
Level 10
January 6, 2018

Excellent answers Praveen and Veena!

iannovicAuthor
Level 2
January 17, 2018

Hi Veena,

Sorry for the late reply.

I managed to try this with a new model that i created and it worked beautifully. Thank you for finding my problem!!!

Regards,

Ian