Expand my Community achievements bar.

SOLVED

Sling mapping between component and sling model

Avatar

Level 6

Hi,

 

how  sling mapping works for UI  component and java class

for example  there is Product component  and respective Sling model is ProductModel in we.retail code.

 

How Product component will  identify ProductModel.java as sling model class on what basis it will identy.

 

can any one  explain

let me know if any information is neeed

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @manikanthar1295 ,

 

Sling models are nothing but POJO, which includes getters of the properties. Now to have the mapping between your HTML code (which is nothing but the component html/HTL), we can use data-sly-use API, like below

<div data-sly-use.nav="org.example.NavigationModel">${nav.foo}</div>

Here 'nav' is the object carrying all properties defined in Sling Model and NavigationModel is Sling Model. so NavigationModel.java will have a setter called getFoo() which you can access by ${nav.foo}.

 

Above explanation holds true for non SPA arch type projects, For SPA project you can skip referencing Sling Model inside HTL, the mapping has been specified in React component JS, like below

MapTo('my-react-app/components/content/aem-image')(ReactImage, ImageEditConfig);

were Sling model written for 'my-react-app/components/content/aem-image' resource type will get all the properties injected by react component.


You can refer this video  Sling Model

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @manikanthar1295 ,

 

Sling models are nothing but POJO, which includes getters of the properties. Now to have the mapping between your HTML code (which is nothing but the component html/HTL), we can use data-sly-use API, like below

<div data-sly-use.nav="org.example.NavigationModel">${nav.foo}</div>

Here 'nav' is the object carrying all properties defined in Sling Model and NavigationModel is Sling Model. so NavigationModel.java will have a setter called getFoo() which you can access by ${nav.foo}.

 

Above explanation holds true for non SPA arch type projects, For SPA project you can skip referencing Sling Model inside HTL, the mapping has been specified in React component JS, like below

MapTo('my-react-app/components/content/aem-image')(ReactImage, ImageEditConfig);

were Sling model written for 'my-react-app/components/content/aem-image' resource type will get all the properties injected by react component.


You can refer this video  Sling Model