AEM:从 dialog-clientlib 访问全局值

问题描述

目前,我将某些值存储在 customheaderlibs.html 中的数据属性

const iframeContents = document.querySelector('iframe').contentwindow.document.body;
        const configElement = iframeContents.querySelector('div[data-score]');

并且正在读取这些值

 

with first as (
SELECT Trim(mc.pid) as pid1
FROM   m_cast mc 
WHERE  Trim(mc.mid) IN (SELECT Trim(m.mid) 
                        FROM   movie m 
                        WHERE  Cast(Substr(Trim(m.year),-4) AS INTEGER) < 1970)
),second as (
SELECT Trim(mc.pid) as pid2
FROM   m_cast mc 
WHERE  Trim(mc.mid) IN (SELECT Trim(m.mid) 
                        FROM   movie m 
                        WHERE  Cast(Substr(Trim(m.year),-4) AS INTEGER) > 1990) 
)
select first.pid1 from first intersect select second.pid2 from second

这适用于特定分辨率的屏幕。但是在较小的视口中,例如在 iPad 中,cq 对话框作为全屏而不是对话框打开,DOM 中似乎没有 customheaderlibs.html。因此,无法在对话就绪时从 javascript 访问这些数据。有什么方法可以访问这些数据,可能是作为查询参数传递给花岗岩对话框路径吗?但不确定如何实现这一点。

解决方法

如果 'score' 和 'team' 是静态字段,您可以通过向 /component/path..json 发送 GET 请求来访问此信息

在其他情况下,您可以创建自定义组件并将其添加到组件的对话框中。

<general
                        jcr:primaryType="nt:unstructured"
                        jcr:title="General"
                        sling:resourceType="granite/ui/components/coral/foundation/container"
                        margin="{Boolean}true">
                        <items jcr:primaryType="nt:unstructured">
                            <columns
                                jcr:primaryType="nt:unstructured"
                                sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns"
                                margin="{Boolean}true">
                                <items
                                    jcr:primaryType="nt:unstructured">
                                    <column
                                        jcr:primaryType="nt:unstructured"
                                        sling:resourceType="granite/ui/components/coral/foundation/container">
                                        <items
                                            jcr:primaryType="nt:unstructured">
                                            <customCMP
                                                jcr:primaryType="nt:unstructured"
                                                sling:resourceType="custom/cmp"/>
                                        </items>
                                    </column>
                                </items>
                            </columns>
                        </items>
                    </general>

/apps/custom/cmp/cmp.html

<div data-sly-use.customCMP="CustomCMP"
     data-score="${myjava.getScore}"
     data-team="${myjava.getTeam}">
</div>

CustomCMP.java

@Model(adaptables = SlingHttpServletRequest.class,defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class CustomCMP {

    @ScriptVariable
    private ResourceResolver resolver;

    private MyJava myJava;

    @PostConstruct
    public void init() {
        final String componentPath = (String) request.getAttribute("granite.ui.form.contentpath");
        Resource componentResource = resolver.getResource(componentPath);

        if (componentResource != null) {
            myJava = componentResource.adaptTo(MyJava.class);
        }

    }

    public String getTeam() {
        return myJava.getTeam();
    }

    public String getScore() {
        return myJava.getScore;
    }
}