3

I'm using Adobe CQ5. I have made two components that are independent of each other. However, I want to use data from one component in the other one. How do I achieve this? How can I make two components interact with each other?

Thanks in advance.

Archit Arora
  • 2,508
  • 7
  • 43
  • 69
  • 1
    could you be more specific? Do you mean accessing the jcr node of another component ?making them interact through JavaScript ? – santiagozky Jan 24 '14 at 08:01

3 Answers3

3

There are a number of options, depending on how closely the components are coupled (e.g. will both always be placed on the page at the same time? Are they hard-wired into the template, or placed by the editors, etc.)

If both components are on the same page, the simplest is probably to set a variable within one component & read it in another, e.g.:

foo.jsp

<c:set var="globalFooTitle" value="${properties.title}" scope="request"/>

bar.jsp

<c:out value="${globalFooTitle}">[Optional fallback value]</c:out>
anotherdave
  • 6,656
  • 4
  • 34
  • 65
  • it will not always work - it depends on which component is processed first – zacheusz Aug 19 '14 at 21:37
  • 1
    @zacheusz Yes, you can't read a property that hasn't been set yet — it's still parsing the scripts sequentially. – anotherdave Aug 20 '14 at 07:53
  • 1
    This is the best approach I've ever met yet to pass parameters from one component to another at run-time. However, I'd rather add `` right after usage to avoid side effects on other components that could share the same approach. – Micik Oct 10 '14 at 06:49
  • 1
    @Micik, that's true — hopefully this approach is more of a rarity, but if you commonly need to do something along these, you'd probably also need to think through a structure for name-spacing things – anotherdave Oct 10 '14 at 08:13
2

While there is nothing wrong with @kmb's answer, I almost always prefer to use higher level Apis than lower level ones. In the case of CQ, this means using the sling resource API instead of the JCR node API.

That would look something like this, assuming the same structure of the 2 components on a single page as he laid out.

Resource r = resourceResolver.getResource(currentResource, "../component2");
if(r != null) {
    ValueMap props = r.adaptTo(ValueMap.class);
    String somePropertyValue = props.get("someProperty", "");
}
shsteimer
  • 28,436
  • 30
  • 79
  • 95
0

You can use javax.jcr.Node and javax.jcr.Property interface to get properties of another component. For example, you have added component1 and component2 to page1. In the repository you should have structure similar to this:

/content
    /project
        /page1
            /jcr:content
                /parsys
                    /component1
                        /...some properties
                    /component2
                        /...some properties

If you want to get properties of component2 while in component1, you can use something like:

Node parsys = currentNode.getParent();
if(parsys.hasNode("component2")) {
    Node component2 = parsys.getNode("component2");
    if(component2.hasProperty("someProperty"))
      Property someProperty = component2.getProperty("someProperty");
}
allicarn
  • 2,859
  • 2
  • 28
  • 47
kmb
  • 871
  • 5
  • 17
  • 34