13 Replies Latest reply on Jun 17, 2013 1:22 PM by Mei G. Lingwood

    Is there way to dyanmically set component proeprties?

    kvelhal

      In one of my component I am making some external service calls and I would like to populate the component's properties with the data received from the service call. How can I do it?

       

      I tried using currentNode.setProperty("mypropname", "myservicedata") but it did not work.

       

      Also when including the text components in the script is there a way to populate its text property of text component with some default value?

       

      mycomponent.jsp

      -----------------------

      . . .

      <cq:include path="text1" resourceType="foundation/components/text"/> <%-- How to pre-populate this component with value say "My Text 1" --%>

      <cq:include path="text2" resourceType="foundation/components/text"/><%-- How to pre-populate this component with value say "Additional Text 2" --%>

      . . .

       

      Has anyone done something like this?

        • 1. Re: Is there way to dyanmically set component proeprties?
          Yogesh Upadhyay Level 4

          I don't think there is any way to initialize it in tag it self. That logic should go in component it self. But it is nice to have feature, Provided that you know exact property name used in that component.

           

          Yogesh

          www.wemblog.com

          • 2. Re: Is there way to dyanmically set component proeprties?
            kvelhal Level 1

            I know that <cq:include> tag does not allow such initialization. However I am looking for any alternate programatic way to achieve this.

             

            I think we should be able to invoke the same APIs that are used when we edit a specific component property interactively from the UI.

             

            E.g. Authors edit the Text component and specify the value they want to display in that Text component. Whatever API calls are made behind the scenes to achieve this should be available to us in the component script. I just don't have any insight into  CQ Java API programming.

            • 3. Re: Is there way to dyanmically set component proeprties?
              rush_pawan Level 4

              Hello,

               

              Populating component properties using external data is possible for some of the xtypes directly through configuration. What you need to do is just create CQ servlet (you can digest your external service here) and return JSON response in expected format. Please refer one my earlier post for reference - http://forums.adobe.com/message/4830914#4830914 , http://forums.adobe.com/message/4785838#4785838

               

              Other types you can also do but require customization.

               

              Also after statement currentNode.setProperty("mypropname", "myservicedata") ; do currentNode.save(); so that it will save data to current node.

               

              Now to answer your next question, there are two things and depends on what you want.

               

              1. If you want to display some default value to some field in component lets take above example for foundation/components/text component then set "defaultValue" property at node "/libs/foundation/components/text/dialog/items/tab1/items/text" with what ever value you want to display.

               

              (refer http://dev.day.com/docs/en/cq/current/widgets-api/index.html for more information related to any of xtypes supported in CQ)

               

              2. If you only want to display default value only at time of component rendering on page then as yogesh mentioned above you have to handle in scriptlet code of that component (as there is not OOTB support through default cq:include taglib or either you customize it locally)

              Normally for this use <cq:text property="propName1"  placeholder="Lorem ipsum dolor sit amet "/> where place holder will be default value at the time of display.

               

              I hope this helps you to proceed.

               

              Thanks,

              Pawan

              • 4. Re: Is there way to dyanmically set component proeprties?
                Abhinav_Sap Level 1

                Try calling this after the property setter call -

                session.save();

                • 5. Re: Is there way to dyanmically set component proeprties?
                  kvelhal Level 1

                  Thanks for the quick responses.

                   

                  I will try using the currentNode.save() after setting the properties. I couldn't find any handy documentation (or code snippet) around manipulation of JCR nodes in CQ repository. It would be nice if Adobe provided any code sample demonstrating the node updates (ACL implications, transactions, etc)

                   

                   

                  I am aware of the defaultValue property but it needs to be set by the developer in CRXDE and it will be fixed for all instances of that component. What I need is the ability to initialize the component property dyanmically per instance of that component. I will try with the placeholder option you mentioned. I had seen that in documentation but did not try out.

                   

                  I am new to CQ and don't know when to write custom servelets and how to plug them in. Is there any document explaining the  development process using CQ Java APIs? E.g. How do I know the organization and life cycle of CQ objects? Also I see a component interface which does not have any setter methods. It is not obvious that we need to refer the component as node and then call set property on it.

                  • 6. Re: Is there way to dyanmically set component proeprties?
                    rush_pawan Level 4

                    Hi,

                     

                    I hope you have considered above suggestion. After looking at you next reply i can suggest using design_dialog in component design. design_dialog helps you to configure any property that you want to set once at template level (using any of the page) and will be shared accross all the pages using same template. Now it will help you to meet your second requirement about having different value but that will be based on template level so when you change template you can configure same property with new value.

                     

                    take reference of existing component - /libs/foundation/components/logo (use currentStyle implcit object) for more information refer for design dialog

                    http://dev.day.com/docs/en/cq/current/developing/components.html

                     

                     

                    Let me know if you need more information.

                     

                    Thanks,

                    Pawan

                    • 7. Re: Is there way to dyanmically set component proeprties?
                      kvelhal Level 1

                      Using design dialog won't help me as I am using the same template for my pages. The template will have a component that  dynamically pulls some data from a backend system. I want authors to  provide an ID for which data to be pulled from backend. The component will then fetch the corresponding data from backend service and populate it in my custom text components (inherited from CQ text component).

                       

                      Depending on the ID provided the service may return different number of data items and for each of them my component will use add text components. Authors can later edit these text components to add more data or formatting as needed

                      • 8. Re: Is there way to dyanmically set component proeprties?
                        justin_at_adobe Adobe Employee

                        You should *not* do this per request. Instead, have a scheduled job which accesses the external service on a periodic basis and populates the components. This job will run as a privledged user who can set properties. Assuming that anyone loading a page will have the rights to edit that page's properties (or the properties of components in that page) is not a safe assumption.

                         

                        Justin

                        • 9. Re: Is there way to dyanmically set component proeprties?
                          kvelhal Level 1

                          My requirement is to have the data loaded dyanmically per request as it may be frequently changing.

                          • 10. Re: Is there way to dyanmically set component proeprties?
                            kvelhal Level 1

                            I am actually interetsed in knowing if there is any way in Adobe CQ to dynamically initialize component properties at the time of inclusion.

                             

                            E.g. <cq:include path="mypath" resourceType="my-component-path"/> I need to set my-component.my-property to some value while including it on the page. It may or may not be user editable component. I am OK to write Java/JSP code that may deal with Node objects instead of using <cq:include> if that allows me to do what I need.

                            • 11. Re: Is there way to dyanmically set component proeprties?
                              justin_at_adobe Adobe Employee

                              If you load it dynamically per request, then you don't need to store that data in the repository. It would be better to load it on a periodic basis.

                              • 12. Re: Is there way to dyanmically set component proeprties?
                                justin_at_adobe Adobe Employee

                                If you do <cq:include path="mypath" resourceType="my-component-path"/> and mypath doesn't exist, a synthetic resource is created with the provided resource type. There's not currently a way to provide additional properties. You could write your own tag to do that. The source for the sling:include tag is available. I don't think the JCR API wouldn't be used as you aren't talking about persisting anything.

                                 

                                It's generally a bad idea to have components impacted by their container. This reduces flexibilty and is usually a sign that the (child) component isn't really independent of the parent. Where it is necessary (which again, is rare), the parent passes information to the child via request attributes.

                                • 13. Re: Is there way to dyanmically set component proeprties?
                                  Mei G. Lingwood

                                  Hi Kvelhal,

                                   

                                  I am looking for ways to dynamically initialize component properties at the time of inclusion.  Have you found a solution yet?

                                   

                                  Thanks,

                                   

                                  --Mei