Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get runmodes value for client-side JS using hidden field

Avatar

Level 7

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/get-runmodes-value-in-clie...

 

Hi folks,

There is a discussion here on using a hidden field to make the "runmode" available to client-side JS widgets. I want to do the same as the OP.

Jorg Hoh said

"You should not expose the concept of runmodes to your frontend. Instead you should define a marker which is queried by your frontend; and this marker might be rendered directly into the page or queried via JSON. And the presence of the marker in the markup (or JSON) is controlled by an OSGI configuration which can be set via runmode-based OSGI configuration.

Otherwise you need to have knowledge in your frontend about backend specifics."

 

Just looking for clarification on the 1st point made, does Jorg mean to derive a value from the runmode and put that on the page ?

 

thanks

Fiona

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @fionas76543059,

 

For fetching the run modes, you can use SlingSettingsService.

 

Code:

@Reference
private SlingSettingsService slingSettingsService;

private Boolean isPublish;

private boolean isPublish() {
return this.slingSettingsService.getRunModes().contains("publish");
}

 

You can now expose this boolean in HTL like this:

${classObject.isPublish}

This resultant object value can be consumed by the Front end as mentioned here

 

As per my understanding what Jorg Hoh is trying to say is to separate the business logic and presentation logic.

 

Thanks,

Kiran Vedantam.

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @fionas76543059,

 

For fetching the run modes, you can use SlingSettingsService.

 

Code:

@Reference
private SlingSettingsService slingSettingsService;

private Boolean isPublish;

private boolean isPublish() {
return this.slingSettingsService.getRunModes().contains("publish");
}

 

You can now expose this boolean in HTL like this:

${classObject.isPublish}

This resultant object value can be consumed by the Front end as mentioned here

 

As per my understanding what Jorg Hoh is trying to say is to separate the business logic and presentation logic.

 

Thanks,

Kiran Vedantam.

Avatar

Employee Advisor

Yes it is indeed best practice to abstract this run mode concept and not expose it in your front end logic. And for abstraction you can set a data attribute in your HTML and the value of which can be set based on an OSGI Configuration. The OSGI configs are read based on the runmodes automatically by the system. 

So you just have to keep different values of the data attribute in OSGI configuration for each run mode. The system will return you the value based on the current AEM running mode and thereby you don't have to worry about run modes in your front end code. 

Avatar

Level 7
Thanks Kunal for your input. I would also Accept your Solution, but I don't seem to be able to accept more than one.!