Using path resolvers

Blank 8/10/2019 03:12 - 8/10/2019 03:12
Developers

When using custom controller mappings either in a custom app or in a website you specify a path that may contain a regular expression for example:

.path("/myPosts/(?.*)")

Path resolvers allows you to specify a javascript method to check if there is a record for postId and if so return the record object.

Specifying a resolver

To specify a resolver for a website use:

.addPathResolver("postId", "checkPostID")

To specify a resolver for a portlet use:

.addSectionResolver("postId", "checkPostID")

Then create your JavaScript method called "checkPostId"

function checkPostId(rf, groupName, groupVal, mapOfGroups){
    log.info("checkPostId rootFolder{} groupName={} groupVal={}", rf, groupName, groupVal);
    
    var jsonDB = rf.find("/jsondb/my-posts-db");
    var postRecord = jsonDB.child(groupVal);
    if(postRecord === null){
        return null;
    }
    
    return postRecord;
}

This method will try and find a record for that postId in a JSon Database, if it's not found it returns null, otherwise it return the record object.

If the method returns null then the path controller will be skipped (Not Found), if it returns a value or object it replaces the groupVal with the returned object.

The variable "rf" refers to RootFolder, if the path is for a websiteController then it is a WebsiteRootFolder, if it's an adminController it's an OrganisationRootFolder