7 Replies Latest reply on Nov 21, 2012 8:00 AM by rush_pawan

    How to connect to CRX from Javascript?

    Veena_07 Level 3

      Hi

       

            I want to connect to the crx from my script function and retrieve a node in my script so dat i cant retrieve the properties of that node using script... Is dat possible? If yes how is it acheivable ?

       

       

      Thanks in Advance

       

      Veena

        • 1. Re: How to connect to CRX from Javascript?
          Sham HC Level 7

          With Client side javascript it is not possible. You can use server side java script (Ecma). Samples below

           

          * Create a file /apps/geometrixx/components/page/test.ecma with content as [1].

          * Which ever component or a page you want to use call as [2] with/without absolute url.

           

           

          [1]

          var resourceResolverFactory = sling.getService(Packages.org.apache.sling.api.resource.ResourceResolverFactory);

          var authInfo = new java.util.HashMap()

          authInfo.put("user.name","admin")

          authInfo.put("user.password",new java.lang.String("admin").toCharArray())

          var resourceResolver = resourceResolverFactory.getResourceResolver(authInfo)

          out.println(resourceResolver.getResource("/content/geometrixx/en") );

           

          [2] <cq:include script="/apps/geometrixx/components/page/test.ecma"/>

          • 2. Re: How to connect to CRX from Javascript?
            splram Level 1

            You can do this with a custom renderer in a JSP as well, place a JSP in the component with a new custom render e.g. json.jsp and invoke the  content (created with the component) URL  by sufixing .json in the URL. From your script call the .json URL (which returns the output as per what you write in the custom json renderer) and process it. For example there many json renderes available out of the box see one of the json renderer (libs/cq/workflow/components/inbox/steps/json.jsp) which returns thea json string. Hope this helps!

            • 3. Re: How to connect to CRX from Javascript?
              Veena_07 Level 3

              Hi

               

                   Is it possible to create widgets in a dialog(existing dialog) from the data collected from nodes.??? if yes then can you please suggest me an example......

               

               

               

              Thanks

              • 4. Re: How to connect to CRX from Javascript?
                rush_pawan Level 4

                Hello Veena,

                 

                If you want to dynamically modify nodes data to dialog properties (widget/xtype) and if it is only for specific scenario the i would prefer to write listener (loadcontent or beforeloalcontent ) on dialog (tabpanen/panel).

                 

                Please refer http://dev.day.com/docs/en/cq/current/widgets-api/index.html (to know about available events for each xtypes)

                You can refer below as example function to travel through nodes and getting/setting property of your dialog

                 

                function(filed, record, path) {

                 

                  var nodeField="filed name from any node you want to capture";

                var targetField="field you want to set on dialog (panel/tabpanel)";

                  var pathParts=path.split('/');

                  pathParts.pop();  // use this line to travel parent node

                  var jcrContentPath=pathParts.join('/');

                  var response = CQ.utils.HTTP.get(jcrContentPath +'.json');

                  eval('var data ='+response.responseText);

                  record.set(targetField, data[nodeField]);  //this line will help to get and set value of property to your dialog or wherever you want to set.

                  return true;

                }

                 

                I hope it will help you. Let me know if you need more information.

                • 5. Re: How to connect to CRX from Javascript?
                  Veena_07 Level 3

                  Hi pawan

                       

                       The solution is not clear for me.

                   

                  My requirement: I have nodes in my crx whose name i want to retrieve at runtime and create checkbox widgets in the existing dialog. So wen the dialog opens it shud open with the node names as checkboxes. I cant use options provider since each of my checkbox is seperate and have seperate name. So the only way is to check the crx at runtime and create widgets dynamically. Hope my requirement is now clear

                   

                  So is it possible to do this???

                   

                   

                   

                  Thanks

                  Veena

                  • 6. Re: How to connect to CRX from Javascript?
                    Veena_07 Level 3

                    Hi Sham

                     

                          Can you please explain a bit more. I have never used Ecma.  So just wanted to know how to write it in CQ and where to write exactly

                     

                     

                     

                    Thanks

                     

                    Veena

                    • 7. Re: How to connect to CRX from Javascript?
                      rush_pawan Level 4

                      Hello Veena,

                       

                      I think the same i mentioned (write listener 'loadcontent'). You have to do some debug and adjust the code as per requirement but i hope it will suits.

                       

                      Line pathParts.pop();  // use this line to travel parent node will help you to travel through node in parent but for sibling nodes you have to do some debuggin better refer

                      http://dev.day.com/docs/en/cq/current/widgets-api/index.html to check out all other public methods availabe for that resource.

                       

                      once you find the exact node using above now you can use

                      var response = CQ.utils.HTTP.get(jcrContentPath +'.json');

                        eval('var data ='+response.responseText);

                       

                      to get the node out of that path and then you can use

                       

                      data[nodeField]

                       

                      to get out the value of required field associated to that node and then you can set it to your actual resource (means dialog panel/tabpanel) using below

                        record.set(targetField, data[nodeField]);

                       

                      i hope this clarifies you bit more. But as i mentioned you have to play with this code to make it work according to your requirement.

                       

                      Thanks,

                      Pawan