【问题标题】:AEM 6.2 get component property from parent pageAEM 6.2 从父页面获取组件属性
【发布时间】:2017-08-04 08:27:45
【问题描述】:

我有一个需要共享的页脚元素。我的计划是在父/主页上设置页脚,但允许子页面覆盖这些属性。

我首先在当前组件上查找属性(非常标准),然后获取到父页面的路径以查找同名组件上的属性。

function getProperty(property, currentPage) {
    var val = null,
        page = currentPage,
        rootPage = page.getAbsoluteParent(2);

    var curNode = currentNode.getPath(),
        nodeStrIdx = curNode.indexOf("jcr:content"),
        nodeStr = curNode.substr(nodeStrIdx + 12);  // Remove 'jcr:content/' too

    while(val == null) {

        // If we've gone higher than the home page, return
        if(page.getDepth() < 3) {
            break;
        }

        // Get the same node on this page
        var resource = page.getContentResource(nodeStr);

        if(resource != null) {
            var node = resource.adaptTo(Node.class);  // *** This is null ***

            // val = node.get(property);
        }

        // Get the parent page
        page = page.getParent();
    }

    return val;
}

我看到您可以将内容资源的类型更改为一个节点,该节点应该允许我获得相同的 propertyresource.adaptTo(Node.class) 返回 null。

如果不清楚,resource 是我要从中提取属性的节点的绝对路径,例如/content/jdf/en/resources/challenge-cards/jcr:content/footer/follow-us

【问题讨论】:

    标签: javascript aem sling htl


    【解决方案1】:

    假设您使用的是Javascript HTL Use API,您需要为 Java 类使用完全限定名,如下所示:

    var node = resource.adaptTo(Packages.javax.jcr.Node);
    

    然后你可以通过这种方式检索你的值:

    if (node.hasProperty(property)) {
        val = node.getProperty(property).getString();
    }
    

    您需要使用Node APIhasProperty 方法,因为当缺少属性时getProperty 会抛出PathNotFoundException。您还需要小心示例中的 granite.resource 对象 - 它不是同一个 Resource 对象并且它没有 adaptTo 方法。要获取组件的原始资源,您需要获取nativeResource 属性:

    var node = granite.resource.nativeResource.adaptTo(Packages.javax.jcr.Node);
    

    但是,在 JS 中也应该有更快的方法从资源中获取属性:

    val = resource.properties[property];
    

    由于这是开发组件属性继承的一种很常见的情况,您还可以在实现设计中考虑一些现成的解决方案,例如HierarchyNodeInheritanceValueMap APIInheritance Paragraph System (iparsys)

    由于这个JS是用Mozilla Rhino在服务端编译的,所以这里使用的所有这些对象和方法都是Java对象和方法,所以你应该也可以通过这种方式使用HierarchyNodeInheritanceValueMap:

    //importClass(Packages.com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap);
    //this import might be needed but not necessarily
    
    var props = new HierarchyNodeInheritanceValueMap(granite.resource.nativeResource);
    val = props.getInherited(property, Packages.java.lang.String);
    

    然后它将返回val当前资源的属性值,或者如果为空,则返回父页面上相同位置的资源的属性值,或者如果为空等。这两行应该都可以那么魔法。

    【讨论】:

    • resource.properties[property];作品!第一个解决方案仍然不起作用。感谢您提供其他链接,我认为我不能使用 HierarchyNodeInheritanceValueMap API,因为我使用的是 Javascript HTL 使用 API。
    • 只是出于好奇,adapto 方法是再次返回 null 还是其他错误。这里可能出现的一个可能错误与获取值的注释行有关,Node API 提供了 getProperty 方法,当找不到属性时它会抛出错误而不是 Sling 空值。关于 HierarchyNodeInheritanceValueMap 服务器端 JS 无论如何都用 Rhino 编译成 Java,所有使用的对象实际上都是 Java 对象。它还应该能够从不同的包中导入对象,其中一些已经导入。我用更多示例更新了我的答案。
    • @KamilCiecierski 这很有帮助!在哪里可以找到 HTL Javascript Use-API 中可用的属性和方法?
    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2023-03-26
    • 2018-06-13
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多