Expand my Community achievements bar.

SOLVED

ResourceResolver using Javascript-Use API?

Avatar

Level 3

Hi there,

I am trying to convert my components over to the HTL/Sightly instead of JSP, and I'm running into a road block.
I'd prefer to do the HTL using Javascript-Use API instead of Java-Use because I am more familiar with JS.

My new JS code below errors out:
"Can't find method com.myproject.aem.util.GlobalUtils.userCanReadRedirectTarget(java.lang.Class,org.mozilla.javascript.Undefined).
Any ideas why?


In the old JSP, I have this:

boolean isUnlocked = GlobalUtils.userCanReadRedirectTarget(resourceResolver, linkPath);
if(!isUnlocked) {
    // do something here
}

 

In the new JS file, I have this:

"use strict";

var GlobalUtils = Packages.com.myproject.aem.util.GlobalUtils;
var ResourceResolver = Packages.org.apache.sling.api.resource.ResourceResolver;

use(function() {
    
    var linkPath = properties.get("linkPath");
    var isUnlocked = GlobalUtils.userCanReadRedirectTarget(ResourceResolver, linkPath);
        
    return {
        isUnlocked: isUnlocked
    };
    
});

 

Thanks in advance!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi

 You can directly use resourceResolver.

e.g

var resourceResolver = resource.getResourceResolver();

let me know if it helps.

 

Thanks

Dipti

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi

 You can directly use resourceResolver.

e.g

var resourceResolver = resource.getResourceResolver();

let me know if it helps.

 

Thanks

Dipti

Avatar

Level 3

Genius - that worked perfectly - thank you! Is there any documentation that explains this a little better? This seems to be the only documentation out there: https://docs.adobe.com/docs/en/htl/overview.html

Avatar

Level 5

gjv wrote...

Genius - that worked perfectly - thank you! Is there any documentation that explains this a little better? This seems to be the only documentation out there: https://docs.adobe.com/docs/en/htl/overview.html

 

You SHOULD be able to use the ResourceResolver directly without needing to define it like Dipti did...

// just an example, "resolver" is the name of the globally defined ResourceResolver var userId = resolver.getUserID();

See full documentation on available global objects in HTL[1]. If you're looking for what you can do with the ResourceResolver, check the full Java documentation for that class[2].

So in your JS file, you could do something like:

"use strict"; var GlobalUtils = Packages.com.myproject.aem.util.GlobalUtils; use(function() { // ...whatever you were going to use GlobalUtils for here... return { "userId": resolver.getUserID(); // just an example }; });

[1] https://docs.adobe.com/docs/en/htl/docs/global-objects.html
[2] https://docs.adobe.com/docs/en/aem/6-2/develop/ref/javadoc/org/apache/sling/api/resource/ResourceRes...

Avatar

Level 2

HI,

Will I be able to use the same in scripts written in GuideBridge API.?

and how should I import java APIs in js file.?