Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Can I load an external js file in the listener node of a dialog

Avatar

Level 4

In some scenarios the dialogs of complex components have multiple listener nodes listening on various events. At times, the JS code in the listeners gets pretty complex and since this code is embedded in the listener node, it is very difficult to debug and troubleshoot issues.

Is there any way of writing the JS code for the listener in an external JS file and then including this file in the event changed property of listener node?

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi 

Please have a look at this post,

Link:- http://stackoverflow.com/questions/26192217/how-to-make-textfield-required-using-listeners

Dialog:

<type jcr:primaryType="cq:Widget" fieldLabel="Type" name="./type" type="radio" xtype="selection"> <options jcr:primaryType="cq:WidgetCollection"> <email jcr:primaryType="nt:unstructured" text="Email" value="email"/> <url jcr:primaryType="nt:unstructured" text="Url" value="url"/> </options> <listeners jcr:primaryType="nt:unstructured" selectionchanged="function(field,value) { MyDialog.setRequired(field,value); }"/> </type>

Listener JavaScript:

MyDialog.setRequired = function(field,value) { var dialog = field.findParentByType("dialog"), email = dialog.getField('./email1'); if('url' == value) { email.allowBlank = true; }else{ email.allowBlank = false; } };
 

I hope this will help you.

Thanks and regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

3 Replies

Avatar

Level 9

Yes, All you need to do is create a clientlib with these javascript functions and include them in the header (Just make sure, functions are available before lister invoke them).

Let me know if you face any issue.

---Jitendra

Avatar

Community Advisor

Hi 

As Jitendra said create a clietlib folder and listener as below

<listeners
        jcr:primaryType="nt:unstructured"
        beforesubmit="function(dialog){return javascriptObject.beforeSubmit(dialog);}"
/>

External JS:

var  javascriptObjectjavascriptObject|| {};

javascriptObject.beforeSubmit = function (dialog) {
    Write here your functionality
  
};

 

Avatar

Correct answer by
Administrator

Hi 

Please have a look at this post,

Link:- http://stackoverflow.com/questions/26192217/how-to-make-textfield-required-using-listeners

Dialog:

<type jcr:primaryType="cq:Widget" fieldLabel="Type" name="./type" type="radio" xtype="selection"> <options jcr:primaryType="cq:WidgetCollection"> <email jcr:primaryType="nt:unstructured" text="Email" value="email"/> <url jcr:primaryType="nt:unstructured" text="Url" value="url"/> </options> <listeners jcr:primaryType="nt:unstructured" selectionchanged="function(field,value) { MyDialog.setRequired(field,value); }"/> </type>

Listener JavaScript:

MyDialog.setRequired = function(field,value) { var dialog = field.findParentByType("dialog"), email = dialog.getField('./email1'); if('url' == value) { email.allowBlank = true; }else{ email.allowBlank = false; } };
 

I hope this will help you.

Thanks and regards

Kautuk Sahni



Kautuk Sahni