0

I have Parent Component and child components. I am including child components(3 types of html based on type) in parent component(html) using data-sly-resource in sightly. For type 3 child component I need to render Parent component's data(i have sling model for Parent Component) in html. comparing types is done from child sling model. How to achieve this funtionality?

my code is

>  <sly data-sly-use.model="com.example.MyBlog.ChildModel" data-sly-unwrap>
>    .
>    .
>   .
>  <div data-sly-test="${model.itemType} == 'type3'">
> <div>  Here I need to access data from Parent Component(either sling model or   JCR)</div>
Riding Cave
  • 1,029
  • 1
  • 15
  • 32
  • my code is . . .
    Here I need to access data from Parent Component(either sling model or JCR)
    – Riding Cave Oct 11 '16 at 15:07
  • 1
    Can you please provide some more information about the problem you are trying to solve with your models? It seems to me that you are trying to solve a problem with code which should be solved with design/architecture. Accessing the parent model from the children smells like bad design to me. If you are dead set on doing something like that you could inject the resource itself into the `ChildModel` and get its parent using the JCR API and then instantiate the `ParentModel` using the `ModelFactory` to access the information of the `ParentModel`. – Jens Oct 11 '16 at 18:37

1 Answers1

1

In your ChildModel you can add field like this:

@Self
@Via("parent")
private Resource patentResource;

this will inject a parent resource instance to your ChildModel. You can even do something like this:

@Self
@Via("parent")
private ParentModel patentModel;

this will automatically adapt your parent resource to ParentModel. After that you can expose some parent resource data in ChildModel and use it on your html.

Tomasz Szymulewski
  • 2,003
  • 23
  • 23