Expand my Community achievements bar.

SOLVED

Access design_dialog(currentStyle) properties from within a Sling Model

Avatar

Level 3

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

View solution in original post

7 Replies

Avatar

Level 3

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!

Avatar

Community Advisor

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;  

Avatar

Level 3

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

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

Avatar

Community Advisor

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

Avatar

Correct answer by
Level 10

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

Avatar

Level 3

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