Search code examples
dialogadobelisteneraem

Is it possible to get Page Properties inside CQ dialog listener


I have a page at /content/admin/mycomp-test/testpage/jcr:content

property

name : validated
type : boolean
value : true

Is it possible to get the above property value inside CQ dialog's I tried the following code, but it;s not working.

<listeners jcr:primaryType="nt:unstructured"
               beforesubmit="function(dialog) {

                var compPath = dialog.path;

                Resource res= resourceResolver.getResource(compPath);

                Node node = res.adaptTo(Node.class);

                var prop= node.getProperty('validated').getValue().getString(); 



                CQ.Ext.Msg.alert('valieded : ' + prop);
                    return false;

        }" />
}

Solution

  • The following code will get you what you're asking for:

    <listeners jcr:primaryType="nt:unstructured"
        beforesubmit="function(dialog) {
            var properties = CQ.shared.HTTP.eval(CQ.shared.HTTP.noCaching(dialog.path + '.-1.json'));
            var validated = properties.validated;
    
            CQ.Ext.Msg.alert('Validated: ' + validated);
    
        }" />
    }
    

    In my opinion, it's better to keep this type of JavaScript in an external file which would be included in a clientlib that only displays in edit mode. You can then call that method from within the dialog. For example:

    beforesubmit="function(dialog){ foo.bar.baz(dialog); }"
    

    If you're looking to validate user input, there are better ways. Explore the Widgets API documentation.