Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Get foundation component property in parent node sling model

Avatar

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile
Karim_Onizuka
Level 1

05-09-2018

Hello all, I just start using Sling models, and I have an issue to retrieve a child node property in the parent model.

here is my jcr structure.

the image node is  from the foundation components. My aim is to get the "filereference" property of the image component in the Topbanner model  then in its sightly script. here is my topbanner code :

    1564751_pastedImage_5.png  

the error I am getting is Identifier Mypackage.models.TopBanner cannot be correctly instantiated by the Use API. ( in the sightly script)

Any help ?

Thank you in advance

Replies

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

05-09-2018

When using Sling Models, its better to have values - like a image for a Banner-  to be set via the component's dialog - as shown in this article:

Creating an Experience Manager 6.4 Responsive Banner Component

ie --

@Model(adaptables = Resource.class)

public class BannerList {

 

    private final Logger LOG = LoggerFactory.getLogger(getClass());

 

    @Inject @Optional

    public String bgstyle; // corresponds to the node in the dialog named bgstyle

    

    @Inject @Optional

    public String bgimage; // corresponds to the node in the dialog named bgimage

This way, all values required for the Banner can be set by an author. There is no advantage of storing these values in child nodes in the JCR.

    

Avatar

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile
Karim_Onizuka
Level 1

05-09-2018

Thank you for your answer. Actually the older version of our banner component was developed based on the  link shared in your replay.

Nevertheless in order to manage the responsivity of our component, the author should set two images based on the screen sizes.

here is a snipet of the older sightly code.

               <source media="(min-width: 768px)" srcset="${banner.backimage1 @ context='uri'}"><img style="display: block;"                            src="blank.jpg" alt="">

                <source media="(max-width: 767px)" srcset="${banner.backimage2 @ context='uri'}">

the solution is not accepted anymore by the authors. thus we opted for this second solution so  we benefit from the image component features to resize only one image for both screens.

Please let me know if there is an other alternative other than using a child node.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

05-09-2018

Its still best practice to allow component values - like text, images, videos, to be set by the component dialog. In terms of an image - a Pathbrowser resource type should be used.

This is essentially best practice for AEM components to be built.  If you are not using a component dialog - how do you plan on getting the image stored in the child node?

Avatar

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile
Karim_Onizuka
Level 1

05-09-2018

That is exactly my problem, I am using a component dialog for the text values and I would  like to combine it with the foundation image dialog ( to keep all its features )  and then get the "filereference" property of the image in my banner component model.

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

05-09-2018

If you are going to base your components on an out of the box component - look at the Core components. Start by looking at the Image COre Component and see how that component is setting an image. (I still think its better to set images via a path browser in the component dialog however)

Avatar

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile
Karim_Onizuka
Level 1

05-09-2018

Ok Thank you smacdonald2008

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

05-09-2018

Just to confirm - Core Image component uses a PathField to select the image - this confirms its best practice to select images via a dialog. This has been a good discussion...

core.png

Avatar

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Karim_Onizuka
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
View profile
Karim_Onizuka
Level 1

05-09-2018

Yes Fully agree with your point of view. I should convince the rest of my team with it.

Indeed, It was a good discussion.

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct Reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,134 likes

Total Posts

3,161 posts

Correct Reply

1,079 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

05-09-2018

I think that you have an issue in your code: in your postConstruct your are trying to adapt the component itself (that means the resources below /apps) into a model. I don't think that this will work. We could help better if you could share the exception you get during instantiation of the mode.

And then to answer your question: if you want to to pull into your model a property from a different resource, you can use the @Via annotation (check [1] for a good example for it). There's a standard way to do it, no need to write code in the postConstruct.

Jörg

[1] Apache Sling :: Sling Models