Riding Cave
Riding Cave

Reputation: 1069

how to use two different sling models(parent sling model in child components html) in sightly?

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>

Upvotes: 0

Views: 1913

Answers (1)

Tomasz Szymulewski
Tomasz Szymulewski

Reputation: 2073

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.

Upvotes: 1

Related Questions